Commit e65cf5cb by Mitsuhiro Tanda

fix directly specified variable rendering

parent 238139fa
...@@ -374,23 +374,33 @@ export default class CloudWatchDatasource { ...@@ -374,23 +374,33 @@ export default class CloudWatchDatasource {
getExpandedVariables(target, dimensionKey, variable, templateSrv) { getExpandedVariables(target, dimensionKey, variable, templateSrv) {
/* if the all checkbox is marked we should add all values to the targets */ /* if the all checkbox is marked we should add all values to the targets */
var allSelected = _.find(variable.options, { selected: true, text: 'All' }); var allSelected = _.find(variable.options, { selected: true, text: 'All' });
return _.chain(variable.options) var selectedVariables = _.filter(variable.options, v => {
.filter(v => { if (allSelected) {
if (allSelected) { return v.text !== 'All';
return v.text !== 'All'; } else {
} else { return v.selected;
return v.selected; }
} });
}) var currentVariables = !_.isArray(variable.current.value)
.map(v => { ? [variable.current]
var t = angular.copy(target); : variable.current.value.map(v => {
var scopedVar = {}; return {
scopedVar[variable.name] = v; text: v,
t.refId = target.refId + '_' + v.value; value: v,
t.dimensions[dimensionKey] = templateSrv.replace(t.dimensions[dimensionKey], scopedVar); };
return t; });
}) let useSelectedVariables =
.value(); selectedVariables.some(s => {
return s.value === currentVariables[0].value;
}) || currentVariables[0].value === '$__all';
return (useSelectedVariables ? selectedVariables : currentVariables).map(v => {
var t = angular.copy(target);
var scopedVar = {};
scopedVar[variable.name] = v;
t.refId = target.refId + '_' + v.value;
t.dimensions[dimensionKey] = templateSrv.replace(t.dimensions[dimensionKey], scopedVar);
return t;
});
} }
expandTemplateVariable(targets, scopedVars, templateSrv) { expandTemplateVariable(targets, scopedVars, templateSrv) {
......
...@@ -133,6 +133,10 @@ describe('CloudWatchDatasource', function() { ...@@ -133,6 +133,10 @@ describe('CloudWatchDatasource', function() {
{ text: 'i-23456789', value: 'i-23456789', selected: false }, { text: 'i-23456789', value: 'i-23456789', selected: false },
{ text: 'i-34567890', value: 'i-34567890', selected: true }, { text: 'i-34567890', value: 'i-34567890', selected: true },
], ],
current: {
text: 'i-34567890',
value: 'i-34567890',
},
}, },
], ],
replace: function(target, scopedVars) { replace: function(target, scopedVars) {
...@@ -169,6 +173,53 @@ describe('CloudWatchDatasource', function() { ...@@ -169,6 +173,53 @@ 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'); expect(result[0].dimensions.InstanceId).to.be('i-34567890');
}); });
it('should generate the correct targets by expanding template variables from url', function() {
var templateSrv = {
variables: [
{
name: 'instance_id',
options: [
{ text: 'i-23456789', value: 'i-23456789', selected: false },
{ text: 'i-34567890', value: 'i-34567890', selected: false },
],
current: 'i-45678901',
},
],
replace: function(target, scopedVars) {
if (target === '$instance_id') {
return 'i-45678901';
} else {
return '';
}
},
getVariableName: function(e) {
return 'instance_id';
},
variableExists: function(e) {
return true;
},
containsVariable: function(str, variableName) {
return str.indexOf('$' + variableName) !== -1;
},
};
var targets = [
{
region: 'us-east-1',
namespace: 'AWS/EC2',
metricName: 'CPUUtilization',
dimensions: {
InstanceId: '$instance_id',
},
statistics: ['Average'],
period: 300,
},
];
var result = ctx.ds.expandTemplateVariable(targets, {}, templateSrv);
expect(result[0].dimensions.InstanceId).to.be('i-45678901');
});
}); });
describe('When query region is "default"', function() { describe('When query region is "default"', function() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment