Skip to content

Commit 6f1a9f4

Browse files
committed
Merge branch 'mtanda-cloudwatch_interval'
2 parents c38f6ff + f4b5fa4 commit 6f1a9f4

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

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

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ define([
33
'lodash',
44
'moment',
55
'app/core/utils/datemath',
6+
'app/core/utils/kbn',
67
'./annotation_query',
78
],
8-
function (angular, _, moment, dateMath, CloudWatchAnnotationQuery) {
9+
function (angular, _, moment, dateMath, kbn, CloudWatchAnnotationQuery) {
910
'use strict';
1011

1112
/** @ngInject */
@@ -36,12 +37,9 @@ function (angular, _, moment, dateMath, CloudWatchAnnotationQuery) {
3637
query.dimensions = self.convertDimensionFormat(target.dimensions, options.scopedVars);
3738
query.statistics = target.statistics;
3839

39-
var range = end - start;
40-
query.period = parseInt(target.period, 10) || (query.namespace === 'AWS/EC2' ? 300 : 60);
41-
if (range / query.period >= 1440) {
42-
query.period = Math.ceil(range / 1440 / 60) * 60;
43-
}
44-
target.period = query.period;
40+
var period = this._getPeriod(target, query, options, start, end);
41+
target.period = period;
42+
query.period = period;
4543

4644
queries.push(query);
4745
}.bind(this));
@@ -69,6 +67,27 @@ function (angular, _, moment, dateMath, CloudWatchAnnotationQuery) {
6967
});
7068
};
7169

70+
this._getPeriod = function(target, query, options, start, end) {
71+
var period;
72+
var range = end - start;
73+
74+
if (!target.period) {
75+
period = (query.namespace === 'AWS/EC2') ? 300 : 60;
76+
} else if (/^\d+$/.test(target.period)) {
77+
period = parseInt(target.period, 10);
78+
} else {
79+
period = kbn.interval_to_seconds(templateSrv.replace(target.period, options.scopedVars));
80+
}
81+
if (query.period < 60) {
82+
period = 60;
83+
}
84+
if (range / query.period >= 1440) {
85+
period = Math.ceil(range / 1440 / 60) * 60;
86+
}
87+
88+
return period;
89+
};
90+
7291
this.performTimeSeriesQuery = function(query, start, end) {
7392
return this.awsRequest({
7493
region: query.region,

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,35 @@ describe('CloudWatchDatasource', function() {
8282
ctx.$rootScope.$apply();
8383
});
8484

85+
it('should generate the correct query with interval variable', function(done) {
86+
ctx.templateSrv.data = {
87+
period: '10m'
88+
};
89+
90+
var query = {
91+
range: { from: 'now-1h', to: 'now' },
92+
targets: [
93+
{
94+
region: 'us-east-1',
95+
namespace: 'AWS/EC2',
96+
metricName: 'CPUUtilization',
97+
dimensions: {
98+
InstanceId: 'i-12345678'
99+
},
100+
statistics: ['Average'],
101+
period: '[[period]]'
102+
}
103+
]
104+
};
105+
106+
ctx.ds.query(query).then(function() {
107+
var params = requestParams.data.parameters;
108+
expect(params.period).to.be(600);
109+
done();
110+
});
111+
ctx.$rootScope.$apply();
112+
});
113+
85114
it('should return series list', function(done) {
86115
ctx.ds.query(query).then(function(result) {
87116
expect(result.data[0].target).to.be('CPUUtilization_Average');

0 commit comments

Comments
 (0)