SqlDataSources: Update metricFindQuery to pass on scopedVars to templateSrv (#73333)

This commit is contained in:
Torkel Ödegaard 2023-08-17 14:58:54 +02:00 committed by GitHub
parent c8a9adf52a
commit 3245e25273
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 18 deletions

View File

@ -383,6 +383,13 @@ export interface MetadataInspectorProps<
data: DataFrame[]; data: DataFrame[];
} }
export interface LegacyMetricFindQueryOptions {
searchFilter?: string;
scopedVars?: ScopedVars;
range?: TimeRange;
variable?: { name: string };
}
export interface QueryEditorProps< export interface QueryEditorProps<
DSType extends DataSourceApi<TQuery, TOptions>, DSType extends DataSourceApi<TQuery, TOptions>,
TQuery extends DataQuery = DataQuery, TQuery extends DataQuery = DataQuery,

View File

@ -11,10 +11,9 @@ import {
DataSourceRef, DataSourceRef,
MetricFindValue, MetricFindValue,
ScopedVars, ScopedVars,
TimeRange,
CoreApp, CoreApp,
getSearchFilterScopedVar, getSearchFilterScopedVar,
SearchFilterOptions, LegacyMetricFindQueryOptions,
} from '@grafana/data'; } from '@grafana/data';
import { EditorMode } from '@grafana/experimental'; import { EditorMode } from '@grafana/experimental';
import { import {
@ -172,17 +171,18 @@ export abstract class SqlDatasource extends DataSourceWithBackend<SQLQuery, SQLO
return; return;
} }
async metricFindQuery(query: string, optionalOptions?: MetricFindQueryOptions): Promise<MetricFindValue[]> { async metricFindQuery(query: string, options?: LegacyMetricFindQueryOptions): Promise<MetricFindValue[]> {
let refId = 'tempvar'; let refId = 'tempvar';
if (optionalOptions && optionalOptions.variable && optionalOptions.variable.name) { if (options && options.variable && options.variable.name) {
refId = optionalOptions.variable.name; refId = options.variable.name;
} }
const rawSql = this.templateSrv.replace( const scopedVars = {
query, ...options?.scopedVars,
getSearchFilterScopedVar({ query, wildcardChar: '%', options: optionalOptions }), ...getSearchFilterScopedVar({ query, wildcardChar: '%', options }),
this.interpolateVariable };
);
const rawSql = this.templateSrv.replace(query, scopedVars, this.interpolateVariable);
const interpolatedQuery: SQLQuery = { const interpolatedQuery: SQLQuery = {
refId: refId, refId: refId,
@ -191,7 +191,7 @@ export abstract class SqlDatasource extends DataSourceWithBackend<SQLQuery, SQLO
format: QueryFormat.Table, format: QueryFormat.Table,
}; };
const response = await this.runMetaQuery(interpolatedQuery, optionalOptions); const response = await this.runMetaQuery(interpolatedQuery, options);
return this.getResponseParser().transformMetricFindResponse(response); return this.getResponseParser().transformMetricFindResponse(response);
} }
@ -200,7 +200,7 @@ export abstract class SqlDatasource extends DataSourceWithBackend<SQLQuery, SQLO
return new DataFrameView<T>(frame); return new DataFrameView<T>(frame);
} }
private runMetaQuery(request: Partial<SQLQuery>, options?: MetricFindQueryOptions): Promise<DataFrame> { private runMetaQuery(request: Partial<SQLQuery>, options?: LegacyMetricFindQueryOptions): Promise<DataFrame> {
const range = getTimeSrv().timeRange(); const range = getTimeSrv().timeRange();
const refId = request.refId || 'meta'; const refId = request.refId || 'meta';
const queries: DataQuery[] = [{ ...request, datasource: request.datasource || this.getRef(), refId }]; const queries: DataQuery[] = [{ ...request, datasource: request.datasource || this.getRef(), refId }];
@ -236,11 +236,6 @@ export abstract class SqlDatasource extends DataSourceWithBackend<SQLQuery, SQLO
} }
} }
interface RunSQLOptions extends MetricFindQueryOptions { interface RunSQLOptions extends LegacyMetricFindQueryOptions {
refId?: string; refId?: string;
} }
interface MetricFindQueryOptions extends SearchFilterOptions {
range?: TimeRange;
variable?: VariableWithMultiSupport;
}