Explore: Fixes issue with adhoc filters when coming from dashboards (#78339)

This commit is contained in:
Torkel Ödegaard 2023-11-20 20:22:01 +01:00 committed by GitHub
parent 2aeb96f422
commit d03b291eed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 5 deletions

View File

@ -3,6 +3,7 @@ import { Unsubscribable } from 'rxjs';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import { import {
AdHocVariableFilter,
CoreApp, CoreApp,
DataQuery, DataQuery,
DataQueryRequest, DataQueryRequest,
@ -49,6 +50,7 @@ export interface GetExploreUrlArguments {
dsRef: DataSourceRef | null | undefined; dsRef: DataSourceRef | null | undefined;
timeRange: TimeRange; timeRange: TimeRange;
scopedVars: ScopedVars | undefined; scopedVars: ScopedVars | undefined;
adhocFilters?: AdHocVariableFilter[];
} }
export function generateExploreId() { export function generateExploreId() {
@ -59,7 +61,7 @@ export function generateExploreId() {
* Returns an Explore-URL that contains a panel's queries and the dashboard time range. * Returns an Explore-URL that contains a panel's queries and the dashboard time range.
*/ */
export async function getExploreUrl(args: GetExploreUrlArguments): Promise<string | undefined> { export async function getExploreUrl(args: GetExploreUrlArguments): Promise<string | undefined> {
const { queries, dsRef, timeRange, scopedVars } = args; const { queries, dsRef, timeRange, scopedVars, adhocFilters } = args;
const interpolatedQueries = ( const interpolatedQueries = (
await Promise.allSettled( await Promise.allSettled(
queries queries
@ -72,7 +74,7 @@ export async function getExploreUrl(args: GetExploreUrlArguments): Promise<strin
return { return {
// interpolate the query using its datasource `interpolateVariablesInQueries` method if defined, othewise return the query as-is. // interpolate the query using its datasource `interpolateVariablesInQueries` method if defined, othewise return the query as-is.
...(queryDs.interpolateVariablesInQueries?.([q], scopedVars ?? {})[0] || q), ...(queryDs.interpolateVariablesInQueries?.([q], scopedVars ?? {}, adhocFilters)[0] || q),
// But always set the datasource as it's required in Explore. // But always set the datasource as it's required in Explore.
// NOTE: if for some reason the query has the "mixed" datasource, we omit the property; // NOTE: if for some reason the query has the "mixed" datasource, we omit the property;
// Upon initialization, Explore use its own logic to determine the datasource. // Upon initialization, Explore use its own logic to determine the datasource.

View File

@ -12,6 +12,13 @@ import { ExploreItemState, ExploreState, StoreState, ThunkDispatch } from '../..
import { exploreReducer, navigateToExplore, splitClose, splitOpen } from './main'; import { exploreReducer, navigateToExplore, splitClose, splitOpen } from './main';
jest.mock('@grafana/runtime', () => ({
...jest.requireActual('@grafana/runtime'),
getDataSourceSrv: () => ({
getInstanceSettings: jest.fn().mockReturnValue({}),
}),
}));
const getNavigateToExploreContext = async (openInNewWindow?: (url: string) => void) => { const getNavigateToExploreContext = async (openInNewWindow?: (url: string) => void) => {
const url = '/explore'; const url = '/explore';
const panel: Partial<PanelModel> = { const panel: Partial<PanelModel> = {
@ -53,6 +60,7 @@ describe('navigateToExplore', () => {
queries: panel.targets, queries: panel.targets,
timeRange, timeRange,
dsRef: panel.datasource, dsRef: panel.datasource,
adhocFilters: [],
}); });
}); });
}); });
@ -73,6 +81,7 @@ describe('navigateToExplore', () => {
queries: panel.targets, queries: panel.targets,
timeRange, timeRange,
dsRef: panel.datasource, dsRef: panel.datasource,
adhocFilters: [],
}); });
}); });

View File

@ -5,6 +5,7 @@ import { SplitOpenOptions, TimeRange, EventBusSrv } from '@grafana/data';
import { locationService } from '@grafana/runtime'; import { locationService } from '@grafana/runtime';
import { generateExploreId, GetExploreUrlArguments } from 'app/core/utils/explore'; import { generateExploreId, GetExploreUrlArguments } from 'app/core/utils/explore';
import { PanelModel } from 'app/features/dashboard/state'; import { PanelModel } from 'app/features/dashboard/state';
import { getTemplateSrv } from 'app/features/templating/template_srv';
import { CorrelationEditorDetailsUpdate, ExploreItemState, ExploreState } from 'app/types/explore'; import { CorrelationEditorDetailsUpdate, ExploreItemState, ExploreState } from 'app/types/explore';
import { RichHistoryResults } from '../../../core/history/RichHistoryStorage'; import { RichHistoryResults } from '../../../core/history/RichHistoryStorage';
@ -131,6 +132,7 @@ export const navigateToExplore = (
dsRef: panel.datasource, dsRef: panel.datasource,
scopedVars: panel.scopedVars, scopedVars: panel.scopedVars,
timeRange, timeRange,
adhocFilters: getTemplateSrv().getAdhocFilters(panel.datasource?.uid ?? '', true),
}); });
if (openInNewWindow && path) { if (openInNewWindow && path) {

View File

@ -303,7 +303,7 @@ export class PanelQueryRunner {
request.interval = norm.interval; request.interval = norm.interval;
request.intervalMs = norm.intervalMs; request.intervalMs = norm.intervalMs;
request.filters = this.templateSrv.getAdhocFilters(ds.name); request.filters = this.templateSrv.getAdhocFilters(ds.name, true);
this.lastRequest = request; this.lastRequest = request;

View File

@ -123,7 +123,7 @@ export class TemplateSrv implements BaseTemplateSrv {
* Use filters property on the request (DataQueryRequest) or if this is called from * Use filters property on the request (DataQueryRequest) or if this is called from
* interpolateVariablesInQueries or applyTemplateVariables it is passed as a new argument * interpolateVariablesInQueries or applyTemplateVariables it is passed as a new argument
**/ **/
getAdhocFilters(datasourceName: string): AdHocVariableFilter[] { getAdhocFilters(datasourceName: string, skipDeprecationWarning?: boolean): AdHocVariableFilter[] {
let filters: any = []; let filters: any = [];
let ds = getDataSourceSrv().getInstanceSettings(datasourceName); let ds = getDataSourceSrv().getInstanceSettings(datasourceName);
@ -131,7 +131,7 @@ export class TemplateSrv implements BaseTemplateSrv {
return []; return [];
} }
if (!this._adhocFiltersDeprecationWarningLogged.get(ds.type)) { if (!skipDeprecationWarning && !this._adhocFiltersDeprecationWarningLogged.get(ds.type)) {
if (process.env.NODE_ENV !== 'test') { if (process.env.NODE_ENV !== 'test') {
deprecationWarning( deprecationWarning(
`DataSource ${ds.type}`, `DataSource ${ds.type}`,