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

Commit 51debf5

Browse files
authored
refactor: further vars cleanup (#747)
1 parent 6e72292 commit 51debf5

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

src/DataSource.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
VariableWithMultiSupport,
1212
} from '@grafana/data';
1313
import { config, FetchResponse, getTemplateSrv, TemplateSrv } from '@grafana/runtime';
14-
import { isArray, isObject } from 'lodash';
14+
import { isArray, isEmpty, isObject } from 'lodash';
1515
import { lastValueFrom } from 'rxjs';
1616
import { map } from 'rxjs/operators';
1717
import { ResponseParser } from './response_parser';
@@ -62,7 +62,6 @@ export class DataSource extends DataSourceApi<GrafanaQuery, GenericOptions> {
6262
}
6363

6464
query(options: QueryRequest): Promise<DataQueryResponse> {
65-
options.scopedVars = { ...this.getVariables(options.scopedVars), ...options.scopedVars };
6665
const mergedVars = { ...this.getVariables(options.scopedVars), ...options.scopedVars };
6766
const nextOptions = { ...options, scopedVars: mergedVars };
6867

@@ -290,7 +289,7 @@ export class DataSource extends DataSourceApi<GrafanaQuery, GenericOptions> {
290289
}
291290

292291
getVariables(scopedVars: ScopedVars | undefined = undefined) {
293-
const variableOptions: Record<VariableWithMultiSupport['id'], VariableOption> = {};
292+
const variableOptions: Record<VariableWithMultiSupport['id'], Omit<VariableOption, 'selected'>> = {};
294293

295294
Object.values(getTemplateSrv().getVariables()).forEach((variable) => {
296295
if (variable.type === 'adhoc') {
@@ -302,10 +301,13 @@ export class DataSource extends DataSourceApi<GrafanaQuery, GenericOptions> {
302301
return;
303302
}
304303

305-
const value = getTemplateSrv().replace('$' + variable.name, scopedVars);
304+
if (isEmpty(variable.current)) {
305+
return;
306+
}
307+
308+
const value = getTemplateSrv().replace('$' + variable.name, scopedVars, 'json');
306309

307310
variableOptions[variable.name] = {
308-
selected: false,
309311
text: variable.current.text,
310312
value: value,
311313
};

0 commit comments

Comments
 (0)