diff --git a/public/app/core/utils/explore.test.ts b/public/app/core/utils/explore.test.ts index eddbec19974..fc239661fa8 100644 --- a/public/app/core/utils/explore.test.ts +++ b/public/app/core/utils/explore.test.ts @@ -11,6 +11,7 @@ import { updateHistory, getExploreUrl, GetExploreUrlArguments, + getTimeRangeFromUrl, } from './explore'; import store from 'app/core/store'; import { DataQueryError, dateTime, ExploreUrlState, LogsSortOrder } from '@grafana/data'; @@ -279,6 +280,43 @@ describe('hasRefId', () => { }); }); +describe('getTimeRangeFromUrl', () => { + it('should parse moment date', () => { + // convert date strings to moment object + const range = { from: dateTime('2020-10-22T10:44:33.615Z'), to: dateTime('2020-10-22T10:49:33.615Z') }; + const result = getTimeRangeFromUrl(range, 'browser'); + expect(result.raw).toEqual(range); + }); + + it('should parse epoch strings', () => { + const range = { + from: dateTime('2020-10-22T10:00:00Z') + .valueOf() + .toString(), + to: dateTime('2020-10-22T11:00:00Z') + .valueOf() + .toString(), + }; + const result = getTimeRangeFromUrl(range, 'browser'); + expect(result.from.valueOf()).toEqual(dateTime('2020-10-22T10:00:00Z').valueOf()); + expect(result.to.valueOf()).toEqual(dateTime('2020-10-22T11:00:00Z').valueOf()); + expect(result.raw.from.valueOf()).toEqual(dateTime('2020-10-22T10:00:00Z').valueOf()); + expect(result.raw.to.valueOf()).toEqual(dateTime('2020-10-22T11:00:00Z').valueOf()); + }); + + it('should parse ISO strings', () => { + const range = { + from: dateTime('2020-10-22T10:00:00Z').toISOString(), + to: dateTime('2020-10-22T11:00:00Z').toISOString(), + }; + const result = getTimeRangeFromUrl(range, 'browser'); + expect(result.from.valueOf()).toEqual(dateTime('2020-10-22T10:00:00Z').valueOf()); + expect(result.to.valueOf()).toEqual(dateTime('2020-10-22T11:00:00Z').valueOf()); + expect(result.raw.from.valueOf()).toEqual(dateTime('2020-10-22T10:00:00Z').valueOf()); + expect(result.raw.to.valueOf()).toEqual(dateTime('2020-10-22T11:00:00Z').valueOf()); + }); +}); + describe('getFirstQueryErrorWithoutRefId', () => { describe('when called with a null value', () => { it('then it should return undefined', () => { diff --git a/public/app/core/utils/explore.ts b/public/app/core/utils/explore.ts index 624881ea6dc..d2f3edbc691 100644 --- a/public/app/core/utils/explore.ts +++ b/public/app/core/utils/explore.ts @@ -23,6 +23,8 @@ import { urlUtil, ExploreUrlState, rangeUtil, + DateTime, + isDateTime, } from '@grafana/data'; import store from 'app/core/store'; import { v4 as uuidv4 } from 'uuid'; @@ -355,11 +357,15 @@ export const getTimeRange = (timeZone: TimeZone, rawRange: RawTimeRange): TimeRa }; }; -const parseRawTime = (value: any): TimeFragment | null => { +const parseRawTime = (value: string | DateTime): TimeFragment | null => { if (value === null) { return null; } + if (isDateTime(value)) { + return value; + } + if (value.indexOf('now') !== -1) { return value; } @@ -374,11 +380,18 @@ const parseRawTime = (value: any): TimeFragment | null => { return toUtc(value, 'YYYY-MM-DD HH:mm:ss'); } - if (!isNaN(value)) { + // This should handle cases where value is an epoch time as string + if (value.match(/^\d+$/)) { const epoch = parseInt(value, 10); return toUtc(epoch); } + // This should handle ISO strings + const time = toUtc(value); + if (time.isValid()) { + return time; + } + return null; };