Skip to content

Commit 469f0bf

Browse files
authored
fix: parse custom and query variables using our special function (#760)
1 parent fd248d7 commit 469f0bf

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/DataSource.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import {
2929
import { VariableSupport } from './variable/VariableSupport';
3030
import { doFetch } from './doFetch';
3131
import { MetricFindQuery } from './MetricFindQuery';
32+
import { match, P } from 'ts-pattern';
33+
import { valueFromVariableWithMultiSupport } from './variable/valueFromVariableWithMultiSupport';
3234

3335
export class DataSource extends DataSourceApi<GrafanaQuery, GenericOptions> {
3436
url: string;
@@ -305,7 +307,12 @@ export class DataSource extends DataSourceApi<GrafanaQuery, GenericOptions> {
305307
return;
306308
}
307309

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

310317
variableOptions[variable.name] = {
311318
text: variable.current.text,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { VariableWithMultiSupport } from '@grafana/data';
2+
import { isEqual } from 'lodash';
3+
4+
export const valueFromVariableWithMultiSupport = (variable: VariableWithMultiSupport) => {
5+
let variableValue = variable.current.value;
6+
if (variableValue === '$__all' || isEqual(variableValue, ['$__all'])) {
7+
if (variable.allValue === null || variable.allValue === '' || variable.allValue === undefined) {
8+
variableValue = variable.options.slice(1).map((variableOption) => {
9+
if (typeof variableOption.value !== 'string') {
10+
const error = new Error('Variable option value is not a string');
11+
console.error(error.message, variableOption, variable);
12+
13+
throw error;
14+
}
15+
return variableOption.value;
16+
});
17+
} else {
18+
variableValue = variable.allValue;
19+
}
20+
}
21+
22+
return variableValue;
23+
};

0 commit comments

Comments
 (0)