Commit b6efebd9 by Hugo Häggmark Committed by GitHub

Elastic: Fixes so templating queries work (#30003)

* Elastic: Fixes so templating queries work

* Chore: fixes test

* Fix: fixes getFields from metricFindQuery
parent b2b3a603
import { DateTime } from '../datetime/moment_wrapper'; import { dateTime, DateTime } from '../datetime/moment_wrapper';
export interface RawTimeRange { export interface RawTimeRange {
from: DateTime | string; from: DateTime | string;
...@@ -42,8 +42,10 @@ export type TimeFragment = string | DateTime; ...@@ -42,8 +42,10 @@ export type TimeFragment = string | DateTime;
export const TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss'; export const TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss';
export const DefaultTimeRange: TimeRange = { export function getDefaultTimeRange(): TimeRange {
from: {} as DateTime, return {
to: {} as DateTime, from: dateTime().subtract(6, 'hour'),
raw: { from: '6h', to: 'now' }, to: dateTime(),
}; raw: { from: 'now-6h', to: 'now' },
};
}
import React, { FC, FormEvent, MouseEvent, useState } from 'react'; import React, { FC, FormEvent, MouseEvent, useState } from 'react';
import { css, cx } from 'emotion'; import { css, cx } from 'emotion';
import { dateTime, GrafanaTheme, TimeRange, TimeZone, dateMath } from '@grafana/data'; import { dateMath, dateTime, getDefaultTimeRange, GrafanaTheme, TimeRange, TimeZone } from '@grafana/data';
import { useStyles } from '../../themes/ThemeContext'; import { useStyles } from '../../themes/ThemeContext';
import { ClickOutsideWrapper } from '../ClickOutsideWrapper/ClickOutsideWrapper'; import { ClickOutsideWrapper } from '../ClickOutsideWrapper/ClickOutsideWrapper';
import { Icon } from '../Icon/Icon'; import { Icon } from '../Icon/Icon';
...@@ -10,12 +10,6 @@ import { TimePickerButtonLabel } from './TimeRangePicker'; ...@@ -10,12 +10,6 @@ import { TimePickerButtonLabel } from './TimeRangePicker';
import { TimePickerContent } from './TimeRangePicker/TimePickerContent'; import { TimePickerContent } from './TimeRangePicker/TimePickerContent';
import { otherOptions, quickOptions } from './rangeOptions'; import { otherOptions, quickOptions } from './rangeOptions';
export const defaultTimeRange: TimeRange = {
from: dateTime().subtract(6, 'hour'),
to: dateTime(),
raw: { from: 'now-6h', to: 'now' },
};
const isValidTimeRange = (range: any) => { const isValidTimeRange = (range: any) => {
return dateMath.isValid(range.from) && dateMath.isValid(range.to); return dateMath.isValid(range.from) && dateMath.isValid(range.to);
}; };
...@@ -86,7 +80,7 @@ export const TimeRangeInput: FC<Props> = ({ ...@@ -86,7 +80,7 @@ export const TimeRangeInput: FC<Props> = ({
<ClickOutsideWrapper includeButtonPress={false} onClick={onClose}> <ClickOutsideWrapper includeButtonPress={false} onClick={onClose}>
<TimePickerContent <TimePickerContent
timeZone={timeZone} timeZone={timeZone}
value={isValidTimeRange(value) ? (value as TimeRange) : defaultTimeRange} value={isValidTimeRange(value) ? (value as TimeRange) : getDefaultTimeRange()}
onChange={onRangeChange} onChange={onRangeChange}
otherOptions={otherOptions} otherOptions={otherOptions}
quickOptions={quickOptions} quickOptions={quickOptions}
......
import { createSlice, PayloadAction } from '@reduxjs/toolkit'; import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { PanelModel } from '../../../state/PanelModel'; import { PanelModel } from '../../../state/PanelModel';
import { DefaultTimeRange, LoadingState, PanelData } from '@grafana/data'; import { getDefaultTimeRange, LoadingState, PanelData } from '@grafana/data';
import { DisplayMode } from '../types'; import { DisplayMode } from '../types';
import store from '../../../../../core/store'; import store from '../../../../../core/store';
...@@ -51,7 +51,7 @@ export const initialState = (): PanelEditorState => { ...@@ -51,7 +51,7 @@ export const initialState = (): PanelEditorState => {
getData: () => ({ getData: () => ({
state: LoadingState.NotStarted, state: LoadingState.NotStarted,
series: [], series: [],
timeRange: DefaultTimeRange, timeRange: getDefaultTimeRange(),
}), }),
initDone: false, initDone: false,
shouldDiscardChanges: false, shouldDiscardChanges: false,
......
...@@ -15,15 +15,15 @@ import { updateLocation } from 'app/core/actions'; ...@@ -15,15 +15,15 @@ import { updateLocation } from 'app/core/actions';
import { DashboardModel, PanelModel } from '../state'; import { DashboardModel, PanelModel } from '../state';
import { PANEL_BORDER } from 'app/core/constants'; import { PANEL_BORDER } from 'app/core/constants';
import { import {
LoadingState,
AbsoluteTimeRange, AbsoluteTimeRange,
DefaultTimeRange, FieldConfigSource,
toUtc, getDefaultTimeRange,
toDataFrameDTO, LoadingState,
PanelData, PanelData,
PanelPlugin, PanelPlugin,
FieldConfigSource,
PanelPluginMeta, PanelPluginMeta,
toDataFrameDTO,
toUtc,
} from '@grafana/data'; } from '@grafana/data';
import { selectors } from '@grafana/e2e-selectors'; import { selectors } from '@grafana/e2e-selectors';
import { loadSnapshotData } from '../utils/loadSnapshotData'; import { loadSnapshotData } from '../utils/loadSnapshotData';
...@@ -65,7 +65,7 @@ export class PanelChrome extends Component<Props, State> { ...@@ -65,7 +65,7 @@ export class PanelChrome extends Component<Props, State> {
data: { data: {
state: LoadingState.NotStarted, state: LoadingState.NotStarted,
series: [], series: [],
timeRange: DefaultTimeRange, timeRange: getDefaultTimeRange(),
}, },
}; };
} }
......
...@@ -13,7 +13,7 @@ import config from 'app/core/config'; ...@@ -13,7 +13,7 @@ import config from 'app/core/config';
// Types // Types
import { DashboardModel, PanelModel } from '../state'; import { DashboardModel, PanelModel } from '../state';
import { StoreState } from 'app/types'; import { StoreState } from 'app/types';
import { DefaultTimeRange, LoadingState, PanelData, PanelPlugin } from '@grafana/data'; import { getDefaultTimeRange, LoadingState, PanelData, PanelPlugin } from '@grafana/data';
import { updateLocation } from 'app/core/actions'; import { updateLocation } from 'app/core/actions';
import { PANEL_BORDER } from 'app/core/constants'; import { PANEL_BORDER } from 'app/core/constants';
import { selectors } from '@grafana/e2e-selectors'; import { selectors } from '@grafana/e2e-selectors';
...@@ -68,7 +68,7 @@ export class PanelChromeAngularUnconnected extends PureComponent<Props, State> { ...@@ -68,7 +68,7 @@ export class PanelChromeAngularUnconnected extends PureComponent<Props, State> {
data: { data: {
state: LoadingState.NotStarted, state: LoadingState.NotStarted,
series: [], series: [],
timeRange: DefaultTimeRange, timeRange: getDefaultTimeRange(),
}, },
}; };
} }
......
...@@ -5,19 +5,19 @@ import coreModule from 'app/core/core_module'; ...@@ -5,19 +5,19 @@ import coreModule from 'app/core/core_module';
// Types // Types
import { import {
dateMath, dateMath,
DefaultTimeRange,
TimeRange,
RawTimeRange,
toUtc,
dateTime, dateTime,
getDefaultTimeRange,
isDateTime, isDateTime,
rangeUtil, rangeUtil,
RawTimeRange,
TimeRange,
toUtc,
} from '@grafana/data'; } from '@grafana/data';
import { ITimeoutService, ILocationService } from 'angular'; import { ILocationService, ITimeoutService } from 'angular';
import { ContextSrv } from 'app/core/services/context_srv'; import { ContextSrv } from 'app/core/services/context_srv';
import { DashboardModel } from '../state/DashboardModel'; import { DashboardModel } from '../state/DashboardModel';
import { GrafanaRootScope } from 'app/routes/GrafanaCtrl'; import { GrafanaRootScope } from 'app/routes/GrafanaCtrl';
import { getZoomedTimeRange, getShiftedTimeRange } from 'app/core/utils/timePicker'; import { getShiftedTimeRange, getZoomedTimeRange } from 'app/core/utils/timePicker';
import { appEvents } from '../../../core/core'; import { appEvents } from '../../../core/core';
import { CoreEvents } from '../../../types'; import { CoreEvents } from '../../../types';
...@@ -41,7 +41,7 @@ export class TimeSrv { ...@@ -41,7 +41,7 @@ export class TimeSrv {
private contextSrv: ContextSrv private contextSrv: ContextSrv
) { ) {
// default time // default time
this.time = DefaultTimeRange.raw; this.time = getDefaultTimeRange().raw;
appEvents.on(CoreEvents.zoomOut, this.zoomOut.bind(this)); appEvents.on(CoreEvents.zoomOut, this.zoomOut.bind(this));
appEvents.on(CoreEvents.shiftTime, this.shiftTime.bind(this)); appEvents.on(CoreEvents.shiftTime, this.shiftTime.bind(this));
......
import { applyFieldOverrides, DefaultTimeRange, LoadingState, PanelData } from '@grafana/data'; import { applyFieldOverrides, getDefaultTimeRange, LoadingState, PanelData } from '@grafana/data';
import { config } from 'app/core/config'; import { config } from 'app/core/config';
import { DashboardModel, PanelModel } from '../state'; import { DashboardModel, PanelModel } from '../state';
import { getProcessedDataFrames } from '../../query/state/runRequest'; import { getProcessedDataFrames } from '../../query/state/runRequest';
...@@ -7,7 +7,7 @@ export function loadSnapshotData(panel: PanelModel, dashboard: DashboardModel): ...@@ -7,7 +7,7 @@ export function loadSnapshotData(panel: PanelModel, dashboard: DashboardModel):
const data = getProcessedDataFrames(panel.snapshotData); const data = getProcessedDataFrames(panel.snapshotData);
return { return {
timeRange: DefaultTimeRange, timeRange: getDefaultTimeRange(),
state: LoadingState.Done, state: LoadingState.Done,
series: applyFieldOverrides({ series: applyFieldOverrides({
data, data,
......
import { PayloadAction } from '@reduxjs/toolkit'; import { PayloadAction } from '@reduxjs/toolkit';
import { DataQuery, DefaultTimeZone, ExploreUrlState, LogsDedupStrategy, toUtc, EventBusExtended } from '@grafana/data'; import { DataQuery, DefaultTimeZone, EventBusExtended, ExploreUrlState, LogsDedupStrategy, toUtc } from '@grafana/data';
import { ExploreId, ExploreItemState, ExploreUpdateState } from 'app/types'; import { ExploreId, ExploreItemState, ExploreUpdateState } from 'app/types';
import { thunkTester } from 'test/core/thunk/thunkTester'; import { thunkTester } from 'test/core/thunk/thunkTester';
import { import {
...@@ -154,7 +154,7 @@ describe('Explore pane reducer', () => { ...@@ -154,7 +154,7 @@ describe('Explore pane reducer', () => {
changeDedupStrategyAction({ exploreId: ExploreId.left, dedupStrategy: LogsDedupStrategy.exact }) changeDedupStrategyAction({ exploreId: ExploreId.left, dedupStrategy: LogsDedupStrategy.exact })
) )
.thenStateShouldEqual({ .thenStateShouldEqual({
...makeExplorePaneState(), ...initialState,
dedupStrategy: LogsDedupStrategy.exact, dedupStrategy: LogsDedupStrategy.exact,
}); });
}); });
......
import { import {
addQueryRowAction,
cancelQueries, cancelQueries,
cancelQueriesAction, cancelQueriesAction,
scanStartAction,
scanStopAction,
queryReducer, queryReducer,
addQueryRowAction,
removeQueryRowAction, removeQueryRowAction,
scanStartAction,
scanStopAction,
} from './query'; } from './query';
import { ExploreId, ExploreItemState } from 'app/types'; import { ExploreId, ExploreItemState } from 'app/types';
import { interval } from 'rxjs'; import { interval } from 'rxjs';
...@@ -71,7 +71,7 @@ describe('reducer', () => { ...@@ -71,7 +71,7 @@ describe('reducer', () => {
.givenReducer(queryReducer, initialState) .givenReducer(queryReducer, initialState)
.whenActionIsDispatched(scanStartAction({ exploreId: ExploreId.left })) .whenActionIsDispatched(scanStartAction({ exploreId: ExploreId.left }))
.thenStateShouldEqual({ .thenStateShouldEqual({
...makeExplorePaneState(), ...initialState,
scanning: true, scanning: true,
}); });
}); });
...@@ -86,7 +86,7 @@ describe('reducer', () => { ...@@ -86,7 +86,7 @@ describe('reducer', () => {
.givenReducer(queryReducer, initialState) .givenReducer(queryReducer, initialState)
.whenActionIsDispatched(scanStopAction({ exploreId: ExploreId.left })) .whenActionIsDispatched(scanStopAction({ exploreId: ExploreId.left }))
.thenStateShouldEqual({ .thenStateShouldEqual({
...makeExplorePaneState(), ...initialState,
scanning: false, scanning: false,
scanRange: undefined, scanRange: undefined,
}); });
......
...@@ -10,7 +10,7 @@ describe('Explore item reducer', () => { ...@@ -10,7 +10,7 @@ describe('Explore item reducer', () => {
it("should result in 'streaming' state, when live-tailing is active", () => { it("should result in 'streaming' state, when live-tailing is active", () => {
const initialState = makeExplorePaneState(); const initialState = makeExplorePaneState();
const expectedState = { const expectedState = {
...makeExplorePaneState(), ...initialState,
refreshInterval: 'LIVE', refreshInterval: 'LIVE',
isLive: true, isLive: true,
loading: true, loading: true,
...@@ -19,7 +19,7 @@ describe('Explore item reducer', () => { ...@@ -19,7 +19,7 @@ describe('Explore item reducer', () => {
rows: [] as any[], rows: [] as any[],
}, },
queryResponse: { queryResponse: {
...makeExplorePaneState().queryResponse, ...initialState.queryResponse,
state: LoadingState.Streaming, state: LoadingState.Streaming,
}, },
}; };
...@@ -32,14 +32,14 @@ describe('Explore item reducer', () => { ...@@ -32,14 +32,14 @@ describe('Explore item reducer', () => {
it("should result in 'done' state, when live-tailing is stopped", () => { it("should result in 'done' state, when live-tailing is stopped", () => {
const initialState = makeExplorePaneState(); const initialState = makeExplorePaneState();
const expectedState = { const expectedState = {
...makeExplorePaneState(), ...initialState,
refreshInterval: '', refreshInterval: '',
logsResult: { logsResult: {
hasUniqueLabels: false, hasUniqueLabels: false,
rows: [] as any[], rows: [] as any[],
}, },
queryResponse: { queryResponse: {
...makeExplorePaneState().queryResponse, ...initialState.queryResponse,
state: LoadingState.Done, state: LoadingState.Done,
}, },
}; };
......
import { import {
DataSourceApi,
EventBusExtended, EventBusExtended,
DefaultTimeRange, getDefaultTimeRange,
HistoryItem,
LoadingState, LoadingState,
LogsDedupStrategy, LogsDedupStrategy,
PanelData, PanelData,
DataSourceApi,
HistoryItem,
} from '@grafana/data'; } from '@grafana/data';
import { ExploreItemState, ExploreUpdateState } from 'app/types/explore'; import { ExploreItemState, ExploreUpdateState } from 'app/types/explore';
...@@ -64,7 +64,7 @@ export const makeExplorePaneState = (): ExploreItemState => ({ ...@@ -64,7 +64,7 @@ export const makeExplorePaneState = (): ExploreItemState => ({
export const createEmptyQueryResponse = (): PanelData => ({ export const createEmptyQueryResponse = (): PanelData => ({
state: LoadingState.NotStarted, state: LoadingState.NotStarted,
series: [], series: [],
timeRange: DefaultTimeRange, timeRange: getDefaultTimeRange(),
}); });
export async function loadAndInitDatasource( export async function loadAndInitDatasource(
......
...@@ -11,11 +11,11 @@ import config from 'app/core/config'; ...@@ -11,11 +11,11 @@ import config from 'app/core/config';
// Types // Types
import { import {
DataQuery, DataQuery,
DefaultTimeRange,
LoadingState,
PanelData,
DataSourceApi, DataSourceApi,
DataSourceInstanceSettings, DataSourceInstanceSettings,
getDefaultTimeRange,
LoadingState,
PanelData,
} from '@grafana/data'; } from '@grafana/data';
import { PluginHelp } from 'app/core/components/PluginHelp/PluginHelp'; import { PluginHelp } from 'app/core/components/PluginHelp/PluginHelp';
import { addQuery } from 'app/core/utils/query'; import { addQuery } from 'app/core/utils/query';
...@@ -62,7 +62,7 @@ export class QueryGroup extends PureComponent<Props, State> { ...@@ -62,7 +62,7 @@ export class QueryGroup extends PureComponent<Props, State> {
data: { data: {
state: LoadingState.NotStarted, state: LoadingState.NotStarted,
series: [], series: [],
timeRange: DefaultTimeRange, timeRange: getDefaultTimeRange(),
}, },
}; };
......
import { of, throwError } from 'rxjs'; import { of, throwError } from 'rxjs';
import { DefaultTimeRange, LoadingState, VariableSupportType } from '@grafana/data'; import { getDefaultTimeRange, LoadingState, VariableSupportType } from '@grafana/data';
import { delay } from 'rxjs/operators'; import { delay } from 'rxjs/operators';
import { UpdateOptionsResults, VariableQueryRunner } from './VariableQueryRunner'; import { UpdateOptionsResults, VariableQueryRunner } from './VariableQueryRunner';
...@@ -46,7 +46,7 @@ function getTestContext(variable?: QueryVariableModel) { ...@@ -46,7 +46,7 @@ function getTestContext(variable?: QueryVariableModel) {
.withId('query') .withId('query')
.build(); .build();
const getTimeSrv = jest.fn().mockReturnValue({ const getTimeSrv = jest.fn().mockReturnValue({
timeRange: jest.fn().mockReturnValue(DefaultTimeRange), timeRange: jest.fn().mockReturnValue(getDefaultTimeRange()),
}); });
const datasource: any = { metricFindQuery: jest.fn().mockResolvedValue([]) }; const datasource: any = { metricFindQuery: jest.fn().mockResolvedValue([]) };
const identifier = toVariableIdentifier(variable); const identifier = toVariableIdentifier(variable);
......
...@@ -5,7 +5,7 @@ import { ...@@ -5,7 +5,7 @@ import {
DataQuery, DataQuery,
DataQueryRequest, DataQueryRequest,
DataSourceApi, DataSourceApi,
DefaultTimeRange, getDefaultTimeRange,
LoadingState, LoadingState,
ScopedVars, ScopedVars,
} from '@grafana/data'; } from '@grafana/data';
...@@ -177,7 +177,7 @@ export class VariableQueryRunner { ...@@ -177,7 +177,7 @@ export class VariableQueryRunner {
const range = const range =
variable.refresh === VariableRefresh.onTimeRangeChanged variable.refresh === VariableRefresh.onTimeRangeChanged
? this.dependencies.getTimeSrv().timeRange() ? this.dependencies.getTimeSrv().timeRange()
: DefaultTimeRange; : getDefaultTimeRange();
const request: DataQueryRequest = { const request: DataQueryRequest = {
app: CoreApp.Dashboard, app: CoreApp.Dashboard,
......
import { DefaultTimeRange, LoadingState } from '@grafana/data'; import { getDefaultTimeRange, LoadingState } from '@grafana/data';
import { variableAdapters } from '../adapters'; import { variableAdapters } from '../adapters';
import { createQueryVariableAdapter } from './adapter'; import { createQueryVariableAdapter } from './adapter';
...@@ -68,7 +68,7 @@ describe('query actions', () => { ...@@ -68,7 +68,7 @@ describe('query actions', () => {
beforeEach(() => { beforeEach(() => {
originalTimeSrv = getTimeSrv(); originalTimeSrv = getTimeSrv();
setTimeSrv(({ setTimeSrv(({
timeRange: jest.fn().mockReturnValue(DefaultTimeRange), timeRange: jest.fn().mockReturnValue(getDefaultTimeRange()),
} as unknown) as TimeSrv); } as unknown) as TimeSrv);
setVariableQueryRunner(new VariableQueryRunner()); setVariableQueryRunner(new VariableQueryRunner());
}); });
......
import { QueryRunners } from './queryRunners'; import { QueryRunners } from './queryRunners';
import { DefaultTimeRange, VariableSupportType } from '@grafana/data'; import { getDefaultTimeRange, VariableSupportType } from '@grafana/data';
import { VariableRefresh } from '../types'; import { VariableRefresh } from '../types';
import { of } from 'rxjs'; import { of } from 'rxjs';
describe('QueryRunners', () => { describe('QueryRunners', () => {
describe('when using a legacy data source', () => { describe('when using a legacy data source', () => {
const getLegacyTestContext = (variable?: any) => { const getLegacyTestContext = (variable?: any) => {
const defaultTimeRange = getDefaultTimeRange();
variable = variable ?? { query: 'A query' }; variable = variable ?? { query: 'A query' };
const timeSrv = { const timeSrv = {
timeRange: jest.fn().mockReturnValue(DefaultTimeRange), timeRange: jest.fn().mockReturnValue(defaultTimeRange),
}; };
const datasource: any = { metricFindQuery: jest.fn().mockResolvedValue([{ text: 'A', value: 'A' }]) }; const datasource: any = { metricFindQuery: jest.fn().mockResolvedValue([{ text: 'A', value: 'A' }]) };
const runner = new QueryRunners().getRunnerForDatasource(datasource); const runner = new QueryRunners().getRunnerForDatasource(datasource);
...@@ -16,7 +17,7 @@ describe('QueryRunners', () => { ...@@ -16,7 +17,7 @@ describe('QueryRunners', () => {
const runnerArgs: any = { datasource, variable, searchFilter: 'A searchFilter', timeSrv, runRequest }; const runnerArgs: any = { datasource, variable, searchFilter: 'A searchFilter', timeSrv, runRequest };
const request: any = {}; const request: any = {};
return { timeSrv, datasource, runner, variable, runnerArgs, request }; return { timeSrv, datasource, runner, variable, runnerArgs, request, defaultTimeRange };
}; };
describe('and calling getRunnerForDatasource', () => { describe('and calling getRunnerForDatasource', () => {
...@@ -35,7 +36,7 @@ describe('QueryRunners', () => { ...@@ -35,7 +36,7 @@ describe('QueryRunners', () => {
}); });
describe('and calling runRequest with a variable that refreshes when time range changes', () => { describe('and calling runRequest with a variable that refreshes when time range changes', () => {
const { datasource, runner, runnerArgs, request, timeSrv } = getLegacyTestContext({ const { datasource, runner, runnerArgs, request, timeSrv, defaultTimeRange } = getLegacyTestContext({
query: 'A query', query: 'A query',
refresh: VariableRefresh.onTimeRangeChanged, refresh: VariableRefresh.onTimeRangeChanged,
}); });
...@@ -47,7 +48,7 @@ describe('QueryRunners', () => { ...@@ -47,7 +48,7 @@ describe('QueryRunners', () => {
expect(value).toEqual({ expect(value).toEqual({
series: [{ text: 'A', value: 'A' }], series: [{ text: 'A', value: 'A' }],
state: 'Done', state: 'Done',
timeRange: { from: {}, raw: { from: '6h', to: 'now' }, to: {} }, timeRange: defaultTimeRange,
}); });
}); });
}); });
...@@ -59,14 +60,7 @@ describe('QueryRunners', () => { ...@@ -59,14 +60,7 @@ describe('QueryRunners', () => {
it('and it should call metricFindQuery with correct options', () => { it('and it should call metricFindQuery with correct options', () => {
expect(datasource.metricFindQuery).toHaveBeenCalledTimes(1); expect(datasource.metricFindQuery).toHaveBeenCalledTimes(1);
expect(datasource.metricFindQuery).toHaveBeenCalledWith('A query', { expect(datasource.metricFindQuery).toHaveBeenCalledWith('A query', {
range: { range: defaultTimeRange,
from: {},
raw: {
from: '6h',
to: 'now',
},
to: {},
},
searchFilter: 'A searchFilter', searchFilter: 'A searchFilter',
variable: { variable: {
query: 'A query', query: 'A query',
...@@ -89,7 +83,7 @@ describe('QueryRunners', () => { ...@@ -89,7 +83,7 @@ describe('QueryRunners', () => {
expect(values).toEqual({ expect(values).toEqual({
series: [{ text: 'A', value: 'A' }], series: [{ text: 'A', value: 'A' }],
state: 'Done', state: 'Done',
timeRange: { from: {}, raw: { from: '6h', to: 'now' }, to: {} }, timeRange: undefined,
}); });
}); });
}); });
......
...@@ -4,7 +4,7 @@ import { ...@@ -4,7 +4,7 @@ import {
DataQuery, DataQuery,
DataQueryRequest, DataQueryRequest,
DataSourceApi, DataSourceApi,
DefaultTimeRange, getDefaultTimeRange,
LoadingState, LoadingState,
PanelData, PanelData,
VariableSupportType, VariableSupportType,
...@@ -91,7 +91,7 @@ class LegacyQueryRunner implements QueryRunner { ...@@ -91,7 +91,7 @@ class LegacyQueryRunner implements QueryRunner {
} }
const series: any = values; const series: any = values;
return of({ series, state: LoadingState.Done, timeRange: DefaultTimeRange }); return of({ series, state: LoadingState.Done, timeRange: queryOptions.range });
}) })
); );
} }
...@@ -174,5 +174,5 @@ class DatasourceQueryRunner implements QueryRunner { ...@@ -174,5 +174,5 @@ class DatasourceQueryRunner implements QueryRunner {
} }
function getEmptyMetricFindValueObservable(): Observable<PanelData> { function getEmptyMetricFindValueObservable(): Observable<PanelData> {
return of({ state: LoadingState.Done, series: [], timeRange: DefaultTimeRange }); return of({ state: LoadingState.Done, series: [], timeRange: getDefaultTimeRange() });
} }
import { of } from 'rxjs'; import { of } from 'rxjs';
import { setBackendSrv } from '@grafana/runtime'; import { setBackendSrv } from '@grafana/runtime';
import { dateTime, DefaultTimeRange } from '@grafana/data'; import { dateTime, getDefaultTimeRange } from '@grafana/data';
import { TemplateSrv } from '../../../features/templating/template_srv'; import { TemplateSrv } from '../../../features/templating/template_srv';
import { CloudWatchDatasource } from './datasource'; import { CloudWatchDatasource } from './datasource';
...@@ -86,7 +86,7 @@ describe('datasource', () => { ...@@ -86,7 +86,7 @@ describe('datasource', () => {
function setup({ data = [] }: { data?: any } = {}) { function setup({ data = [] }: { data?: any } = {}) {
const datasource = new CloudWatchDatasource({ jsonData: { defaultRegion: 'us-west-1' } } as any, new TemplateSrv(), { const datasource = new CloudWatchDatasource({ jsonData: { defaultRegion: 'us-west-1' } } as any, new TemplateSrv(), {
timeRange() { timeRange() {
return DefaultTimeRange; return getDefaultTimeRange();
}, },
} as any); } as any);
const fetchMock = jest.fn().mockReturnValue(of({ data })); const fetchMock = jest.fn().mockReturnValue(of({ data }));
......
...@@ -2,7 +2,14 @@ import { Observable } from 'rxjs'; ...@@ -2,7 +2,14 @@ import { Observable } from 'rxjs';
import { QueryRunnerOptions } from 'app/features/query/state/PanelQueryRunner'; import { QueryRunnerOptions } from 'app/features/query/state/PanelQueryRunner';
import { DashboardQuery, SHARED_DASHBODARD_QUERY } from './types'; import { DashboardQuery, SHARED_DASHBODARD_QUERY } from './types';
import { getDashboardSrv } from 'app/features/dashboard/services/DashboardSrv'; import { getDashboardSrv } from 'app/features/dashboard/services/DashboardSrv';
import { LoadingState, DefaultTimeRange, DataQuery, PanelData, DataSourceApi, DataQueryRequest } from '@grafana/data'; import {
DataQuery,
DataQueryRequest,
DataSourceApi,
getDefaultTimeRange,
LoadingState,
PanelData,
} from '@grafana/data';
export function isSharedDashboardQuery(datasource: string | DataSourceApi | null) { export function isSharedDashboardQuery(datasource: string | DataSourceApi | null) {
if (!datasource) { if (!datasource) {
...@@ -72,6 +79,6 @@ function getQueryError(msg: string): PanelData { ...@@ -72,6 +79,6 @@ function getQueryError(msg: string): PanelData {
series: [], series: [],
request: {} as DataQueryRequest, request: {} as DataQueryRequest,
error: { message: msg }, error: { message: msg },
timeRange: DefaultTimeRange, timeRange: getDefaultTimeRange(),
}; };
} }
import _ from 'lodash'; import _ from 'lodash';
import { import {
DataSourceApi,
DataSourceInstanceSettings,
DataQueryRequest,
DataQueryResponse,
DataFrame, DataFrame,
ScopedVars,
DataLink, DataLink,
PluginMeta,
DataQuery, DataQuery,
LogRowModel, DataQueryRequest,
DataQueryResponse,
DataSourceApi,
DataSourceInstanceSettings,
DateTime,
dateTime,
Field, Field,
getDefaultTimeRange,
LogRowModel,
MetricFindValue, MetricFindValue,
PluginMeta,
ScopedVars,
TimeRange, TimeRange,
DefaultTimeRange, toUtc,
DateTime,
dateTime,
} from '@grafana/data'; } from '@grafana/data';
import LanguageProvider from './language_provider'; import LanguageProvider from './language_provider';
import { ElasticResponse } from './elastic_response'; import { ElasticResponse } from './elastic_response';
import { IndexPattern } from './index_pattern'; import { IndexPattern } from './index_pattern';
import { ElasticQueryBuilder } from './query_builder'; import { ElasticQueryBuilder } from './query_builder';
import { toUtc } from '@grafana/data';
import { defaultBucketAgg, hasMetricOfType } from './query_def'; import { defaultBucketAgg, hasMetricOfType } from './query_def';
import { getBackendSrv, getDataSourceSrv } from '@grafana/runtime'; import { getBackendSrv, getDataSourceSrv } from '@grafana/runtime';
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv'; import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
...@@ -142,7 +142,7 @@ export class ElasticDatasource extends DataSourceApi<ElasticsearchQuery, Elastic ...@@ -142,7 +142,7 @@ export class ElasticDatasource extends DataSourceApi<ElasticsearchQuery, Elastic
* *
* @param url the url to query the index on, for example `/_mapping`. * @param url the url to query the index on, for example `/_mapping`.
*/ */
private get(url: string, range = DefaultTimeRange) { private get(url: string, range = getDefaultTimeRange()) {
const indexList = this.indexPattern.getIndexList(range.from, range.to); const indexList = this.indexPattern.getIndexList(range.from, range.to);
if (_.isArray(indexList) && indexList.length) { if (_.isArray(indexList) && indexList.length) {
return this.requestAllIndices(indexList, url).then((results: any) => { return this.requestAllIndices(indexList, url).then((results: any) => {
...@@ -667,7 +667,7 @@ export class ElasticDatasource extends DataSourceApi<ElasticsearchQuery, Elastic ...@@ -667,7 +667,7 @@ export class ElasticDatasource extends DataSourceApi<ElasticsearchQuery, Elastic
}); });
} }
getTerms(queryDef: any, range = DefaultTimeRange) { getTerms(queryDef: any, range = getDefaultTimeRange()) {
const searchType = this.esVersion >= 5 ? 'query_then_fetch' : 'count'; const searchType = this.esVersion >= 5 ? 'query_then_fetch' : 'count';
const header = this.getQueryHeader(searchType, range.from, range.to); const header = this.getQueryHeader(searchType, range.from, range.to);
let esQuery = JSON.stringify(this.queryBuilder.getTermsQuery(queryDef)); let esQuery = JSON.stringify(this.queryBuilder.getTermsQuery(queryDef));
...@@ -706,14 +706,14 @@ export class ElasticDatasource extends DataSourceApi<ElasticsearchQuery, Elastic ...@@ -706,14 +706,14 @@ export class ElasticDatasource extends DataSourceApi<ElasticsearchQuery, Elastic
const parsedQuery = JSON.parse(query); const parsedQuery = JSON.parse(query);
if (query) { if (query) {
if (parsedQuery.find === 'fields') { if (parsedQuery.find === 'fields') {
parsedQuery.field = this.templateSrv.replace(parsedQuery.field, {}, 'lucene'); parsedQuery.type = this.templateSrv.replace(parsedQuery.type, {}, 'lucene');
return this.getFields(query, range); return this.getFields(parsedQuery.type, range);
} }
if (parsedQuery.find === 'terms') { if (parsedQuery.find === 'terms') {
parsedQuery.field = this.templateSrv.replace(parsedQuery.field, {}, 'lucene'); parsedQuery.field = this.templateSrv.replace(parsedQuery.field, {}, 'lucene');
parsedQuery.query = this.templateSrv.replace(parsedQuery.query || '*', {}, 'lucene'); parsedQuery.query = this.templateSrv.replace(parsedQuery.query || '*', {}, 'lucene');
return this.getTerms(query, range); return this.getTerms(parsedQuery, range);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment