Explore: Ensure datasource is present in the URL for every query (#70307)

This commit is contained in:
Giordano Ricci
2023-06-19 10:46:59 +01:00
committed by GitHub
parent 1bfa3a0f1e
commit 6a3c6eb174
3 changed files with 11 additions and 5 deletions
@@ -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'
);
});
@@ -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<DataQuery>
: (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())
@@ -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' },
}),
};