Skip to content
This repository was archived by the owner on Jan 1, 2026. It is now read-only.

Commit 9d1273f

Browse files
authored
fix: interpret variables via templateSrv.replace (#703)
1 parent b8d8b91 commit 9d1273f

2 files changed

Lines changed: 8 additions & 41 deletions

File tree

src/DataSource.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import {
2626
QueryRequest,
2727
VariableQuery,
2828
} from './types';
29-
import { match, P } from 'ts-pattern';
30-
import { valueFromVariableWithMultiSupport } from './variable/valueFromVariableWithMultiSupport';
3129
import { VariableSupport } from './variable/VariableSupport';
3230
import { doFetch } from './doFetch';
3331
import { MetricFindQuery } from './MetricFindQuery';
@@ -64,14 +62,16 @@ export class DataSource extends DataSourceApi<GrafanaQuery, GenericOptions> {
6462
}
6563

6664
query(options: QueryRequest): Promise<DataQueryResponse> {
67-
const request = this.processTargets(options);
65+
options.scopedVars = { ...this.getVariables(options.scopedVars), ...options.scopedVars };
66+
const mergedVars = { ...this.getVariables(options.scopedVars), ...options.scopedVars };
67+
const nextOptions = { ...options, scopedVars: mergedVars };
68+
69+
const request = this.processTargets(nextOptions);
6870

6971
if (request.targets.length === 0) {
7072
return Promise.resolve({ data: [] });
7173
}
7274

73-
options.scopedVars = { ...this.getVariables(), ...options.scopedVars };
74-
7575
return lastValueFrom(
7676
doFetch<any[]>(this, {
7777
url: `${this.url}/query`,
@@ -289,7 +289,7 @@ export class DataSource extends DataSourceApi<GrafanaQuery, GenericOptions> {
289289
return replacedMatch;
290290
}
291291

292-
getVariables() {
292+
getVariables(scopedVars: ScopedVars | undefined = undefined) {
293293
const variableOptions: Record<VariableWithMultiSupport['id'], VariableOption> = {};
294294

295295
Object.values(getTemplateSrv().getVariables()).forEach((variable) => {
@@ -302,19 +302,9 @@ export class DataSource extends DataSourceApi<GrafanaQuery, GenericOptions> {
302302
return;
303303
}
304304

305-
const value = match(variable)
306-
.with({ type: P.union('custom', 'query') }, (v) => valueFromVariableWithMultiSupport(v))
307-
.with(
308-
{ type: P.union('constant', 'datasource', 'groupby', 'interval', 'snapshot', 'textbox') },
309-
(v) => v.current.value
310-
)
311-
.exhaustive();
312-
313-
if (value === undefined) {
314-
return;
315-
}
305+
const value = getTemplateSrv().replace('$' + variable.name, scopedVars, 'json');
316306

317-
variableOptions[variable.id] = {
307+
variableOptions[variable.name] = {
318308
selected: false,
319309
text: variable.current.text,
320310
value: value,

src/variable/valueFromVariableWithMultiSupport.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)