Skip to content

Commit 6e04d6c

Browse files
authored
add dashbaord template var query interpolation (#22)
1 parent 6e0701f commit 6e04d6c

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/datasource.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
LogLevel,
1010
} from '@grafana/data';
1111
import { Observable } from 'rxjs';
12-
import { getBackendSrv } from '@grafana/runtime';
12+
import { getBackendSrv, getTemplateSrv } from '@grafana/runtime';
1313
import { queryLogsVolume } from './features/log/LogsModel';
1414

1515
import { MyQuery, MyDataSourceOptions, CachedQuery } from './types';
@@ -45,9 +45,19 @@ export class DataSource
4545
this.timestampColumn = instanceSettings.jsonData.timestamp_column;
4646
}
4747

48+
applyTemplateVariables(query: MyQuery, scopedVars: any): MyQuery {
49+
return {
50+
...query,
51+
query: getTemplateSrv().replace(query.query || '', scopedVars),
52+
};
53+
}
54+
4855
async query(options: DataQueryRequest<MyQuery>): Promise<DataQueryResponse> {
4956
const timestamps = getConsumableTime(options.range);
50-
const promises = options.targets.map((target) => {
57+
const interpolatedTargets = options.targets.map((target) => {
58+
return this.applyTemplateVariables(target, options.scopedVars);
59+
});
60+
const promises = interpolatedTargets.map((target) => {
5161
if (!this.cachedQuery.data) {
5262
this.cachedQuery.data = new Promise((resolve, reject) => {
5363
this.cachedQuery.promise = {

0 commit comments

Comments
 (0)