From c30cfdf590c7630423e03f783e6d93c9d447ae8d Mon Sep 17 00:00:00 2001 From: cauemarcondes Date: Wed, 16 Jun 2021 09:24:12 -0400 Subject: [PATCH] addressing PR comments --- .../url_params_context/helpers.test.ts | 26 +++++++++-------- .../context/url_params_context/helpers.ts | 28 +++++++++---------- 2 files changed, 28 insertions(+), 26 deletions(-) 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 54ec68947607da..784b10b3f3ee1e 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 @@ -25,8 +25,8 @@ describe('url_params_context helpers', () => { }) ).toEqual({ start: '2021-01-28T05:47:00.000Z', - exactStart: '2021-01-28T05:47:00.000Z', end: '2021-01-28T05:48:55.304Z', + exactStart: '2021-01-28T05:47:52.134Z', exactEnd: '2021-01-28T05:48:55.304Z', }); }); @@ -41,8 +41,8 @@ describe('url_params_context helpers', () => { }) ).toEqual({ start: '2021-01-27T05:46:00.000Z', - exactStart: '2021-01-27T05:46:00.000Z', end: '2021-01-28T05:46:13.367Z', + exactStart: '2021-01-27T05:46:07.377Z', exactEnd: '2021-01-28T05:46:13.367Z', }); }); @@ -58,8 +58,8 @@ describe('url_params_context helpers', () => { }) ).toEqual({ start: '2020-01-28T05:52:00.000Z', - exactStart: '2020-01-28T05:52:00.000Z', end: '2021-01-28T05:52:39.741Z', + exactStart: '2020-01-28T05:52:36.290Z', exactEnd: '2021-01-28T05:52:39.741Z', }); }); @@ -83,8 +83,8 @@ describe('url_params_context helpers', () => { }) ).toEqual({ start: '1970-01-01T00:00:00.000Z', - exactStart: '1970-01-01T00:00:00.000Z', end: '1971-01-01T00:00:00.000Z', + exactStart: '1970-01-01T00:00:00.000Z', exactEnd: '1971-01-01T00:00:00.000Z', }); }); @@ -106,19 +106,22 @@ describe('url_params_context helpers', () => { }) ).toEqual({ start: '1972-01-01T00:00:00.000Z', - exactStart: '1972-01-01T00:00:00.000Z', end: '1973-01-01T00:00:00.000Z', - exactEnd: '1973-01-01T00:00:00.000Z', + exactStart: undefined, + exactEnd: undefined, }); }); }); describe('when the start or end are invalid', () => { it('returns the previous state', () => { + const endDate = moment('2021-06-04T18:03:24.211Z'); jest .spyOn(datemath, 'parse') .mockReturnValueOnce(undefined) - .mockReturnValueOnce(moment('2021-06-04T18:03:24.211Z')); + .mockReturnValueOnce(endDate) + .mockReturnValueOnce(undefined) + .mockReturnValueOnce(endDate); expect( helpers.getDateRange({ state: { @@ -165,11 +168,10 @@ describe('url_params_context helpers', () => { }); describe('getExactDate', () => { - it('returns undefined when date in not provided', () => { - expect(helpers.getExactDate()).toBeUndefined(); - }); - it('returns undefined when date in not relative', () => { - expect(helpers.getExactDate('2021-01-28T05:47:52.134Z')).toBeUndefined(); + it('returns date when it is not not relative', () => { + expect(helpers.getExactDate('2021-01-28T05:47:52.134Z')).toEqual( + new Date('2021-01-28T05:47:52.134Z') + ); }); ['s', 'm', 'h', 'd', 'w'].map((roundingOption) => 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 795ffe8796a70c..902456bf4ebc07 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 @@ -19,15 +19,14 @@ function getParsedDate(rawDate?: string, options = {}) { } } -export function getExactDate(rawDate?: string, options = {}) { - if (rawDate) { - const isRelativeDate = rawDate.substring(0, 3) === 'now'; - if (isRelativeDate) { - // remove rounding from relative dates "Today" (now/d) and "This week" (now/w) - const rawDateWithouRounding = rawDate.replace(/\/([smhdw])$/, ''); - return getParsedDate(rawDateWithouRounding, options); - } +export function getExactDate(rawDate: string) { + const isRelativeDate = rawDate.startsWith('now'); + if (isRelativeDate) { + // remove rounding from relative dates "Today" (now/d) and "This week" (now/w) + const rawDateWithouRounding = rawDate.replace(/\/([smhdw])$/, ''); + return getParsedDate(rawDateWithouRounding); } + return getParsedDate(rawDate); } export function getDateRange({ @@ -51,27 +50,28 @@ export function getDateRange({ const start = getParsedDate(rangeFrom); const end = getParsedDate(rangeTo, { roundUp: true }); + const exactStart = rangeFrom ? getExactDate(rangeFrom) : undefined; + const exactEnd = rangeTo ? getExactDate(rangeTo) : undefined; + // `getParsedDate` will return undefined for invalid or empty dates. We return // the previous state if either date is undefined. if (!start || !end) { return { start: state.start, - exactStart: state.start, end: state.end, - exactEnd: state.end, + exactStart: state.exactStart, + exactEnd: state.exactEnd, }; } // rounds down start to minute const roundedStart = moment(start).startOf('minute'); - const exactStart = getExactDate(rangeFrom) || roundedStart; - const exactEnd = getExactDate(rangeTo) || end; return { start: roundedStart.toISOString(), - exactStart: exactStart.toISOString(), end: end.toISOString(), - exactEnd: exactEnd.toISOString(), + exactStart: exactStart?.toISOString(), + exactEnd: exactEnd?.toISOString(), }; }