mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
support [[var]] in cloudwatch datasource (#7649)
* support [[var]] form in cloudwatch datasource * support multi variable in single dimension * fix test
This commit is contained in:
parent
d88286ab81
commit
68bb417db9
@ -4,9 +4,10 @@ define([
|
||||
'moment',
|
||||
'app/core/utils/datemath',
|
||||
'app/core/utils/kbn',
|
||||
'app/features/templating/variable',
|
||||
'./annotation_query',
|
||||
],
|
||||
function (angular, _, moment, dateMath, kbn, CloudWatchAnnotationQuery) {
|
||||
function (angular, _, moment, dateMath, kbn, templatingVariable, CloudWatchAnnotationQuery) {
|
||||
'use strict';
|
||||
|
||||
/** @ngInject */
|
||||
@ -390,7 +391,7 @@ function (angular, _, moment, dateMath, kbn, CloudWatchAnnotationQuery) {
|
||||
});
|
||||
}
|
||||
|
||||
this.getExpandedVariables = function(target, dimensionKey, variable) {
|
||||
this.getExpandedVariables = function(target, dimensionKey, variable, templateSrv) {
|
||||
/* if the all checkbox is marked we should add all values to the targets */
|
||||
var allSelected = _.find(variable.options, {'selected': true, 'text': 'All'});
|
||||
return _.chain(variable.options)
|
||||
@ -403,15 +404,13 @@ function (angular, _, moment, dateMath, kbn, CloudWatchAnnotationQuery) {
|
||||
})
|
||||
.map(function(v) {
|
||||
var t = angular.copy(target);
|
||||
t.dimensions[dimensionKey] = v.value;
|
||||
var scopedVar = {};
|
||||
scopedVar[variable.name] = v;
|
||||
t.dimensions[dimensionKey] = templateSrv.replace(t.dimensions[dimensionKey], scopedVar);
|
||||
return t;
|
||||
}).value();
|
||||
};
|
||||
|
||||
this.containsVariable = function (str, variableName) {
|
||||
return str.indexOf('$' + variableName) !== -1;
|
||||
};
|
||||
|
||||
this.expandTemplateVariable = function(targets, scopedVars, templateSrv) {
|
||||
var self = this;
|
||||
return _.chain(targets)
|
||||
@ -421,10 +420,13 @@ function (angular, _, moment, dateMath, kbn, CloudWatchAnnotationQuery) {
|
||||
});
|
||||
|
||||
if (dimensionKey) {
|
||||
var variable = _.find(templateSrv.variables, function(variable) {
|
||||
return self.containsVariable(target.dimensions[dimensionKey], variable.name);
|
||||
var multiVariable = _.find(templateSrv.variables, function(variable) {
|
||||
return templatingVariable.containsVariable(target.dimensions[dimensionKey], variable.name) && variable.multi;
|
||||
});
|
||||
return self.getExpandedVariables(target, dimensionKey, variable);
|
||||
var variable = _.find(templateSrv.variables, function(variable) {
|
||||
return templatingVariable.containsVariable(target.dimensions[dimensionKey], variable.name);
|
||||
});
|
||||
return self.getExpandedVariables(target, dimensionKey, multiVariable || variable, templateSrv);
|
||||
} else {
|
||||
return [target];
|
||||
}
|
||||
|
@ -134,11 +134,18 @@ describe('CloudWatchDatasource', function() {
|
||||
{
|
||||
name: 'instance_id',
|
||||
options: [
|
||||
{ value: 'i-23456789', selected: false },
|
||||
{ value: 'i-34567890', selected: true }
|
||||
{ text: 'i-23456789', value: 'i-23456789', selected: false },
|
||||
{ text: 'i-34567890', value: 'i-34567890', selected: true }
|
||||
]
|
||||
}
|
||||
],
|
||||
replace: function (target, scopedVars) {
|
||||
if (target === '$instance_id' && scopedVars['instance_id']['text'] === 'i-34567890') {
|
||||
return 'i-34567890';
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
},
|
||||
getVariableName: function (e) { return 'instance_id'; },
|
||||
variableExists: function (e) { return true; },
|
||||
containsVariable: function (str, variableName) { return str.indexOf('$' + variableName) !== -1; }
|
||||
|
Loading…
Reference in New Issue
Block a user