Skip to content

Commit

Permalink
TimeSeries: fix time comparer not comparing date strings properly (#6…
Browse files Browse the repository at this point in the history
…4622)

* fix time comparer not comparing times properly

* move isDateTime last as it's probably the most expensive check
  • Loading branch information
ashharrison90 authored Mar 13, 2023
1 parent d6eea0c commit 3a1862f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
5 changes: 2 additions & 3 deletions .betterer.results
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,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.ts:5381": [
[0, 0, 0, "Do not use any type assertions.", "0"],
Expand Down
13 changes: 12 additions & 1 deletion packages/grafana-data/src/datetime/moment_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
4 changes: 2 additions & 2 deletions packages/grafana-data/src/field/fieldComparers.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 3a1862f

Please sign in to comment.