[v10.1.x] SqlDataSources: Update metricFindQuery to pass on scopedVars to templateSrv (#73398)

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

(cherry picked from commit 3245e25273)

Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
This commit is contained in:
grafana-delivery-bot[bot]
2023-08-17 16:07:10 +03:00
committed by GitHub
co-authored by Torkel Ödegaard
parent c868461105
commit 8ce593efdc
2 changed files with 20 additions and 18 deletions
@@ -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,
@@ -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;
}