Skip to content

Commit 6b70157

Browse files
committed
Merge branch 'cloudwatch_interval' of https://github.com/mtanda/grafana into mtanda-cloudwatch_interval
2 parents 36f0bf0 + ab9abee commit 6b70157

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

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

Lines changed: 12 additions & 2 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 */
@@ -37,7 +38,16 @@ function (angular, _, moment, dateMath, CloudWatchAnnotationQuery) {
3738
query.statistics = target.statistics;
3839

3940
var range = end - start;
40-
query.period = parseInt(target.period, 10) || (query.namespace === 'AWS/EC2' ? 300 : 60);
41+
if (!target.period) {
42+
query.period = (query.namespace === 'AWS/EC2') ? 300 : 60;
43+
} else if (/^\d+$/.test(target.period)) {
44+
query.period = parseInt(target.period, 10);
45+
} else {
46+
query.period = kbn.interval_to_seconds(templateSrv.replace(target.period, options.scopedVars));
47+
}
48+
if (query.period < 60) {
49+
query.period = 60;
50+
}
4151
if (range / query.period >= 1440) {
4252
query.period = Math.ceil(range / 1440 / 60) * 60;
4353
}

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)