From 83b79dd624938374c77f6a82f19a9f0d87522659 Mon Sep 17 00:00:00 2001 From: Mitsuhiro Tanda Date: Wed, 16 Aug 2017 16:40:46 +0900 Subject: [PATCH] fix test --- .../datasource/cloudwatch/datasource.js | 4 +- .../cloudwatch/specs/datasource_specs.ts | 85 ++++++++++++++----- 2 files changed, 67 insertions(+), 22 deletions(-) diff --git a/public/app/plugins/datasource/cloudwatch/datasource.js b/public/app/plugins/datasource/cloudwatch/datasource.js index c8fd9e71237..0a8791f489c 100644 --- a/public/app/plugins/datasource/cloudwatch/datasource.js +++ b/public/app/plugins/datasource/cloudwatch/datasource.js @@ -72,10 +72,10 @@ function (angular, _, moment, dateMath, kbn, templatingVariable, CloudWatchAnnot return this.performTimeSeriesQuery(request); }; - this.getPeriod = function(target, options) { + this.getPeriod = function(target, options, now) { var start = this.convertToCloudWatchTime(options.range.from, false); var end = this.convertToCloudWatchTime(options.range.to, true); - var now = Math.round(Date.now() / 1000); + now = Math.round((now || Date.now()) / 1000); var period; var range = end - start; diff --git a/public/app/plugins/datasource/cloudwatch/specs/datasource_specs.ts b/public/app/plugins/datasource/cloudwatch/specs/datasource_specs.ts index 87acb40028c..1150dd9c58b 100644 --- a/public/app/plugins/datasource/cloudwatch/specs/datasource_specs.ts +++ b/public/app/plugins/datasource/cloudwatch/specs/datasource_specs.ts @@ -320,30 +320,75 @@ describe('CloudWatchDatasource', function() { it('should caclculate the correct period', function () { var hourSec = 60 * 60; var daySec = hourSec * 24; - var start = 1483196400; + var start = 1483196400 * 1000; var testData: any[] = [ - [{ period: 60 }, { namespace: 'AWS/EC2' }, {}, start, start + 3600, (hourSec * 3), 60], - [{ period: null }, { namespace: 'AWS/EC2' }, {}, start, start + 3600, (hourSec * 3), 300], - [{ period: 60 }, { namespace: 'AWS/ELB' }, {}, start, start + 3600, (hourSec * 3), 60], - [{ period: null }, { namespace: 'AWS/ELB' }, {}, start, start + 3600, (hourSec * 3), 60], - [{ period: 1 }, { namespace: 'CustomMetricsNamespace' }, {}, start, start + 1440 - 1, (hourSec * 3 - 1), 1], - [{ period: 1 }, { namespace: 'CustomMetricsNamespace' }, {}, start, start + 3600, (hourSec * 3 - 1), 60], - [{ period: 60 }, { namespace: 'CustomMetricsNamespace' }, {}, start, start + 3600, (hourSec * 3), 60], - [{ period: null }, { namespace: 'CustomMetricsNamespace' }, {}, start, start + 3600, (hourSec * 3 - 1), 60], - [{ period: null }, { namespace: 'CustomMetricsNamespace' }, {}, start, start + 3600, (hourSec * 3), 60], - [{ period: null }, { namespace: 'CustomMetricsNamespace' }, {}, start, start + 3600, (daySec * 15), 60], - [{ period: null }, { namespace: 'CustomMetricsNamespace' }, {}, start, start + 3600, (daySec * 63), 300], - [{ period: null }, { namespace: 'CustomMetricsNamespace' }, {}, start, start + 3600, (daySec * 455), 3600] + [ + { period: 60, namespace: 'AWS/EC2' }, + { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } }, + (hourSec * 3), 60 + ], + [ + { period: null, namespace: 'AWS/EC2' }, + { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } }, + (hourSec * 3), 300 + ], + [ + { period: 60, namespace: 'AWS/ELB' }, + { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } }, + (hourSec * 3), 60 + ], + [ + { period: null, namespace: 'AWS/ELB' }, + { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } }, + (hourSec * 3), 60 + ], + [ + { period: 1, namespace: 'CustomMetricsNamespace' }, + { range: { from: new Date(start), to: new Date(start + (1440 - 1) * 1000) } }, + (hourSec * 3 - 1), 1 + ], + [ + { period: 1, namespace: 'CustomMetricsNamespace' }, + { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } }, + (hourSec * 3 - 1), 60 + ], + [ + { period: 60, namespace: 'CustomMetricsNamespace' }, + { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } }, + (hourSec * 3), 60 + ], + [ + { period: null, namespace: 'CustomMetricsNamespace' }, + { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } }, + (hourSec * 3 - 1), 60 + ], + [ + { period: null, namespace: 'CustomMetricsNamespace' }, + { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } }, + (hourSec * 3), 60 + ], + [ + { period: null, namespace: 'CustomMetricsNamespace' }, + { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } }, + (daySec * 15), 60 + ], + [ + { period: null, namespace: 'CustomMetricsNamespace' }, + { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } }, + (daySec * 63), 300 + ], + [ + { period: null, namespace: 'CustomMetricsNamespace' }, + { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } }, + (daySec * 455), 3600 + ] ]; for (let t of testData) { let target = t[0]; - let query = t[1]; - let options = t[2]; - let start = t[3]; - let end = t[4]; - let now = start + t[5]; - let expected = t[6]; - let actual = ctx.ds.getPeriod(target, query, options, start, end, now); + let options = t[1]; + let now = new Date(options.range.from.valueOf() + t[2] * 1000); + let expected = t[3]; + let actual = ctx.ds.getPeriod(target, options, now); expect(actual).to.be(expected); } });