Queries: Move explore state out of query targets (#26203)

* Datasource/CloudWatch: Interal vars no longer sent with query requests

Closes #26202
This commit is contained in:
kay delaney 2020-07-16 14:00:28 +01:00 committed by GitHub
parent d4e4cb4c71
commit 1124da56d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 41 additions and 27 deletions

View File

@ -433,6 +433,11 @@ export interface DataQueryRequest<TQuery extends DataQuery = DataQuery> {
// Request Timing // Request Timing
startTime: number; startTime: number;
endTime?: number; endTime?: number;
// Explore state used by various datasources
liveStreaming?: boolean;
showingGraph?: boolean;
showingTable?: boolean;
} }
export interface DataQueryTimings { export interface DataQueryTimings {

View File

@ -124,7 +124,6 @@ export function buildQueryTransaction(
scanning: boolean, scanning: boolean,
timeZone?: TimeZone timeZone?: TimeZone
): QueryTransaction { ): QueryTransaction {
const configuredQueries = queries.map(query => ({ ...query, ...queryOptions }));
const key = queries.reduce((combinedKey, query) => { const key = queries.reduce((combinedKey, query) => {
combinedKey += query.key; combinedKey += query.key;
return combinedKey; return combinedKey;
@ -149,7 +148,7 @@ export function buildQueryTransaction(
// TODO: the query request expects number and we are using string here. Seems like it works so far but can create // TODO: the query request expects number and we are using string here. Seems like it works so far but can create
// issues down the road. // issues down the road.
panelId: panelId as any, panelId: panelId as any,
targets: configuredQueries, // Datasources rely on DataQueries being passed under the targets key. targets: queries, // Datasources rely on DataQueries being passed under the targets key.
range, range,
requestId: 'explore', requestId: 'explore',
rangeRaw: range.raw, rangeRaw: range.raw,
@ -159,6 +158,9 @@ export function buildQueryTransaction(
}, },
maxDataPoints: queryOptions.maxDataPoints, maxDataPoints: queryOptions.maxDataPoints,
exploreMode: queryOptions.mode, exploreMode: queryOptions.mode,
liveStreaming: queryOptions.liveStreaming,
showingGraph: queryOptions.showingGraph,
showingTable: queryOptions.showingTable,
}; };
return { return {

View File

@ -1,4 +1,4 @@
import LokiDatasource, { RangeQueryOptions } from './datasource'; import LokiDatasource from './datasource';
import { LokiQuery, LokiResponse, LokiResultType } from './types'; import { LokiQuery, LokiResponse, LokiResultType } from './types';
import { getQueryOptions } from 'test/helpers/getQueryOptions'; import { getQueryOptions } from 'test/helpers/getQueryOptions';
import { AnnotationQueryRequest, DataFrame, DataSourceApi, dateTime, FieldCache, TimeRange } from '@grafana/data'; import { AnnotationQueryRequest, DataFrame, DataSourceApi, dateTime, FieldCache, TimeRange } from '@grafana/data';
@ -81,7 +81,7 @@ describe('LokiDatasource', () => {
intervalMs: 2000, intervalMs: 2000,
}; };
const req = ds.createRangeQuery(target, options); const req = ds.createRangeQuery(target, options as any);
expect(req.start).toBeDefined(); expect(req.start).toBeDefined();
expect(req.end).toBeDefined(); expect(req.end).toBeDefined();
expect(adjustIntervalSpy).toHaveBeenCalledWith(2000, expect.anything()); expect(adjustIntervalSpy).toHaveBeenCalledWith(2000, expect.anything());
@ -345,8 +345,8 @@ describe('LokiDatasource', () => {
raw: { from: '0', to: '1000000001' }, raw: { from: '0', to: '1000000001' },
}; };
// Odd timerange/interval combination that would lead to a float step // Odd timerange/interval combination that would lead to a float step
const options: RangeQueryOptions = { range, intervalMs: 2000 }; const options = { range, intervalMs: 2000 };
expect(Number.isInteger(ds.createRangeQuery(query, options).step!)).toBeTruthy(); expect(Number.isInteger(ds.createRangeQuery(query, options as any).step!)).toBeTruthy();
}); });
}); });

View File

