[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 3a1862f37f)

Co-authored-by: Ashley Harrison <ashley.harrison@grafana.com>
This commit is contained in:
Grot (@grafanabot)
2023-03-13 05:43:31 -04:00
committed by GitHub
co-authored by Ashley Harrison
parent 01917df71a
commit d75b8c13ef
3 changed files with 16 additions and 6 deletions
+2 -3
View File
@@ -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"]
@@ -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);
};
@@ -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;
}