From 6a3c6eb1748a6e449b7d2d988ce86a6167332f2d Mon Sep 17 00:00:00 2001 From: Giordano Ricci Date: Mon, 19 Jun 2023 10:46:59 +0100 Subject: [PATCH] Explore: Ensure datasource is present in the URL for every query (#70307) --- public/app/features/explore/ExplorePage.test.tsx | 7 ++++--- public/app/features/explore/hooks/useStateSync.ts | 7 ++++++- public/app/features/explore/spec/query.test.tsx | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/public/app/features/explore/ExplorePage.test.tsx b/public/app/features/explore/ExplorePage.test.tsx index c9c5b5fcef2..5b9eff30b1d 100644 --- a/public/app/features/explore/ExplorePage.test.tsx +++ b/public/app/features/explore/ExplorePage.test.tsx @@ -90,14 +90,15 @@ describe('ExplorePage', () => { const urlParams = { left: serializeStateToUrlParam({ datasource: 'loki-uid', - queries: [{ refId: 'A', expr: '{ label="value"}' }], + queries: [{ refId: 'A', expr: '{ label="value"}', datasource: { type: 'logs', uid: 'loki-uid' } }], range: { from: 'now-1h', to: 'now' }, }), right: serializeStateToUrlParam({ datasource: 'elastic-uid', - queries: [{ refId: 'A', expr: 'error' }], + queries: [{ refId: 'A', expr: 'error', datasource: { type: 'logs', uid: 'elastic-uid' } }], range: { from: 'now-1h', to: 'now' }, }), + orgId: '1', }; const { datasources, location } = setupExplore({ urlParams }); @@ -330,7 +331,7 @@ describe('ExplorePage', () => { await waitFor(() => { const urlParams = decodeURIComponent(location.getSearch().toString()); expect(urlParams).toBe( - 'left={"datasource":"elastic-uid","queries":[{"refId":"A"}],"range":{"from":"now-1h","to":"now"}}&orgId=1' + 'left={"datasource":"elastic-uid","queries":[{"refId":"A","datasource":{"type":"logs","uid":"elastic-uid"}}],"range":{"from":"now-1h","to":"now"}}&orgId=1' ); }); diff --git a/public/app/features/explore/hooks/useStateSync.ts b/public/app/features/explore/hooks/useStateSync.ts index bb622b37b94..7b8a0c6237a 100644 --- a/public/app/features/explore/hooks/useStateSync.ts +++ b/public/app/features/explore/hooks/useStateSync.ts @@ -1,4 +1,4 @@ -import { isEmpty, isEqual, isObject, mapValues, omitBy } from 'lodash'; +import { identity, isEmpty, isEqual, isObject, mapValues, omitBy } from 'lodash'; import { useEffect, useRef } from 'react'; import { @@ -183,6 +183,11 @@ export function useStateSync(params: ExploreQueryParams) { withUniqueRefIds(queries) // but filter out the ones that are not compatible with the pane datasource .filter(getQueryFilter(paneDatasource)) + .map( + isMixedDatasource(paneDatasource) + ? identity + : (query) => ({ ...query, datasource: paneDatasource.getRef() }) + ) : getDatasourceSrv() // otherwise we get a default query from the pane datasource or from the default datasource if the pane datasource is mixed .get(isMixedDatasource(paneDatasource) ? undefined : paneDatasource.getRef()) diff --git a/public/app/features/explore/spec/query.test.tsx b/public/app/features/explore/spec/query.test.tsx index 0a8a8dcaa8d..cfc8e69e286 100644 --- a/public/app/features/explore/spec/query.test.tsx +++ b/public/app/features/explore/spec/query.test.tsx @@ -30,7 +30,7 @@ describe('Explore: handle running/not running query', () => { const urlParams = { left: serializeStateToUrlParam({ datasource: 'loki-uid', - queries: [{ refId: 'A', expr: '{ label="value"}' }], + queries: [{ refId: 'A', expr: '{ label="value"}', datasource: { type: 'logs', uid: 'loki-uid' } }], range: { from: 'now-1h', to: 'now' }, }), };