@ -42,7 +42,7 @@ import LanguageProvider from './language_provider';
import { serializeParams } from '../../../core/utils/fetch'; import { serializeParams } from '../../../core/utils/fetch';
import { RowContextOptions } from '@grafana/ui/src/components/Logs/LogRowContextProvider'; import { RowContextOptions } from '@grafana/ui/src/components/Logs/LogRowContextProvider';
export type RangeQueryOptions = Pick<DataQueryRequest<LokiQuery>, 'range' | 'intervalMs' | 'maxDataPoints' | 'reverse'>; export type RangeQueryOptions = DataQueryRequest<LokiQuery> | AnnotationQueryRequest<LokiQuery>;
export const DEFAULT_MAX_LINES = 1000; export const DEFAULT_MAX_LINES = 1000;
export const LOKI_ENDPOINT = '/loki/api/v1'; export const LOKI_ENDPOINT = '/loki/api/v1';
@ -143,7 +143,9 @@ export class LokiDatasource extends DataSourceApi<LokiQuery, LokiOptions> {
const startNs = this.getTime(options.range.from, false); const startNs = this.getTime(options.range.from, false);
const endNs = this.getTime(options.range.to, true); const endNs = this.getTime(options.range.to, true);
const rangeMs = Math.ceil((endNs - startNs) / 1e6); const rangeMs = Math.ceil((endNs - startNs) / 1e6);
const step = Math.ceil(this.adjustInterval(options.intervalMs || 1000, rangeMs) / 1000); const step = Math.ceil(
this.adjustInterval((options as DataQueryRequest<LokiQuery>).intervalMs || 1000, rangeMs) / 1000
);
const alignedTimes = { const alignedTimes = {
start: startNs - (startNs % 1e9), start: startNs - (startNs % 1e9),
end: endNs + (1e9 - (endNs % 1e9)), end: endNs + (1e9 - (endNs % 1e9)),
@ -160,7 +162,7 @@ export class LokiDatasource extends DataSourceApi<LokiQuery, LokiOptions> {
...DEFAULT_QUERY_PARAMS, ...DEFAULT_QUERY_PARAMS,
...range, ...range,
query, query,
limit: Math.min(options.maxDataPoints || Infinity, this.maxLines), limit: Math.min((options as DataQueryRequest<LokiQuery>).maxDataPoints || Infinity, this.maxLines),
}; };
} }
@ -182,7 +184,7 @@ export class LokiDatasource extends DataSourceApi<LokiQuery, LokiOptions> {
let linesLimit = 0; let linesLimit = 0;
if (target.maxLines === undefined) { if (target.maxLines === undefined) {
// no target.maxLines, using options.maxDataPoints // no target.maxLines, using options.maxDataPoints
linesLimit = Math.min(options.maxDataPoints || Infinity, this.maxLines); linesLimit = Math.min((options as DataQueryRequest<LokiQuery>).maxDataPoints || Infinity, this.maxLines);
} else { } else {
// using target.maxLines // using target.maxLines
if (isNaN(target.maxLines)) { if (isNaN(target.maxLines)) {
@ -193,7 +195,7 @@ export class LokiDatasource extends DataSourceApi<LokiQuery, LokiOptions> {
} }
const queryOptions = { ...options, maxDataPoints: linesLimit }; const queryOptions = { ...options, maxDataPoints: linesLimit };
if (target.liveStreaming) { if ((options as DataQueryRequest<LokiQuery>).liveStreaming) {
return this.runLiveQuery(target, queryOptions); return this.runLiveQuery(target, queryOptions);
} }
const query = this.createRangeQuery(target, queryOptions); const query = this.createRangeQuery(target, queryOptions);
@ -207,7 +209,7 @@ export class LokiDatasource extends DataSourceApi<LokiQuery, LokiOptions> {
responseListLength, responseListLength,
linesLimit, linesLimit,
this.instanceSettings.jsonData, this.instanceSettings.jsonData,
options.reverse (options as DataQueryRequest<LokiQuery>).reverse
) )
) )
); );

View File

