CloudWatch: Make sure period variable is being interpreted correctly (#20447)

* Make sure variable is interpreted before parsing int

* Use correct datatype in tests

(cherry picked from commit 38d5abfadb)
This commit is contained in:
Erik Sundell
2019-11-25 12:01:36 +01:00
committed by Marcus Efraimsson
parent 239d491a0b
commit ee896b8341
2 changed files with 15 additions and 14 deletions
@@ -158,10 +158,11 @@ export default class CloudWatchDatasource extends DataSourceApi<CloudWatchQuery,
periodUnit = period = 60 * 60; periodUnit = period = 60 * 60;
} }
} else { } else {
if (/^\d+$/.test(target.period)) { period = this.templateSrv.replace(target.period, options.scopedVars);
period = parseInt(target.period, 10); if (/^\d+$/.test(period)) {
period = parseInt(period, 10);
} else { } else {
period = kbn.interval_to_seconds(this.templateSrv.replace(target.period, options.scopedVars)); period = kbn.interval_to_seconds(period);
} }
} }
if (period < 1) { if (period < 1) {
@@ -340,7 +340,7 @@ describe('CloudWatchDatasource', () => {
InstanceId: 'i-12345678', InstanceId: 'i-12345678',
}, },
statistics: ['Average'], statistics: ['Average'],
period: 300, period: '300s',
}, },
], ],
}; };
@@ -367,7 +367,7 @@ describe('CloudWatchDatasource', () => {
TargetGroup: 'tg', TargetGroup: 'tg',
}, },
statistics: ['p90.00'], statistics: ['p90.00'],
period: 300, period: '300s',
}, },
], ],
}; };
@@ -484,7 +484,7 @@ describe('CloudWatchDatasource', () => {
dim2: '[[var2]]', dim2: '[[var2]]',
}, },
statistics: ['Average'], statistics: ['Average'],
period: 300, period: '300s',
}, },
], ],
}; };
@@ -511,7 +511,7 @@ describe('CloudWatchDatasource', () => {
dim3: '[[var3]]', dim3: '[[var3]]',
}, },
statistics: ['Average'], statistics: ['Average'],
period: 300, period: '300s',
}, },
], ],
scopedVars: { scopedVars: {
@@ -544,7 +544,7 @@ describe('CloudWatchDatasource', () => {
dim4: '[[var4]]', dim4: '[[var4]]',
}, },
statistics: ['Average'], statistics: ['Average'],
period: 300, period: '300s',
}, },
], ],
}; };
@@ -573,7 +573,7 @@ describe('CloudWatchDatasource', () => {
dim3: '[[var3]]', dim3: '[[var3]]',
}, },
statistics: ['Average'], statistics: ['Average'],
period: 300, period: '300',
}, },
], ],
scopedVars: { scopedVars: {
@@ -755,7 +755,7 @@ describe('CloudWatchDatasource', () => {
const start = 1483196400 * 1000; const start = 1483196400 * 1000;
const testData: any[] = [ const testData: any[] = [
[ [
{ period: 60, namespace: 'AWS/EC2' }, { period: '60s', namespace: 'AWS/EC2' },
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } }, { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
hourSec * 3, hourSec * 3,
60, 60,
@@ -767,7 +767,7 @@ describe('CloudWatchDatasource', () => {
300, 300,
], ],
[ [
{ period: 60, namespace: 'AWS/ELB' }, { period: '60s', namespace: 'AWS/ELB' },
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } }, { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
hourSec * 3, hourSec * 3,
60, 60,
@@ -779,7 +779,7 @@ describe('CloudWatchDatasource', () => {
60, 60,
], ],
[ [
{ period: 1, namespace: 'CustomMetricsNamespace' }, { period: '1', namespace: 'CustomMetricsNamespace' },
{ {
range: { range: {
from: new Date(start), from: new Date(start),
@@ -790,13 +790,13 @@ describe('CloudWatchDatasource', () => {
1, 1,
], ],
[ [
{ period: 1, namespace: 'CustomMetricsNamespace' }, { period: '1', namespace: 'CustomMetricsNamespace' },
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } }, { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
hourSec * 3 - 1, hourSec * 3 - 1,
60, 60,
], ],
[ [
{ period: 60, namespace: 'CustomMetricsNamespace' }, { period: '60s', namespace: 'CustomMetricsNamespace' },
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } }, { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
hourSec * 3, hourSec * 3,
60, 60,