From 3f6e5af624f6b81510383e80ea9b2286c802147c Mon Sep 17 00:00:00 2001 From: oscarkilhed Date: Thu, 28 Nov 2024 12:07:34 +0100 Subject: [PATCH 1/8] Supply fixed now to datemath caluclations --- packages/scenes/src/utils/evaluateTimeRange.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/scenes/src/utils/evaluateTimeRange.ts b/packages/scenes/src/utils/evaluateTimeRange.ts index 2984ebaac..2c2f1dffc 100644 --- a/packages/scenes/src/utils/evaluateTimeRange.ts +++ b/packages/scenes/src/utils/evaluateTimeRange.ts @@ -9,10 +9,10 @@ export function evaluateTimeRange( delay?: string ): TimeRange { const hasDelay = delay && to === 'now'; - + const now = Date.now(); return { - from: dateMath.parse(from, false, timeZone, fiscalYearStartMonth)!, - to: dateMath.parse(hasDelay ? 'now-' + delay : to, true, timeZone, fiscalYearStartMonth)!, + from: dateMath.parse(from, false, timeZone, fiscalYearStartMonth, now)!, + to: dateMath.parse(hasDelay ? 'now-' + delay : to, true, timeZone, fiscalYearStartMonth, now)!, raw: { from: from, to: to, From e4f17c5f8a668c642f723b39d3da696dcd43b6e8 Mon Sep 17 00:00:00 2001 From: oscarkilhed Date: Mon, 2 Dec 2024 15:34:27 +0100 Subject: [PATCH 2/8] Use new toDateTime function instead --- packages/scenes/src/utils/evaluateTimeRange.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/scenes/src/utils/evaluateTimeRange.ts b/packages/scenes/src/utils/evaluateTimeRange.ts index 2c2f1dffc..793a85276 100644 --- a/packages/scenes/src/utils/evaluateTimeRange.ts +++ b/packages/scenes/src/utils/evaluateTimeRange.ts @@ -11,8 +11,18 @@ export function evaluateTimeRange( const hasDelay = delay && to === 'now'; const now = Date.now(); return { - from: dateMath.parse(from, false, timeZone, fiscalYearStartMonth, now)!, - to: dateMath.parse(hasDelay ? 'now-' + delay : to, true, timeZone, fiscalYearStartMonth, now)!, + from: dateMath.toDateTime(from, { + roundUp: false, + timezone: timeZone, + fiscalYearStartMonth: fiscalYearStartMonth, + now: now, + })!, + to: dateMath.toDateTime(hasDelay ? 'now-' + delay : to, { + roundUp: true, + timezone: timeZone, + fiscalYearStartMonth: fiscalYearStartMonth, + now: now, + })!, raw: { from: from, to: to, From bd28bb6aab7a722472425285ad4714a8137576b2 Mon Sep 17 00:00:00 2001 From: oscarkilhed Date: Thu, 12 Dec 2024 15:55:20 +0100 Subject: [PATCH 3/8] Use dateMath.parse if dateMath.toDateTime isn't present. Also switch the calculation of to and from to help issues --- .../scenes/src/utils/evaluateTimeRange.ts | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/packages/scenes/src/utils/evaluateTimeRange.ts b/packages/scenes/src/utils/evaluateTimeRange.ts index 793a85276..98bf83670 100644 --- a/packages/scenes/src/utils/evaluateTimeRange.ts +++ b/packages/scenes/src/utils/evaluateTimeRange.ts @@ -1,4 +1,4 @@ -import { dateMath, DateTime, TimeRange } from '@grafana/data'; +import { dateMath, DateTime, DateTimeInput, TimeRange } from '@grafana/data'; import { TimeZone } from '@grafana/schema'; export function evaluateTimeRange( @@ -10,15 +10,36 @@ export function evaluateTimeRange( ): TimeRange { const hasDelay = delay && to === 'now'; const now = Date.now(); + + /** This tries to use dateMath.toDateTime if available, otherwise falls back to dateMath.parse. + * Using dateMath.parse can potentially result in now being calculated using two different timestamps. + * If two different timestamps are used, the time range "now-24h to now" will potentially be 24h +- number of milliseconds it takes between calculations. + */ + const parseOrToDateTime = ( + val: string | DateTime, + options: { roundUp: boolean; timezone: TimeZone; fiscalYearStartMonth?: number; now?: DateTimeInput } + ) => { + if (dateMath.toDateTime) { + return dateMath.toDateTime(val, options); + } else { + dateMath.parse(val, options.roundUp, options.timezone, options.fiscalYearStartMonth); + } + }; + + + /** The order of calculating to and from is important. This is because if we're using the old dateMath.parse we could potentially get two different timestamps. + * If we calculate to first, then from. The timerange "now-24h to now" will err on the side of being shorter than 24h. This will aleviate some of the issues arising + * the timerange indeterminently alternating between less than or equal to 24h and being greater than 24h. + */ return { - from: dateMath.toDateTime(from, { - roundUp: false, + to: parseOrToDateTime(hasDelay ? 'now-' + delay : to, { + roundUp: true, timezone: timeZone, fiscalYearStartMonth: fiscalYearStartMonth, now: now, })!, - to: dateMath.toDateTime(hasDelay ? 'now-' + delay : to, { - roundUp: true, + from: parseOrToDateTime(from, { + roundUp: false, timezone: timeZone, fiscalYearStartMonth: fiscalYearStartMonth, now: now, From 8eefbdcd65df4b817946b56ab130b162fc5f3aa4 Mon Sep 17 00:00:00 2001 From: oscarkilhed Date: Thu, 12 Dec 2024 16:19:53 +0100 Subject: [PATCH 4/8] Prettier --- packages/scenes/src/utils/evaluateTimeRange.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/scenes/src/utils/evaluateTimeRange.ts b/packages/scenes/src/utils/evaluateTimeRange.ts index 98bf83670..5f26223ac 100644 --- a/packages/scenes/src/utils/evaluateTimeRange.ts +++ b/packages/scenes/src/utils/evaluateTimeRange.ts @@ -26,11 +26,10 @@ export function evaluateTimeRange( } }; - /** The order of calculating to and from is important. This is because if we're using the old dateMath.parse we could potentially get two different timestamps. * If we calculate to first, then from. The timerange "now-24h to now" will err on the side of being shorter than 24h. This will aleviate some of the issues arising * the timerange indeterminently alternating between less than or equal to 24h and being greater than 24h. - */ + */ return { to: parseOrToDateTime(hasDelay ? 'now-' + delay : to, { roundUp: true, From c21c130c6ab0a5d2d531e9422370b2eaa6df33b3 Mon Sep 17 00:00:00 2001 From: oscarkilhed Date: Thu, 12 Dec 2024 16:21:55 +0100 Subject: [PATCH 5/8] Fix comments --- packages/scenes/src/utils/evaluateTimeRange.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/scenes/src/utils/evaluateTimeRange.ts b/packages/scenes/src/utils/evaluateTimeRange.ts index 5f26223ac..6c2d1c88d 100644 --- a/packages/scenes/src/utils/evaluateTimeRange.ts +++ b/packages/scenes/src/utils/evaluateTimeRange.ts @@ -12,7 +12,7 @@ export function evaluateTimeRange( const now = Date.now(); /** This tries to use dateMath.toDateTime if available, otherwise falls back to dateMath.parse. - * Using dateMath.parse can potentially result in now being calculated using two different timestamps. + * Using dateMath.parse can potentially result in to and from being calculated using two different timestamps. * If two different timestamps are used, the time range "now-24h to now" will potentially be 24h +- number of milliseconds it takes between calculations. */ const parseOrToDateTime = ( @@ -28,7 +28,7 @@ export function evaluateTimeRange( /** The order of calculating to and from is important. This is because if we're using the old dateMath.parse we could potentially get two different timestamps. * If we calculate to first, then from. The timerange "now-24h to now" will err on the side of being shorter than 24h. This will aleviate some of the issues arising - * the timerange indeterminently alternating between less than or equal to 24h and being greater than 24h. + * from the timerange indeterminently alternating between less than or equal to 24h and being greater than 24h. */ return { to: parseOrToDateTime(hasDelay ? 'now-' + delay : to, { From 4d644b6342b9a2a09503030ef9b93bdef48886cc Mon Sep 17 00:00:00 2001 From: oscarkilhed Date: Thu, 12 Dec 2024 16:38:52 +0100 Subject: [PATCH 6/8] make sure to return the parsed datetime... --- packages/scenes/src/utils/evaluateTimeRange.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/scenes/src/utils/evaluateTimeRange.ts b/packages/scenes/src/utils/evaluateTimeRange.ts index 6c2d1c88d..24d053b2b 100644 --- a/packages/scenes/src/utils/evaluateTimeRange.ts +++ b/packages/scenes/src/utils/evaluateTimeRange.ts @@ -22,7 +22,7 @@ export function evaluateTimeRange( if (dateMath.toDateTime) { return dateMath.toDateTime(val, options); } else { - dateMath.parse(val, options.roundUp, options.timezone, options.fiscalYearStartMonth); + return dateMath.parse(val, options.roundUp, options.timezone, options.fiscalYearStartMonth); } }; From 6ff749e6b98c06513e577ded4316d81072411298 Mon Sep 17 00:00:00 2001 From: oscarkilhed Date: Thu, 12 Dec 2024 17:34:31 +0100 Subject: [PATCH 7/8] Ignore function that might be missing --- packages/scenes/src/utils/evaluateTimeRange.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/scenes/src/utils/evaluateTimeRange.ts b/packages/scenes/src/utils/evaluateTimeRange.ts index 24d053b2b..ff6ef8f1d 100644 --- a/packages/scenes/src/utils/evaluateTimeRange.ts +++ b/packages/scenes/src/utils/evaluateTimeRange.ts @@ -20,6 +20,7 @@ export function evaluateTimeRange( options: { roundUp: boolean; timezone: TimeZone; fiscalYearStartMonth?: number; now?: DateTimeInput } ) => { if (dateMath.toDateTime) { + // @ts-ignore return dateMath.toDateTime(val, options); } else { return dateMath.parse(val, options.roundUp, options.timezone, options.fiscalYearStartMonth); From eae451b152a8751207ae55e9481dd909586fcf04 Mon Sep 17 00:00:00 2001 From: oscarkilhed Date: Thu, 12 Dec 2024 23:09:57 +0100 Subject: [PATCH 8/8] Also ignore the other use of toDateTime --- packages/scenes/src/utils/evaluateTimeRange.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/scenes/src/utils/evaluateTimeRange.ts b/packages/scenes/src/utils/evaluateTimeRange.ts index ff6ef8f1d..a95376704 100644 --- a/packages/scenes/src/utils/evaluateTimeRange.ts +++ b/packages/scenes/src/utils/evaluateTimeRange.ts @@ -19,6 +19,7 @@ export function evaluateTimeRange( val: string | DateTime, options: { roundUp: boolean; timezone: TimeZone; fiscalYearStartMonth?: number; now?: DateTimeInput } ) => { + // @ts-ignore if (dateMath.toDateTime) { // @ts-ignore return dateMath.toDateTime(val, options);