Skip to content

Commit

Permalink
Merge pull request #143 from GoshPosh/DATA-5475_apply_template_variab…
Browse files Browse the repository at this point in the history
…les_in_expression

[DATA-5475] Apply template variables in expression
  • Loading branch information
ShilpaSivanesan authored Mar 2, 2022
2 parents a678be6 + c808291 commit c33c1a6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
18 changes: 18 additions & 0 deletions dist/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ function (angular, _, dateMath, moment) {
console.log("Do query");
console.log(options);

// Replace the template variable in expression with its current value
options["targets"].forEach(function (target) {
if (target.hasOwnProperty("expression")) {
var expression = target.expression;
if (expression.indexOf("$") >= 0) {
var variables = datasourceSrv["templateSrv"]["variables"];
Object.keys(variables).sort().reverse() // Greedy matching
.forEach(function(key) {
var variable = "$" + variables[key]["name"];
if (expression.indexOf(variable) >= 0) {
expression = expression.replace(variable, variables[key]["current"]["value"]);
target.expression = expression;
}
});
}
}
});

var _this = this;
var promisesByRefId = {};
var promises = [];
Expand Down
18 changes: 18 additions & 0 deletions src/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ function (angular, _, dateMath, moment) {
console.log("Do query");
console.log(options);

// Replace the template variable in expression with its current value
options["targets"].forEach(function (target) {
if (target.hasOwnProperty("expression")) {
var expression = target.expression;
if (expression.indexOf("$") >= 0) {
var variables = datasourceSrv["templateSrv"]["variables"];
Object.keys(variables).sort().reverse() // Greedy matching
.forEach(function(key) {
var variable = "$" + variables[key]["name"];
if (expression.indexOf(variable) >= 0) {
expression = expression.replace(variable, variables[key]["current"]["value"]);
target.expression = expression;
}
});
}
}
});

var _this = this;
var promisesByRefId = {};
var promises = [];
Expand Down

0 comments on commit c33c1a6

Please sign in to comment.