Skip to content

Commit

Permalink
addressing PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cauemarcondes committed Jun 16, 2021
1 parent 74b9619 commit c30cfdf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});
});
Expand All @@ -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',
});
});
Expand All @@ -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',
});
});
Expand All @@ -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',
});
});
Expand All @@ -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: {
Expand Down Expand Up @@ -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) =>
Expand Down
28 changes: 14 additions & 14 deletions x-pack/plugins/apm/public/context/url_params_context/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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(),
};
}

Expand Down

0 comments on commit c30cfdf

Please sign in to comment.