From d75b8c13ef25f90ad363ea83b403d1ec56f9bc09 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Mon, 13 Mar 2023 11:43:31 +0200 Subject: [PATCH] [v9.4.x] TimeSeries: fix time comparer not comparing date strings properly (#64674) TimeSeries: fix time comparer not comparing date strings properly (#64622) * fix time comparer not comparing times properly * move isDateTime last as it's probably the most expensive check (cherry picked from commit 3a1862f37f0b9ae30257a8d229c81231a0e47ccb) Co-authored-by: Ashley Harrison --- .betterer.results | 5 ++--- .../grafana-data/src/datetime/moment_wrapper.ts | 13 ++++++++++++- packages/grafana-data/src/field/fieldComparers.ts | 4 ++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/.betterer.results b/.betterer.results index 0698602e43b..827eddc8187 100644 --- a/.betterer.results +++ b/.betterer.results @@ -122,15 +122,14 @@ exports[`better eslint`] = { ], "packages/grafana-data/src/datetime/moment_wrapper.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], - [0, 0, 0, "Unexpected any. Specify a different type.", "1"], + [0, 0, 0, "Do not use any type assertions.", "1"], [0, 0, 0, "Do not use any type assertions.", "2"], [0, 0, 0, "Do not use any type assertions.", "3"], [0, 0, 0, "Do not use any type assertions.", "4"], [0, 0, 0, "Do not use any type assertions.", "5"], [0, 0, 0, "Do not use any type assertions.", "6"], [0, 0, 0, "Do not use any type assertions.", "7"], - [0, 0, 0, "Do not use any type assertions.", "8"], - [0, 0, 0, "Do not use any type assertions.", "9"] + [0, 0, 0, "Do not use any type assertions.", "8"] ], "packages/grafana-data/src/datetime/parser.test.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"] diff --git a/packages/grafana-data/src/datetime/moment_wrapper.ts b/packages/grafana-data/src/datetime/moment_wrapper.ts index 4bcfab24659..73f5850dde5 100644 --- a/packages/grafana-data/src/datetime/moment_wrapper.ts +++ b/packages/grafana-data/src/datetime/moment_wrapper.ts @@ -89,7 +89,18 @@ export const getLocaleData = (): DateTimeLocale => { return moment.localeData(); }; -export const isDateTime = (value: any): value is DateTime => { +export const isDateTimeInput = (value: unknown): value is DateTimeInput => { + return ( + value === null || + typeof value === 'string' || + typeof value === 'number' || + value instanceof Date || + (Array.isArray(value) && value.every((v) => typeof v === 'string' || typeof v === 'number')) || + isDateTime(value) + ); +}; + +export const isDateTime = (value: unknown): value is DateTime => { return moment.isMoment(value); }; diff --git a/packages/grafana-data/src/field/fieldComparers.ts b/packages/grafana-data/src/field/fieldComparers.ts index 058642dc93e..374fc707206 100644 --- a/packages/grafana-data/src/field/fieldComparers.ts +++ b/packages/grafana-data/src/field/fieldComparers.ts @@ -1,6 +1,6 @@ import { isNumber } from 'lodash'; -import { dateTime, isDateTime } from '../datetime'; +import { dateTime, isDateTimeInput } from '../datetime'; import { Field, FieldType } from '../types/dataFrame'; import { Vector } from '../types/vector'; @@ -34,7 +34,7 @@ export const timeComparer = (a: unknown, b: unknown): number => { return numericComparer(a, b); } - if (isDateTime(a) && isDateTime(b)) { + if (isDateTimeInput(a) && isDateTimeInput(b)) { if (dateTime(a).isBefore(b)) { return -1; }