Skip to content

Commit 2f3442d

Browse files
committed
addressing PR comments
1 parent a81cb9f commit 2f3442d

2 files changed

Lines changed: 13 additions & 32 deletions

File tree

x-pack/plugins/apm/public/context/url_params_context/helpers.test.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -171,21 +171,13 @@ describe('url_params_context helpers', () => {
171171
it('returns undefined when date in not relative', () => {
172172
expect(helpers.getExactDate('2021-01-28T05:47:52.134Z')).toBeUndefined();
173173
});
174-
it('returns exact date', () => {
175-
jest
176-
.spyOn(datemath, 'parse')
177-
.mockReturnValue(moment('2021-06-02T17:56:49.260Z').utc());
178-
expect(helpers.getExactDate('now-24h/h')?.toISOString()).toEqual(
179-
'2021-06-02T17:56:49.260Z'
180-
);
181-
});
182-
it('returns original date when now/d is passed', () => {
183-
jest
184-
.spyOn(datemath, 'parse')
185-
.mockReturnValue(moment('2021-06-02T17:00:00.000Z').utc());
186-
expect(helpers.getExactDate('now/d')?.toISOString()).toEqual(
187-
'2021-06-02T17:00:00.000Z'
188-
);
174+
175+
it('removes rounding option from relative time', () => {
176+
const spy = jest.spyOn(datemath, 'parse');
177+
helpers.getExactDate('now/d');
178+
expect(spy).toHaveBeenCalledWith('now', {});
179+
helpers.getExactDate('now-24h/h');
180+
expect(spy).toHaveBeenCalledWith('now-24h', {});
189181
});
190182
});
191183
});

x-pack/plugins/apm/public/context/url_params_context/helpers.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,7 @@ export function getExactDate(rawDate?: string, options = {}) {
2323
if (rawDate) {
2424
const isRelativeDate = rawDate.substring(0, 3) === 'now';
2525
if (isRelativeDate) {
26-
const isSubtractingDate = rawDate.indexOf('-') > 0;
27-
const isRoundingDate = rawDate.indexOf('/') > 0;
28-
29-
const rawDateWithouRounding =
30-
// When relative time is subtracting a period and rounding the result (e.g. now-24h/h)
31-
// removed the rounding part in order to get the exact time.
32-
// This is needed because of of "Today"(now/d) and "This week"(now/w) options, it rounds the values up and down
33-
// so the exact time is the rounded value.
34-
isSubtractingDate && isRoundingDate
35-
? rawDate.substring(0, rawDate.indexOf('/'))
36-
: rawDate;
37-
26+
const rawDateWithouRounding = rawDate.replace(/\/(\w)$/, '');
3827
return getParsedDate(rawDateWithouRounding, options);
3928
}
4029
}
@@ -74,15 +63,15 @@ export function getDateRange({
7463

7564
// rounds down start to minute
7665
const roundedStart = moment(start).startOf('minute');
66+
// rounds down to minute
67+
const exactStart = getExactDate(rangeFrom) || roundedStart;
68+
const exactEnd = getExactDate(rangeTo, { roundUp: true }) || end;
7769

7870
return {
7971
start: roundedStart.toISOString(),
80-
exactStart:
81-
getExactDate(rangeFrom)?.toISOString() || roundedStart.toISOString(),
72+
exactStart: exactStart.toISOString(),
8273
end: end.toISOString(),
83-
exactEnd:
84-
getExactDate(rangeTo, { roundUp: true })?.toISOString() ||
85-
end.toISOString(),
74+
exactEnd: exactEnd.toISOString(),
8675
};
8776
}
8877

0 commit comments

Comments
 (0)