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

[Uptime] Update relative time handling #55693

Merged
merged 16 commits into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};