@ -24,7 +24,6 @@ export enum LokiResultType {
export interface LokiQuery extends DataQuery { export interface LokiQuery extends DataQuery {
expr: string; expr: string;
liveStreaming?: boolean;
query?: string; query?: string;
format?: string; format?: string;
reverse?: boolean; reverse?: boolean;

View File

@ -21,6 +21,7 @@ import templateSrv from 'app/features/templating/template_srv';
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv'; import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
import { VariableHide } from '../../../features/variables/types'; import { VariableHide } from '../../../features/variables/types';
import { describe } from '../../../../test/lib/common'; import { describe } from '../../../../test/lib/common';
import { QueryOptions } from 'app/types';
const datasourceRequestMock = jest.fn().mockResolvedValue(createDefaultPromResponse()); const datasourceRequestMock = jest.fn().mockResolvedValue(createDefaultPromResponse());
@ -1683,7 +1684,7 @@ describe('PrometheusDatasource for POST', () => {
}); });
}); });
const getPrepareTargetsContext = (target: PromQuery, app?: CoreApp) => { const getPrepareTargetsContext = (target: PromQuery, app?: CoreApp, queryOptions?: Partial<QueryOptions>) => {
const instanceSettings = ({ const instanceSettings = ({
url: 'proxied', url: 'proxied',
directUrl: 'direct', directUrl: 'direct',
@ -1694,7 +1695,9 @@ const getPrepareTargetsContext = (target: PromQuery, app?: CoreApp) => {
const start = 0; const start = 0;
const end = 1; const end = 1;
const panelId = '2'; const panelId = '2';
const options = ({ targets: [target], interval: '1s', panelId, app } as any) as DataQueryRequest<PromQuery>; const options = ({ targets: [target], interval: '1s', panelId, app, ...queryOptions } as any) as DataQueryRequest<
PromQuery
>;
const ds = new PrometheusDatasource(instanceSettings); const ds = new PrometheusDatasource(instanceSettings);
const { queries, activeTargets } = ds.prepareTargets(options, start, end); const { queries, activeTargets } = ds.prepareTargets(options, start, end);
@ -1744,11 +1747,12 @@ describe('prepareTargets', () => {
const target: PromQuery = { const target: PromQuery = {
refId: 'A', refId: 'A',
expr: 'up', expr: 'up',
showingGraph: true,
showingTable: true,
}; };
const { queries, activeTargets, panelId, end, start } = getPrepareTargetsContext(target, CoreApp.Explore); const { queries, activeTargets, panelId, end, start } = getPrepareTargetsContext(target, CoreApp.Explore, {
showingGraph: true,
showingTable: true,
});
expect(queries.length).toBe(2); expect(queries.length).toBe(2);
expect(activeTargets.length).toBe(2); expect(activeTargets.length).toBe(2);
@ -1817,11 +1821,12 @@ describe('prepareTargets', () => {
const target: PromQuery = { const target: PromQuery = {
refId: 'A', refId: 'A',
expr: 'up', expr: 'up',
showingGraph: false,
showingTable: true,
}; };
const { queries, activeTargets, panelId, end, start } = getPrepareTargetsContext(target, CoreApp.Explore); const { queries, activeTargets, panelId, end, start } = getPrepareTargetsContext(target, CoreApp.Explore, {
showingGraph: false,
showingTable: true,
});
expect(queries.length).toBe(1); expect(queries.length).toBe(1);
expect(activeTargets.length).toBe(1); expect(activeTargets.length).toBe(1);
@ -1854,11 +1859,12 @@ describe('prepareTargets', () => {
const target: PromQuery = { const target: PromQuery = {
refId: 'A', refId: 'A',
expr: 'up', expr: 'up',
showingGraph: true,
showingTable: false,
}; };
const { queries, activeTargets, panelId, end, start } = getPrepareTargetsContext(target, CoreApp.Explore); const { queries, activeTargets, panelId, end, start } = getPrepareTargetsContext(target, CoreApp.Explore, {
showingGraph: true,
showingTable: false,
});
expect(queries.length).toBe(1); expect(queries.length).toBe(1);
expect(activeTargets.length).toBe(1); expect(activeTargets.length).toBe(1);
@ -1958,7 +1964,6 @@ function createDataRequest(targets: any[], overrides?: Partial<DataQueryRequest>
start: dateTime().subtract(5, 'minutes'), start: dateTime().subtract(5, 'minutes'),
end: dateTime(), end: dateTime(),
expr: 'test', expr: 'test',
showingGraph: true,
...t, ...t,
}; };
}), }),
@ -1967,6 +1972,7 @@ function createDataRequest(targets: any[], overrides?: Partial<DataQueryRequest>
to: dateTime(), to: dateTime(),
}, },
interval: '15s', interval: '15s',
showingGraph: true,
}; };
return Object.assign(defaults, overrides || {}) as DataQueryRequest<PromQuery>; return Object.assign(defaults, overrides || {}) as DataQueryRequest<PromQuery>;

View File

@ -205,7 +205,7 @@ export class PrometheusDatasource extends DataSourceApi<PromQuery, PromOptions>
continue; continue;
} }
if (target.showingTable) { if (options.showingTable) {
// create instant target only if Table is showed in Explore // create instant target only if Table is showed in Explore
const instantTarget: any = cloneDeep(target); const instantTarget: any = cloneDeep(target);
instantTarget.format = 'table'; instantTarget.format = 'table';
@ -218,7 +218,7 @@ export class PrometheusDatasource extends DataSourceApi<PromQuery, PromOptions>
queries.push(this.createQuery(instantTarget, options, start, end)); queries.push(this.createQuery(instantTarget, options, start, end));
} }
if (target.showingGraph) { if (options.showingGraph) {
// create time series target only if Graph is showed in Explore // create time series target only if Graph is showed in Explore
target.format = 'time_series'; target.format = 'time_series';
target.instant = false; target.instant = false;