From 2e5a1328a84d627cc81573f59f3a8ffa384fce55 Mon Sep 17 00:00:00 2001 From: Mitsuhiro Tanda Date: Fri, 4 Mar 2016 11:36:30 +0900 Subject: [PATCH 1/3] (cloudwatch) support interval template variable --- .../plugins/datasource/cloudwatch/datasource.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/public/app/plugins/datasource/cloudwatch/datasource.js b/public/app/plugins/datasource/cloudwatch/datasource.js index a034134e657..34522344931 100644 --- a/public/app/plugins/datasource/cloudwatch/datasource.js +++ b/public/app/plugins/datasource/cloudwatch/datasource.js @@ -3,9 +3,10 @@ define([ 'lodash', 'moment', 'app/core/utils/datemath', + 'app/core/utils/kbn', './annotation_query', ], -function (angular, _, moment, dateMath, CloudWatchAnnotationQuery) { +function (angular, _, moment, dateMath, kbn, CloudWatchAnnotationQuery) { 'use strict'; /** @ngInject */ @@ -36,7 +37,16 @@ function (angular, _, moment, dateMath, CloudWatchAnnotationQuery) { query.statistics = target.statistics; var range = end - start; - query.period = parseInt(target.period, 10) || (query.namespace === 'AWS/EC2' ? 300 : 60); + if (!target.period) { + query.period = (query.namespace === 'AWS/EC2') ? 300 : 60; + } else if (/^\d+$/.test(target.period)) { + query.period = parseInt(target.period, 10); + } else { + query.period = kbn.interval_to_seconds(templateSrv.replace(target.period, options.scopedVars)); + } + if (query.period < 60) { + query.period = 60; + } if (range / query.period >= 1440) { query.period = Math.ceil(range / 1440 / 60) * 60; } From ab9abee67b6292e0ecb135346aaa2e563f1c92ce Mon Sep 17 00:00:00 2001 From: Mitsuhiro Tanda Date: Tue, 19 Apr 2016 23:14:41 +0900 Subject: [PATCH 2/3] (cloudwatch) add test for interval variable --- .../cloudwatch/specs/datasource_specs.ts | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/public/app/plugins/datasource/cloudwatch/specs/datasource_specs.ts b/public/app/plugins/datasource/cloudwatch/specs/datasource_specs.ts index 86e085b3f6f..cd13c4502d7 100644 --- a/public/app/plugins/datasource/cloudwatch/specs/datasource_specs.ts +++ b/public/app/plugins/datasource/cloudwatch/specs/datasource_specs.ts @@ -82,6 +82,35 @@ describe('CloudWatchDatasource', function() { 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) { ctx.ds.query(query).then(function(result) { expect(result.data[0].target).to.be('CPUUtilization_Average'); From 96ca69c5a9f22d65f756c62a7cbf5e730215b5b3 Mon Sep 17 00:00:00 2001 From: bergquist Date: Mon, 3 Oct 2016 10:31:07 +0200 Subject: [PATCH 3/3] tech(cloudwatch): extract method --- .../datasource/cloudwatch/datasource.js | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/public/app/plugins/datasource/cloudwatch/datasource.js b/public/app/plugins/datasource/cloudwatch/datasource.js index ddb0a7da254..c4f3a82f98e 100644 --- a/public/app/plugins/datasource/cloudwatch/datasource.js +++ b/public/app/plugins/datasource/cloudwatch/datasource.js @@ -37,21 +37,9 @@ function (angular, _, moment, dateMath, kbn, CloudWatchAnnotationQuery) { query.dimensions = self.convertDimensionFormat(target.dimensions, options.scopedVars); query.statistics = target.statistics; - var range = end - start; - if (!target.period) { - query.period = (query.namespace === 'AWS/EC2') ? 300 : 60; - } else if (/^\d+$/.test(target.period)) { - query.period = parseInt(target.period, 10); - } else { - query.period = kbn.interval_to_seconds(templateSrv.replace(target.period, options.scopedVars)); - } - if (query.period < 60) { - query.period = 60; - } - if (range / query.period >= 1440) { - query.period = Math.ceil(range / 1440 / 60) * 60; - } - target.period = query.period; + var period = this._getPeriod(target, query, options, start, end); + target.period = period; + query.period = period; queries.push(query); }.bind(this)); @@ -79,6 +67,27 @@ function (angular, _, moment, dateMath, kbn, 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) { return this.awsRequest({ region: query.region,