Commit ce1f7908 by Zoltán Bedi Committed by GitHub

Explore: parse time range fix (#28467)

* Explore: parse time range fix

* Remove commented out code

* Fix cases for string epoch and ISO strings

Co-authored-by: Andrej Ocenas <mr.ocenas@gmail.com>
parent 48e753d8
......@@ -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', () => {
......
......@@ -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;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment