Просмотр исходного кода

Merge branch 'cloudwatch_interval' of https://github.com/mtanda/grafana into mtanda-cloudwatch_interval

bergquist 9 лет назад
Родитель
Сommit
6b7015799c

+ 12 - 2
public/app/plugins/datasource/cloudwatch/datasource.js

@@ -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 */
@@ -37,7 +38,16 @@ function (angular, _, moment, dateMath, CloudWatchAnnotationQuery) {
         query.statistics = target.statistics;
         query.statistics = target.statistics;
 
 
         var range = end - start;
         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) {
         if (range / query.period >= 1440) {
           query.period = Math.ceil(range / 1440 / 60) * 60;
           query.period = Math.ceil(range / 1440 / 60) * 60;
         }
         }

+ 29 - 0
public/app/plugins/datasource/cloudwatch/specs/datasource_specs.ts

@@ -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');