Commit 6f1a9f4f by bergquist

Merge branch 'mtanda-cloudwatch_interval'

parents c38f6ff1 f4b5fa4d
...@@ -3,9 +3,10 @@ define([ ...@@ -3,9 +3,10 @@ define([
'lodash', 'lodash',
'moment', 'moment',
'app/core/utils/datemath', 'app/core/utils/datemath',
'app/core/utils/kbn',
'./annotation_query', './annotation_query',
], ],
function (angular, _, moment, dateMath, CloudWatchAnnotationQuery) { function (angular, _, moment, dateMath, kbn, CloudWatchAnnotationQuery) {
'use strict'; 'use strict';
/** @ngInject */ /** @ngInject */
...@@ -36,12 +37,9 @@ function (angular, _, moment, dateMath, CloudWatchAnnotationQuery) { ...@@ -36,12 +37,9 @@ function (angular, _, moment, dateMath, CloudWatchAnnotationQuery) {
query.dimensions = self.convertDimensionFormat(target.dimensions, options.scopedVars); query.dimensions = self.convertDimensionFormat(target.dimensions, options.scopedVars);
query.statistics = target.statistics; query.statistics = target.statistics;
var range = end - start; var period = this._getPeriod(target, query, options, start, end);
query.period = parseInt(target.period, 10) || (query.namespace === 'AWS/EC2' ? 300 : 60); target.period = period;
if (range / query.period >= 1440) { query.period = period;
query.period = Math.ceil(range / 1440 / 60) * 60;
}
target.period = query.period;
queries.push(query); queries.push(query);
}.bind(this)); }.bind(this));
...@@ -69,6 +67,27 @@ function (angular, _, moment, dateMath, CloudWatchAnnotationQuery) { ...@@ -69,6 +67,27 @@ function (angular, _, moment, dateMath, CloudWatchAnnotationQuery) {
}); });
}; };
this._getPeriod = function(target, query, options, start, end) {
var period;
var range = end - start;
if (!target.period) {
period = (query.namespace === 'AWS/EC2') ? 300 : 60;
} else if (/^\d+$/.test(target.period)) {
period = parseInt(target.period, 10);
} else {
period = kbn.interval_to_seconds(templateSrv.replace(target.period, options.scopedVars));
}
if (query.period < 60) {
period = 60;
}
if (range / query.period >= 1440) {
period = Math.ceil(range / 1440 / 60) * 60;
}
return period;
};
this.performTimeSeriesQuery = function(query, start, end) { this.performTimeSeriesQuery = function(query, start, end) {
return this.awsRequest({ return this.awsRequest({
region: query.region, region: query.region,
......
...@@ -82,6 +82,35 @@ describe('CloudWatchDatasource', function() { ...@@ -82,6 +82,35 @@ describe('CloudWatchDatasource', function() {
ctx.$rootScope.$apply(); ctx.$rootScope.$apply();
}); });
it('should generate the correct query with interval variable', function(done) {
ctx.templateSrv.data = {
period: '10m'
};
var query = {
range: { from: 'now-1h', to: 'now' },
targets: [
{
region: 'us-east-1',
namespace: 'AWS/EC2',
metricName: 'CPUUtilization',
dimensions: {
InstanceId: 'i-12345678'
},
statistics: ['Average'],
period: '[[period]]'
}
]
};
ctx.ds.query(query).then(function() {
var params = requestParams.data.parameters;
expect(params.period).to.be(600);
done();
});
ctx.$rootScope.$apply();
});
it('should return series list', function(done) { it('should return series list', function(done) {
ctx.ds.query(query).then(function(result) { ctx.ds.query(query).then(function(result) {
expect(result.data[0].target).to.be('CPUUtilization_Average'); expect(result.data[0].target).to.be('CPUUtilization_Average');
......
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