From f7522b63226fe87c0427c8c57bcf6ef8277a8690 Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Tue, 5 Sep 2023 15:34:23 -0700 Subject: [PATCH] Plugins: Move filter back to DataSourceWithBackend (#74147) --- packages/grafana-data/src/types/datasource.ts | 6 ++---- .../src/utils/DataSourceWithBackend.ts | 10 ++++++++++ .../unified/state/AlertingQueryRunner.test.ts | 15 +++++++++++---- .../alerting/unified/state/AlertingQueryRunner.ts | 7 +++++-- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/packages/grafana-data/src/types/datasource.ts b/packages/grafana-data/src/types/datasource.ts index 2644c14e063..6149b39f337 100644 --- a/packages/grafana-data/src/types/datasource.ts +++ b/packages/grafana-data/src/types/datasource.ts @@ -263,11 +263,9 @@ abstract class DataSourceApi< abstract testDatasource(): Promise; /** - * 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; diff --git a/packages/grafana-runtime/src/utils/DataSourceWithBackend.ts b/packages/grafana-runtime/src/utils/DataSourceWithBackend.ts index 86502103a21..3a3fa775306 100644 --- a/packages/grafana-runtime/src/utils/DataSourceWithBackend.ts +++ b/packages/grafana-runtime/src/utils/DataSourceWithBackend.ts @@ -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. diff --git a/public/app/features/alerting/unified/state/AlertingQueryRunner.test.ts b/public/app/features/alerting/unified/state/AlertingQueryRunner.test.ts index 6d037bf6cc7..324083d74b2 100644 --- a/public/app/features/alerting/unified/state/AlertingQueryRunner.test.ts +++ b/public/app/features/alerting/unified/state/AlertingQueryRunner.test.ts @@ -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) => { +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; }; diff --git a/public/app/features/alerting/unified/state/AlertingQueryRunner.ts b/public/app/features/alerting/unified/state/AlertingQueryRunner.ts index 62fd898c836..e4f4ad23e7e 100644 --- a/public/app/features/alerting/unified/state/AlertingQueryRunner.ts +++ b/public/app/features/alerting/unified/state/AlertingQueryRunner.ts @@ -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);