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[];
}
export interface LegacyMetricFindQueryOptions {
searchFilter?: string;
scopedVars?: ScopedVars;
range?: TimeRange;
variable?: { name: string };
}
export interface QueryEditorProps<
DSType extends DataSourceApi<TQuery, TOptions>,
TQuery extends DataQuery = DataQuery,

View File

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