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 14, 2021
1 parent a81cb9f commit 2f3442d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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', {});
});
});
});
23 changes: 6 additions & 17 deletions x-pack/plugins/apm/public/context/url_params_context/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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(),
};
}

Expand Down

0 comments on commit 2f3442d

Please sign in to comment.