From 055405eb650ac24d5b12ed77a4b3f843d37d0bab Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Mon, 25 Oct 2021 07:57:46 -0600 Subject: [PATCH] Explore: Fix running queries without a datasource property set (#40805) (#40881) * Explore: Fix running queries without a datasource property set * fix test * adjust test (cherry picked from commit 2ee54b7ffec6ce5384268a9591d74b8a4da9ce8a) Co-authored-by: Giordano Ricci --- public/app/core/utils/explore.test.ts | 2 +- public/app/core/utils/explore.ts | 7 +++++-- public/app/features/explore/state/query.ts | 6 +++++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/public/app/core/utils/explore.test.ts b/public/app/core/utils/explore.test.ts index d049094ac6f..7262ea496f0 100644 --- a/public/app/core/utils/explore.test.ts +++ b/public/app/core/utils/explore.test.ts @@ -243,7 +243,7 @@ describe('hasNonEmptyQuery', () => { }); test('should return false if query is empty', () => { - expect(hasNonEmptyQuery([{ refId: '1', key: '2', context: 'panel' }])).toBeFalsy(); + expect(hasNonEmptyQuery([{ refId: '1', key: '2', context: 'panel', datasource: 'some-ds' }])).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 c81962bcf5e..81d06f96532 100644 --- a/public/app/core/utils/explore.ts +++ b/public/app/core/utils/explore.ts @@ -282,9 +282,12 @@ export function ensureQueries(queries?: DataQuery[]): DataQuery[] { } /** - * A target is non-empty when it has keys (with non-empty values) other than refId, key and context. + * A target is non-empty when it has keys (with non-empty values) other than refId, key, context and datasource. + * FIXME: While this is reasonable for practical use cases, a query without any propery might still be "non-empty" + * in its own scope, for instance when there's no user input needed. This might be the case for an hypothetic datasource in + * which query options are only set in its config and the query object itself, as generated from its query editor it's always "empty" */ -const validKeys = ['refId', 'key', 'context']; +const validKeys = ['refId', 'key', 'context', 'datasource']; export function hasNonEmptyQuery(queries: TQuery[]): boolean { return ( queries && diff --git a/public/app/features/explore/state/query.ts b/public/app/features/explore/state/query.ts index 82b556409fc..fd0a4bc32fd 100644 --- a/public/app/features/explore/state/query.ts +++ b/public/app/features/explore/state/query.ts @@ -285,7 +285,6 @@ export const runQueries = ( const exploreItemState = getState().explore[exploreId]!; const { datasourceInstance, - queries, containerWidth, isLive: live, range, @@ -299,6 +298,11 @@ export const runQueries = ( } = exploreItemState; let newQuerySub; + const queries = exploreItemState.queries.map((query) => ({ + ...query, + datasource: query.datasource || datasourceInstance?.name, + })); + const cachedValue = getResultsFromCache(cache, absoluteRange); // If we have results saved in cache, we are going to use those results instead of running queries