Skip to content

Commit 21fcb2c

Browse files
committed
Merge branch 'mtanda-cloudwatch_expand_template'
2 parents 87be56a + 46866ad commit 21fcb2c

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* **Graphite**: Add support for groupByNode, closes [#5613](https://github.com/grafana/grafana/pull/5613)
1313
* **Influxdb**: Add support for elapsed(), closes [#5827](https://github.com/grafana/grafana/pull/5827)
1414
* **OAuth**: Add support for generic oauth, closes [#4718](https://github.com/grafana/grafana/pull/4718)
15+
* **Cloudwatch**: Add support to expand multi select template variable, closes [#5003](https://github.com/grafana/grafana/pull/5003)
1516

1617
### Breaking changes
1718
* **SystemD**: Change systemd description, closes [#5971](https://github.com/grafana/grafana/pull/5971)

docs/sources/installation/configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ session provider you have configured.
428428
- **mysql:** go-sql-driver/mysql dsn config string, e.g. `user:password@tcp(127.0.0.1:3306)/database_name`
429429
- **postgres:** ex: user=a password=b host=localhost port=5432 dbname=c sslmode=disable
430430
- **memcache:** ex: 127.0.0.1:11211
431-
- **redis:** ex: `addr=127.0.0.1:6379,pool_size=100,db=grafana`
431+
- **redis:** ex: `addr=127.0.0.1:6379,pool_size=100,prefix=grafana`
432432

433433
If you use MySQL or Postgres as the session store you need to create the
434434
session table manually.

public/app/plugins/datasource/cloudwatch/datasource.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function (angular, _, moment, dateMath, CloudWatchAnnotationQuery) {
2323

2424
var queries = [];
2525
options = angular.copy(options);
26+
options.targets = this.expandTemplateVariable(options.targets, templateSrv);
2627
_.each(options.targets, function(target) {
2728
if (target.hide || !target.namespace || !target.metricName || _.isEmpty(target.statistics)) {
2829
return;
@@ -337,6 +338,37 @@ function (angular, _, moment, dateMath, CloudWatchAnnotationQuery) {
337338
});
338339
}
339340

341+
this.getExpandedVariables = function(target, dimensionKey, variable) {
342+
return _.chain(variable.options)
343+
.filter(function(v) {
344+
return v.selected;
345+
})
346+
.map(function(v) {
347+
var t = angular.copy(target);
348+
t.dimensions[dimensionKey] = v.value;
349+
return t;
350+
}).value();
351+
};
352+
353+
this.expandTemplateVariable = function(targets, templateSrv) {
354+
var self = this;
355+
return _.chain(targets)
356+
.map(function(target) {
357+
var dimensionKey = _.findKey(target.dimensions, function(v) {
358+
return templateSrv.variableExists(v);
359+
});
360+
361+
if (dimensionKey) {
362+
var variable = _.find(templateSrv.variables, function(variable) {
363+
return templateSrv.containsVariable(target.dimensions[dimensionKey], variable.name);
364+
});
365+
return self.getExpandedVariables(target, dimensionKey, variable);
366+
} else {
367+
return [target];
368+
}
369+
}).flatten().value();
370+
};
371+
340372
this.convertToCloudWatchTime = function(date, roundUp) {
341373
if (_.isString(date)) {
342374
date = dateMath.parse(date, roundUp);

public/app/plugins/datasource/cloudwatch/specs/datasource_specs.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,38 @@ describe('CloudWatchDatasource', function() {
9898
});
9999
ctx.$rootScope.$apply();
100100
});
101+
102+
it('should generate the correct targets by expanding template variables', function() {
103+
var templateSrv = {
104+
variables: [
105+
{
106+
name: 'instance_id',
107+
options: [
108+
{ value: 'i-23456789', selected: false },
109+
{ value: 'i-34567890', selected: true }
110+
]
111+
}
112+
],
113+
variableExists: function (e) { return true; },
114+
containsVariable: function (str, variableName) { return str.indexOf('$' + variableName) !== -1; }
115+
};
116+
117+
var targets = [
118+
{
119+
region: 'us-east-1',
120+
namespace: 'AWS/EC2',
121+
metricName: 'CPUUtilization',
122+
dimensions: {
123+
InstanceId: '$instance_id'
124+
},
125+
statistics: ['Average'],
126+
period: 300
127+
}
128+
];
129+
130+
var result = ctx.ds.expandTemplateVariable(targets, templateSrv);
131+
expect(result[0].dimensions.InstanceId).to.be('i-34567890');
132+
});
101133
});
102134

103135
function describeMetricFindQuery(query, func) {

0 commit comments

Comments
 (0)