Skip to content

Commit

Permalink
Plugins: Move filter back to DataSourceWithBackend (grafana#74147)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryantxu authored and chauchausoup committed Sep 15, 2023
1 parent 7f73c9f commit 13dd060
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
6 changes: 2 additions & 4 deletions packages/grafana-data/src/types/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
10 changes: 10 additions & 0 deletions packages/grafana-runtime/src/utils/DataSourceWithBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 13dd060

Please sign in to comment.