Skip to content

Commit

Permalink
[Uptime] Update relative time handling (#55693) (#56589)
Browse files Browse the repository at this point in the history
* update relative time handling

* fix type

* update logic to handle end date

* fix test

* PR feedback

* refactor code

* refactor interval time

* fix tests

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
shahzad31 and elasticmachine authored Feb 3, 2020
1 parent 8821277 commit 2c87f52
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ export const getSupportedUrlParams = (params: {
),
absoluteDateRangeEnd: parseAbsoluteDate(
dateRangeEnd || DATE_RANGE_END,
ABSOLUTE_DATE_RANGE_END
ABSOLUTE_DATE_RANGE_END,
{ roundUp: true }
),
autorefreshInterval: parseUrlInt(autorefreshInterval, AUTOREFRESH_INTERVAL),
autorefreshIsPaused: parseIsPaused(autorefreshIsPaused, AUTOREFRESH_IS_PAUSED),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import DateMath from '@elastic/datemath';

export const parseAbsoluteDate = (date: string, defaultValue: number): number => {
const momentWrapper = DateMath.parse(date);
export const parseAbsoluteDate = (date: string, defaultValue: number, options = {}): number => {
const momentWrapper = DateMath.parse(date, options);
if (momentWrapper) {
return momentWrapper.valueOf();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export const findPotentialMatches = async (
size: number
) => {
const queryResult = await query(queryContext, searchAfter, size);

const checkGroups = new Set<string>();
const monitorIds: string[] = [];
get<any>(queryResult, 'aggregations.monitors.buckets', []).forEach((b: any) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
* you may not use this file except in compliance with the Elastic License.
*/

import DateMath from '@elastic/datemath';
import { APICaller } from 'kibana/server';
import { CursorPagination } from '../adapter_types';
import { INDEX_NAMES } from '../../../../../common/constants';
import { parseRelativeDate } from '../../../helper/get_histogram_interval';

export class QueryContext {
callES: APICaller;
Expand Down Expand Up @@ -95,8 +95,9 @@ export class QueryContext {
// latencies and slowdowns that's dangerous. Making this value larger makes things
// only slower, but only marginally so, and prevents people from seeing weird
// behavior.
const tsStart = DateMath.parse(this.dateRangeEnd)!.subtract(5, 'minutes');
const tsEnd = DateMath.parse(this.dateRangeEnd)!;

const tsEnd = parseRelativeDate(this.dateRangeEnd, { roundUp: true })!;
const tsStart = tsEnd.subtract(5, 'minutes');

return {
range: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { parseRelativeDate } from '../get_histogram_interval';
import { Moment } from 'moment';

describe('Parsing a relative end date properly', () => {
it('converts the upper range of relative end dates to now', async () => {
const thisWeekEndDate = 'now/w';

let endDate = parseRelativeDate(thisWeekEndDate, { roundUp: true });
expect(Date.now() - (endDate as Moment).valueOf()).toBeLessThan(1000);

const todayEndDate = 'now/d';

endDate = parseRelativeDate(todayEndDate, { roundUp: true });

expect(Date.now() - (endDate as Moment).valueOf()).toBeLessThan(1000);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,39 @@
import DateMath from '@elastic/datemath';
import { QUERY } from '../../../common/constants';

export const parseRelativeDate = (dateStr: string, options = {}) => {
// We need this this parsing because if user selects This week or this date
// That represents end date in future, if week or day is still in the middle
// Uptime data can never be collected in future, so we will reset date to now
// in That case. Example case we select this week range will be to='now/w' and from = 'now/w';

const parsedDate = DateMath.parse(dateStr, options);
const dateTimestamp = parsedDate?.valueOf() ?? 0;
if (dateTimestamp > Date.now()) {
return DateMath.parse('now');
}
return parsedDate;
};

export const getHistogramInterval = (
dateRangeStart: string,
dateRangeEnd: string,
bucketCount?: number
): number => {
const from = DateMath.parse(dateRangeStart);
const to = DateMath.parse(dateRangeEnd);
const from = parseRelativeDate(dateRangeStart);

// roundUp is required for relative date like now/w to get the end of the week
const to = parseRelativeDate(dateRangeEnd, { roundUp: true });
if (from === undefined) {
throw Error('Invalid dateRangeStart value');
}
if (to === undefined) {
throw Error('Invalid dateRangeEnd value');
}
return Math.round((to.valueOf() - from.valueOf()) / (bucketCount || QUERY.DEFAULT_BUCKET_COUNT));
const interval = Math.round(
(to.valueOf() - from.valueOf()) / (bucketCount || QUERY.DEFAULT_BUCKET_COUNT)
);

// Interval can never be zero, if it's 0 we return at least 1ms interval
return interval > 0 ? interval : 1;
};

0 comments on commit 2c87f52

Please sign in to comment.