diff --git a/x-pack/plugins/apm/public/context/url_params_context/helpers.test.ts b/x-pack/plugins/apm/public/context/url_params_context/helpers.test.ts index 9cdf36e0bd655e..2ef4e4e519bce6 100644 --- a/x-pack/plugins/apm/public/context/url_params_context/helpers.test.ts +++ b/x-pack/plugins/apm/public/context/url_params_context/helpers.test.ts @@ -171,21 +171,13 @@ describe('url_params_context helpers', () => { it('returns undefined when date in not relative', () => { expect(helpers.getExactDate('2021-01-28T05:47:52.134Z')).toBeUndefined(); }); - it('returns exact date', () => { - jest - .spyOn(datemath, 'parse') - .mockReturnValue(moment('2021-06-02T17:56:49.260Z').utc()); - expect(helpers.getExactDate('now-24h/h')?.toISOString()).toEqual( - '2021-06-02T17:56:49.260Z' - ); - }); - it('returns original date when now/d is passed', () => { - jest - .spyOn(datemath, 'parse') - .mockReturnValue(moment('2021-06-02T17:00:00.000Z').utc()); - expect(helpers.getExactDate('now/d')?.toISOString()).toEqual( - '2021-06-02T17:00:00.000Z' - ); + + it('removes rounding option from relative time', () => { + const spy = jest.spyOn(datemath, 'parse'); + helpers.getExactDate('now/d'); + expect(spy).toHaveBeenCalledWith('now', {}); + helpers.getExactDate('now-24h/h'); + expect(spy).toHaveBeenCalledWith('now-24h', {}); }); }); }); diff --git a/x-pack/plugins/apm/public/context/url_params_context/helpers.ts b/x-pack/plugins/apm/public/context/url_params_context/helpers.ts index 584332542f5128..7960d9ab25553a 100644 --- a/x-pack/plugins/apm/public/context/url_params_context/helpers.ts +++ b/x-pack/plugins/apm/public/context/url_params_context/helpers.ts @@ -23,18 +23,7 @@ export function getExactDate(rawDate?: string, options = {}) { if (rawDate) { const isRelativeDate = rawDate.substring(0, 3) === 'now'; if (isRelativeDate) { - const isSubtractingDate = rawDate.indexOf('-') > 0; - const isRoundingDate = rawDate.indexOf('/') > 0; - - const rawDateWithouRounding = - // When relative time is subtracting a period and rounding the result (e.g. now-24h/h) - // removed the rounding part in order to get the exact time. - // This is needed because of of "Today"(now/d) and "This week"(now/w) options, it rounds the values up and down - // so the exact time is the rounded value. - isSubtractingDate && isRoundingDate - ? rawDate.substring(0, rawDate.indexOf('/')) - : rawDate; - + const rawDateWithouRounding = rawDate.replace(/\/(\w)$/, ''); return getParsedDate(rawDateWithouRounding, options); } } @@ -74,15 +63,15 @@ export function getDateRange({ // rounds down start to minute const roundedStart = moment(start).startOf('minute'); + // rounds down to minute + const exactStart = getExactDate(rangeFrom) || roundedStart; + const exactEnd = getExactDate(rangeTo, { roundUp: true }) || end; return { start: roundedStart.toISOString(), - exactStart: - getExactDate(rangeFrom)?.toISOString() || roundedStart.toISOString(), + exactStart: exactStart.toISOString(), end: end.toISOString(), - exactEnd: - getExactDate(rangeTo, { roundUp: true })?.toISOString() || - end.toISOString(), + exactEnd: exactEnd.toISOString(), }; }