From 10a4a8990283eb3b82aaf172d4bcf373e521c989 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=A4ggmark?= Date: Tue, 4 Jun 2019 14:07:25 +0200 Subject: [PATCH] Fix: Adds context to list of keys that are not part of query (#17423) Fixes: #17408 --- public/app/core/utils/explore.test.ts | 4 ++-- public/app/core/utils/explore.ts | 16 +++++++++------- .../prometheus/components/PromQueryField.tsx | 4 ++-- .../plugins/datasource/prometheus/datasource.ts | 11 +++++++---- .../app/plugins/datasource/prometheus/types.ts | 7 ++++++- 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/public/app/core/utils/explore.test.ts b/public/app/core/utils/explore.test.ts index 9e11fddd629..344de380320 100644 --- a/public/app/core/utils/explore.test.ts +++ b/public/app/core/utils/explore.test.ts @@ -181,11 +181,11 @@ describe('updateHistory()', () => { describe('hasNonEmptyQuery', () => { test('should return true if one query is non-empty', () => { - expect(hasNonEmptyQuery([{ refId: '1', key: '2', expr: 'foo' }])).toBeTruthy(); + expect(hasNonEmptyQuery([{ refId: '1', key: '2', context: 'explore', expr: 'foo' }])).toBeTruthy(); }); test('should return false if query is empty', () => { - expect(hasNonEmptyQuery([{ refId: '1', key: '2' }])).toBeFalsy(); + expect(hasNonEmptyQuery([{ refId: '1', key: '2', context: 'panel' }])).toBeFalsy(); }); test('should return false if no queries exist', () => { diff --git a/public/app/core/utils/explore.ts b/public/app/core/utils/explore.ts index 4a4697d7d0a..811950a9251 100644 --- a/public/app/core/utils/explore.ts +++ b/public/app/core/utils/explore.ts @@ -303,17 +303,19 @@ export function ensureQueries(queries?: DataQuery[]): DataQuery[] { } /** - * A target is non-empty when it has keys (with non-empty values) other than refId and key. + * A target is non-empty when it has keys (with non-empty values) other than refId, key and context. */ +const validKeys = ['refId', 'key', 'context']; export function hasNonEmptyQuery(queries: TQuery[]): boolean { return ( queries && - queries.some( - query => - Object.keys(query) - .map(k => query[k]) - .filter(v => v).length > 2 - ) + queries.some(query => { + const keys = Object.keys(query) + .filter(key => validKeys.indexOf(key) === -1) + .map(k => query[k]) + .filter(v => v); + return keys.length > 0; + }) ); } diff --git a/public/app/plugins/datasource/prometheus/components/PromQueryField.tsx b/public/app/plugins/datasource/prometheus/components/PromQueryField.tsx index d42447b6746..7efb9185b04 100644 --- a/public/app/plugins/datasource/prometheus/components/PromQueryField.tsx +++ b/public/app/plugins/datasource/prometheus/components/PromQueryField.tsx @@ -13,7 +13,7 @@ import { TypeaheadOutput, HistoryItem } from 'app/types/explore'; import { getNextCharacter, getPreviousCousin } from 'app/features/explore/utils/dom'; import BracesPlugin from 'app/features/explore/slate-plugins/braces'; import QueryField, { TypeaheadInput, QueryFieldState } from 'app/features/explore/QueryField'; -import { PromQuery } from '../types'; +import { PromQuery, PromContext } from '../types'; import { CancelablePromise, makePromiseCancelable } from 'app/core/utils/CancelablePromise'; import { DataSourceApi, ExploreQueryFieldProps, DataSourceStatus, QueryHint } from '@grafana/ui'; @@ -223,7 +223,7 @@ class PromQueryField extends React.PureComponent { type: string; @@ -224,7 +224,7 @@ export class PrometheusDatasource extends DataSourceApi continue; } - if (target.context === 'explore') { + if (target.context === PromContext.Explore) { target.format = 'time_series'; target.instant = false; const instantTarget: any = _.cloneDeep(target); @@ -260,7 +260,10 @@ export class PrometheusDatasource extends DataSourceApi return this.$q.when({ data: [] }) as Promise<{ data: any }>; } - if (observer && options.targets.filter(target => target.context === 'explore').length === options.targets.length) { + if ( + observer && + options.targets.filter(target => target.context === PromContext.Explore).length === options.targets.length + ) { // using observer to make the instant query return immediately this.runObserverQueries(options, observer, queries, activeTargets, end); return this.$q.when({ data: [] }) as Promise<{ data: any }>; diff --git a/public/app/plugins/datasource/prometheus/types.ts b/public/app/plugins/datasource/prometheus/types.ts index a256f289cfe..7971ad65bde 100644 --- a/public/app/plugins/datasource/prometheus/types.ts +++ b/public/app/plugins/datasource/prometheus/types.ts @@ -1,8 +1,13 @@ import { DataQuery, DataSourceJsonData } from '@grafana/ui/src/types'; +export enum PromContext { + Explore = 'explore', + Panel = 'panel', +} + export interface PromQuery extends DataQuery { expr: string; - context?: 'explore' | 'panel'; + context?: PromContext; format?: string; instant?: boolean; hinting?: boolean;