This commit is contained in:
Mitsuhiro Tanda 2017-08-16 16:40:46 +09:00
parent 728e96e134
commit 83b79dd624
2 changed files with 67 additions and 22 deletions

View File

@ -72,10 +72,10 @@ function (angular, _, moment, dateMath, kbn, templatingVariable, CloudWatchAnnot
return this.performTimeSeriesQuery(request); return this.performTimeSeriesQuery(request);
}; };
this.getPeriod = function(target, options) { this.getPeriod = function(target, options, now) {
var start = this.convertToCloudWatchTime(options.range.from, false); var start = this.convertToCloudWatchTime(options.range.from, false);
var end = this.convertToCloudWatchTime(options.range.to, true); 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 period;
var range = end - start; var range = end - start;

View File

@ -320,30 +320,75 @@ describe('CloudWatchDatasource', function() {
it('should caclculate the correct period', function () { it('should caclculate the correct period', function () {
var hourSec = 60 * 60; var hourSec = 60 * 60;
var daySec = hourSec * 24; var daySec = hourSec * 24;
var start = 1483196400; var start = 1483196400 * 1000;
var testData: any[] = [ 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/EC2' },
[{ period: 60 }, { namespace: 'AWS/ELB' }, {}, start, start + 3600, (hourSec * 3), 60], { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
[{ period: null }, { namespace: 'AWS/ELB' }, {}, start, start + 3600, (hourSec * 3), 60], (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: 'AWS/EC2' },
[{ period: null }, { namespace: 'CustomMetricsNamespace' }, {}, start, start + 3600, (hourSec * 3 - 1), 60], { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
[{ period: null }, { namespace: 'CustomMetricsNamespace' }, {}, start, start + 3600, (hourSec * 3), 60], (hourSec * 3), 300
[{ 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/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) { for (let t of testData) {
let target = t[0]; let target = t[0];
let query = t[1]; let options = t[1];
let options = t[2]; let now = new Date(options.range.from.valueOf() + t[2] * 1000);
let start = t[3]; let expected = t[3];
let end = t[4]; let actual = ctx.ds.getPeriod(target, options, now);
let now = start + t[5];
let expected = t[6];
let actual = ctx.ds.getPeriod(target, query, options, start, end, now);
expect(actual).to.be(expected); expect(actual).to.be(expected);
} }
}); });