Datasources: Fix expressions that reference hidden queries (#84977)
call expression ds before filter query
This commit is contained in:
@@ -12,6 +12,7 @@ import {
|
||||
PanelData,
|
||||
} from '@grafana/data';
|
||||
import { setEchoSrv } from '@grafana/runtime';
|
||||
import { ExpressionDatasourceRef } from '@grafana/runtime/src/utils/DataSourceWithBackend';
|
||||
import { DataQuery } from '@grafana/schema';
|
||||
|
||||
import { deepFreeze } from '../../../../test/core/redux/reducerTester';
|
||||
@@ -35,6 +36,12 @@ jest.mock('app/features/dashboard/services/DashboardSrv', () => ({
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock('app/features/expressions/ExpressionDatasource', () => ({
|
||||
dataSource: {
|
||||
query: jest.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
class ScenarioCtx {
|
||||
ds!: DataSourceApi;
|
||||
request!: DataQueryRequest;
|
||||
@@ -532,6 +539,28 @@ describe('callQueryMethod', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('Should not call filterQuery when targets include expression query', async () => {
|
||||
setup({
|
||||
targets: [
|
||||
{
|
||||
refId: 'A',
|
||||
q: 'SUM(foo)',
|
||||
},
|
||||
{
|
||||
refId: 'B',
|
||||
q: 'SUM(foo2)',
|
||||
},
|
||||
{
|
||||
datasource: ExpressionDatasourceRef,
|
||||
refId: 'C',
|
||||
q: 'SUM(foo3)',
|
||||
},
|
||||
],
|
||||
filterQuery: (query: DataQuery) => query.refId !== 'A',
|
||||
});
|
||||
expect(filterQuerySpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('Should get ds default query when query is empty', async () => {
|
||||
setup({
|
||||
targets: [
|
||||
|
||||
@@ -201,15 +201,6 @@ export function callQueryMethod(
|
||||
: t
|
||||
);
|
||||
|
||||
// do not filter queries in case a custom query function is provided (for example in variable queries)
|
||||
if (!queryFunction) {
|
||||
request.targets = request.targets.filter((t) => datasource.filterQuery?.(t) ?? true);
|
||||
}
|
||||
|
||||
if (request.targets.length === 0) {
|
||||
return of<DataQueryResponse>({ data: [] });
|
||||
}
|
||||
|
||||
// If its a public datasource, just return the result. Expressions will be handled on the backend.
|
||||
if (config.publicDashboardAccessToken) {
|
||||
return from(datasource.query(request));
|
||||
@@ -221,6 +212,15 @@ export function callQueryMethod(
|
||||
}
|
||||
}
|
||||
|
||||
// do not filter queries in case a custom query function is provided (for example in variable queries)
|
||||
if (!queryFunction) {
|
||||
request.targets = request.targets.filter((t) => datasource.filterQuery?.(t) ?? true);
|
||||
}
|
||||
|
||||
if (request.targets.length === 0) {
|
||||
return of<DataQueryResponse>({ data: [] });
|
||||
}
|
||||
|
||||
// Otherwise it is a standard datasource request
|
||||
const returnVal = queryFunction ? queryFunction(request) : datasource.query(request);
|
||||
return from(returnVal);
|
||||
|
||||
Reference in New Issue
Block a user