diff --git a/x-pack/plugins/apm/public/components/shared/time_comparison/index.tsx b/x-pack/plugins/apm/public/components/shared/time_comparison/index.tsx index 4ac7ee1a2171ab..cbe7b81486a64d 100644 --- a/x-pack/plugins/apm/public/components/shared/time_comparison/index.tsx +++ b/x-pack/plugins/apm/public/components/shared/time_comparison/index.tsx @@ -69,14 +69,12 @@ export function getComparisonTypes({ const momentStart = moment(start); const momentEnd = moment(end); - const dateDiff = Number( - getDateDifference({ - start: momentStart, - end: momentEnd, - unitOfTime: 'days', - precise: true, - }) - ); + const dateDiff = getDateDifference({ + start: momentStart, + end: momentEnd, + unitOfTime: 'days', + precise: true, + }); // Less than or equals to one day if (dateDiff <= 1) { 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 2ef4e4e519bce6..54ec68947607da 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 @@ -172,12 +172,24 @@ describe('url_params_context helpers', () => { expect(helpers.getExactDate('2021-01-28T05:47:52.134Z')).toBeUndefined(); }); - it('removes rounding option from relative time', () => { + ['s', 'm', 'h', 'd', 'w'].map((roundingOption) => + it(`removes /${roundingOption} rounding option from relative time`, () => { + const spy = jest.spyOn(datemath, 'parse'); + helpers.getExactDate(`now/${roundingOption}`); + expect(spy).toHaveBeenCalledWith('now', {}); + }) + ); + + it('removes rounding option but keeps subtracting 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', {}); }); + + it('removes rounding option but keeps adding time', () => { + const spy = jest.spyOn(datemath, 'parse'); + helpers.getExactDate('now+15m/h'); + expect(spy).toHaveBeenCalledWith('now+15m', {}); + }); }); }); 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 7960d9ab25553a..795ffe8796a70c 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,7 +23,8 @@ export function getExactDate(rawDate?: string, options = {}) { if (rawDate) { const isRelativeDate = rawDate.substring(0, 3) === 'now'; if (isRelativeDate) { - const rawDateWithouRounding = rawDate.replace(/\/(\w)$/, ''); + // remove rounding from relative dates "Today" (now/d) and "This week" (now/w) + const rawDateWithouRounding = rawDate.replace(/\/([smhdw])$/, ''); return getParsedDate(rawDateWithouRounding, options); } } @@ -63,9 +64,8 @@ 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; + const exactEnd = getExactDate(rangeTo) || end; return { start: roundedStart.toISOString(),