Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[APM] Fixing time comparison types #101423

Merged
merged 9 commits into from
Jun 16, 2021
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';
cauemarcondes marked this conversation as resolved.
Show resolved Hide resolved
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)$/, '');
cauemarcondes marked this conversation as resolved.
Show resolved Hide resolved
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