Plugins: Move filter back to DataSourceWithBackend (#74147)

This commit is contained in:
Ryan McKinley
2023-09-06 01:34:23 +03:00
committed by GitHub
parent 87e8b654a2
commit f7522b6322
4 changed files with 28 additions and 10 deletions
@@ -263,11 +263,9 @@ abstract class DataSourceApi<
abstract testDatasource(): Promise<TestDataSourceResponse>;
/**
* Override to skip executing a query
* This function is not called automatically unless running within the DataSourceWithBackend
*
* @returns false if the query should be skipped
*
* @virtual
* @deprecated
*/
filterQuery?(query: TQuery): boolean;
@@ -267,6 +267,16 @@ class DataSourceWithBackend<
return queries.map((q) => this.applyTemplateVariables(q, scopedVars) as TQuery);
}
/**
* Override to skip executing a query. Note this function may not be called
* if the query method is overwritten.
*
* @returns false if the query should be skipped
*
* @virtual
*/
filterQuery?(query: TQuery): boolean;
/**
* Override to apply template variables. The result is usually also `TQuery`, but sometimes this can
* be used to modify the query structure before sending to the backend.
@@ -6,14 +6,15 @@ import { createFetchResponse } from 'test/helpers/createFetchResponse';
import {
DataFrame,
DataFrameJSON,
DataSourceApi,
Field,
FieldType,
getDefaultRelativeTimeRange,
LoadingState,
rangeUtil,
DataSourceInstanceSettings,
} from '@grafana/data';
import { DataSourceSrv, FetchResponse } from '@grafana/runtime';
import { DataSourceSrv, FetchResponse, DataSourceWithBackend } from '@grafana/runtime';
import { DataQuery } from '@grafana/schema';
import { BackendSrv } from 'app/core/services/backend_srv';
import { AlertDataQuery, AlertQuery } from 'app/types/unified-alerting-dto';
@@ -256,9 +257,15 @@ const mockBackendSrv = ({ fetch }: MockBackendSrvConfig): BackendSrv => {
} as unknown as BackendSrv;
};
const mockDataSourceSrv = (dsApi?: Partial<DataSourceApi>) => {
interface MockOpts {
filterQuery?: (query: DataQuery) => boolean;
}
const mockDataSourceSrv = (opts?: MockOpts) => {
const ds = new DataSourceWithBackend({} as unknown as DataSourceInstanceSettings);
ds.filterQuery = opts?.filterQuery;
return {
get: () => Promise.resolve(dsApi ?? {}),
get: () => Promise.resolve(ds),
} as unknown as DataSourceSrv;
};
@@ -14,7 +14,7 @@ import {
withLoadingIndicator,
preProcessPanelData,
} from '@grafana/data';
import { FetchResponse, getDataSourceSrv, toDataQueryError } from '@grafana/runtime';
import { FetchResponse, getDataSourceSrv, toDataQueryError, DataSourceWithBackend } from '@grafana/runtime';
import { BackendSrv, getBackendSrv } from 'app/core/services/backend_srv';
import { isExpressionQuery } from 'app/features/expressions/guards';
import { cancelNetworkRequestsOnUnsubscribe } from 'app/features/query/state/processing/canceler';
@@ -61,7 +61,10 @@ export class AlertingQueryRunner {
}
const dataSourceInstance = await this.dataSourceSrv.get(query.datasourceUid);
const skipRunningQuery = dataSourceInstance.filterQuery && !dataSourceInstance.filterQuery(query.model);
const skipRunningQuery =
dataSourceInstance instanceof DataSourceWithBackend &&
dataSourceInstance.filterQuery &&
!dataSourceInstance.filterQuery(query.model);
if (skipRunningQuery) {
queriesToExclude.push(refId);