From 246d0de16b162f4fe7789a8990fe898279b46b8c Mon Sep 17 00:00:00 2001 From: Mike Goatly Date: Wed, 6 Jun 2018 22:11:27 +0100 Subject: [PATCH 1/3] Allow for more timespan options --- .../utils/data-formats/formats/timespan.ts | 48 ++++++++++++++----- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/client/src/utils/data-formats/formats/timespan.ts b/client/src/utils/data-formats/formats/timespan.ts index 16e6ea7..f68d4b8 100644 --- a/client/src/utils/data-formats/formats/timespan.ts +++ b/client/src/utils/data-formats/formats/timespan.ts @@ -3,6 +3,40 @@ import utils from '../../index'; import { DataFormatTypes, IDataFormat, formatWarn, getPrefix } from '../common'; import { IDataSourcePlugin } from '../../../data-sources/plugins/DataSourcePlugin'; +enum Interval { + Hours = 0, + Days = 1, + Weeks= 2, + Months = 3 +} + +interface IQueryTimespan { + queryTimespan: string; + granularity: string; +} + +const timespanRegex = /^(\d+) (hour|day|week|month)s?$/g; +function parseTimespan(timespanText: string) : IQueryTimespan { + var match = timespanRegex.exec(timespanText); + if (!match) { + // Backwards compatibility with existing functionality + return { queryTimespan: "P90D", granularity: "1d" }; + } + + switch (match[2]) { + case "hour": + return { queryTimespan: `PT${match[1]}H`, granularity: "5m" }; + case "day": + return { queryTimespan: `P${match[1]}D`, granularity: "30m" }; + case "week": + return { queryTimespan: `P${parseInt(match[1]) * 7}D`, granularity: "1d" }; + case "month": + return { queryTimespan: `P${parseInt(match[1]) * 30}D`, granularity: "1d" }; + default: + return { queryTimespan: "P90D", granularity: "1d" }; + } +} + /** * Receives a timespan data source and formats is accordingly * @@ -45,20 +79,8 @@ export function timespan( const params = plugin.getParams(); const prefix = getPrefix(format); - let queryTimespan = - state.selectedValue === '24 hours' ? 'PT24H' : - state.selectedValue === '1 week' ? 'P7D' : - state.selectedValue === '1 month' ? 'P30D' : - 'P90D'; - let granularity = - state.selectedValue === '24 hours' ? '5m' : - state.selectedValue === '1 week' ? '1d' : '1d'; - - let result = { - queryTimespan, - granularity - }; + let result = parseTimespan(state.selectedValue); const args = typeof format !== 'string' && format.args || {}; let values = params[args.data || 'values']; From fcd5826b0248155a110299e76df083d9ef2bd241 Mon Sep 17 00:00:00 2001 From: Mike Goatly Date: Wed, 6 Jun 2018 22:21:00 +0100 Subject: [PATCH 2/3] Render all hourly timespans as hours in timeline --- client/src/utils/data-formats/formats/timeline.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/utils/data-formats/formats/timeline.ts b/client/src/utils/data-formats/formats/timeline.ts index d6e1d95..ef037bd 100644 --- a/client/src/utils/data-formats/formats/timeline.ts +++ b/client/src/utils/data-formats/formats/timeline.ts @@ -97,7 +97,7 @@ export function timeline( let result = {}; result[prefix + 'graphData'] = timelineValues; - result[prefix + 'timeFormat'] = (timespan === '24 hours' ? 'hour' : 'date'); + result[prefix + 'timeFormat'] = (timespan.indexOf("hour") > 0 ? 'hour' : 'date'); result[prefix + 'lines'] = lines; result[prefix + 'pieData'] = usage; From 8834c9df3936e4628454689afc440e4791043347 Mon Sep 17 00:00:00 2001 From: Mike Goatly Date: Wed, 6 Jun 2018 22:36:00 +0100 Subject: [PATCH 3/3] Fixed broken tests --- client/src/utils/data-formats/formats/timeline.ts | 2 +- client/src/utils/data-formats/formats/timespan.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/client/src/utils/data-formats/formats/timeline.ts b/client/src/utils/data-formats/formats/timeline.ts index ef037bd..6129116 100644 --- a/client/src/utils/data-formats/formats/timeline.ts +++ b/client/src/utils/data-formats/formats/timeline.ts @@ -97,7 +97,7 @@ export function timeline( let result = {}; result[prefix + 'graphData'] = timelineValues; - result[prefix + 'timeFormat'] = (timespan.indexOf("hour") > 0 ? 'hour' : 'date'); + result[prefix + 'timeFormat'] = ((timespan || "").indexOf("hour") > 0 ? 'hour' : 'date'); result[prefix + 'lines'] = lines; result[prefix + 'pieData'] = usage; diff --git a/client/src/utils/data-formats/formats/timespan.ts b/client/src/utils/data-formats/formats/timespan.ts index f68d4b8..ba26de3 100644 --- a/client/src/utils/data-formats/formats/timespan.ts +++ b/client/src/utils/data-formats/formats/timespan.ts @@ -15,7 +15,7 @@ interface IQueryTimespan { granularity: string; } -const timespanRegex = /^(\d+) (hour|day|week|month)s?$/g; +const timespanRegex = /^(\d+) (hour|day|week|month)s?$/; function parseTimespan(timespanText: string) : IQueryTimespan { var match = timespanRegex.exec(timespanText); if (!match) {