From 5f6a3ecd56ac8fffa1d43e0b6bd9e3a439bdc82e Mon Sep 17 00:00:00 2001 From: Mitsuhiro Tanda Date: Tue, 6 Dec 2016 16:16:41 +0900 Subject: [PATCH] (cloudwatch) don't expand variable when panel or row repeat is used (#6618) * (cloudwatch) don't expand variable when panel or row repeat is used * fix test --- public/app/features/templating/templateSrv.js | 12 ++++++++++-- .../app/plugins/datasource/cloudwatch/datasource.js | 6 +++--- .../datasource/cloudwatch/specs/datasource_specs.ts | 3 ++- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/public/app/features/templating/templateSrv.js b/public/app/features/templating/templateSrv.js index f2aa317351c..a41f5a9d0f3 100644 --- a/public/app/features/templating/templateSrv.js +++ b/public/app/features/templating/templateSrv.js @@ -112,10 +112,18 @@ function (angular, _, kbn) { this._grafanaVariables[name] = value; }; - this.variableExists = function(expression) { + this.getVariableName = function(expression) { this._regex.lastIndex = 0; var match = this._regex.exec(expression); - return match && (self._index[match[1] || match[2]] !== void 0); + if (!match) { + return null; + } + return match[1] || match[2]; + }; + + this.variableExists = function(expression) { + var name = this.getVariableName(expression); + return name && (self._index[name] !== void 0); }; this.highlightVariablesAsHtml = function(str) { diff --git a/public/app/plugins/datasource/cloudwatch/datasource.js b/public/app/plugins/datasource/cloudwatch/datasource.js index acef572367d..dc57c41b94d 100644 --- a/public/app/plugins/datasource/cloudwatch/datasource.js +++ b/public/app/plugins/datasource/cloudwatch/datasource.js @@ -31,7 +31,7 @@ function (angular, _, moment, dateMath, kbn, CloudWatchAnnotationQuery) { var queries = []; options = angular.copy(options); - options.targets = this.expandTemplateVariable(options.targets, templateSrv); + options.targets = this.expandTemplateVariable(options.targets, options.scopedVars, templateSrv); _.each(options.targets, function(target) { if (target.hide || !target.namespace || !target.metricName || _.isEmpty(target.statistics)) { return; @@ -412,12 +412,12 @@ function (angular, _, moment, dateMath, kbn, CloudWatchAnnotationQuery) { return str.indexOf('$' + variableName) !== -1; }; - this.expandTemplateVariable = function(targets, templateSrv) { + this.expandTemplateVariable = function(targets, scopedVars, templateSrv) { var self = this; return _.chain(targets) .map(function(target) { var dimensionKey = _.findKey(target.dimensions, function(v) { - return templateSrv.variableExists(v); + return templateSrv.variableExists(v) && !_.has(scopedVars, templateSrv.getVariableName(v)); }); if (dimensionKey) { diff --git a/public/app/plugins/datasource/cloudwatch/specs/datasource_specs.ts b/public/app/plugins/datasource/cloudwatch/specs/datasource_specs.ts index a834fdb2973..93dfa24036d 100644 --- a/public/app/plugins/datasource/cloudwatch/specs/datasource_specs.ts +++ b/public/app/plugins/datasource/cloudwatch/specs/datasource_specs.ts @@ -139,6 +139,7 @@ describe('CloudWatchDatasource', function() { ] } ], + getVariableName: function (e) { return 'instance_id'; }, variableExists: function (e) { return true; }, containsVariable: function (str, variableName) { return str.indexOf('$' + variableName) !== -1; } }; @@ -156,7 +157,7 @@ describe('CloudWatchDatasource', function() { } ]; - var result = ctx.ds.expandTemplateVariable(targets, templateSrv); + var result = ctx.ds.expandTemplateVariable(targets, {}, templateSrv); expect(result[0].dimensions.InstanceId).to.be('i-34567890'); }); });