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

fix: heatmap snap domain to interval #1253

Merged
merged 13 commits into from
Jul 30, 2021
Merged
37 changes: 37 additions & 0 deletions packages/charts/src/utils/data/date_time.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { DateTime } from 'luxon';

import { snapDateToInterval } from './date_time';

describe('snap to interval', () => {
it('should snap to begin of calendar interval', () => {
const initialDate = DateTime.fromISO('2020-01-03T07:00:01Z');
const snappedDate = snapDateToInterval(initialDate.toMillis(), 'calendar', '1d', 'start', 'UTC');
expect(DateTime.fromMillis(snappedDate, { zone: 'utc' }).toISO()).toBe('2020-01-03T00:00:00.000Z');
});

it('should snap to end of calendar interval', () => {
const initialDate = DateTime.fromISO('2020-01-03T07:00:01Z');
const snappedDate = snapDateToInterval(initialDate.toMillis(), 'calendar', '1d', 'end', 'UTC');
expect(DateTime.fromMillis(snappedDate, { zone: 'utc' }).toISO()).toBe('2020-01-03T23:59:59.999Z');
});

it('should snap to begin of fixed interval', () => {
const initialDate = DateTime.fromISO('2020-01-03T07:00:01Z');
const snappedDate = snapDateToInterval(initialDate.toMillis(), 'fixed', '30m', 'start', 'UTC');
expect(DateTime.fromMillis(snappedDate, { zone: 'utc' }).toISO()).toBe('2020-01-03T07:00:00.000Z');
});

it('should snap to end of fixed interval', () => {
const initialDate = DateTime.fromISO('2020-01-03T07:00:01Z');
const snappedDate = snapDateToInterval(initialDate.toMillis(), 'fixed', '30m', 'end', 'UTC');
expect(DateTime.fromMillis(snappedDate, { zone: 'utc' }).toISO()).toBe('2020-01-03T07:29:59.999Z');
});
nickofthyme marked this conversation as resolved.
Show resolved Hide resolved
});
83 changes: 83 additions & 0 deletions packages/charts/src/utils/data/date_time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

import { unitOfTime } from 'moment';
import moment from 'moment-timezone';

/** @internal */
Expand All @@ -18,3 +19,85 @@ export function getMomentWithTz(date: number | Date, timeZone?: string) {
}
return moment.tz(date, timeZone);
}

/** @internal */
export type UnixTimestamp = number;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

@monfera monfera Jul 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eelsewhere we have export type TimeMs = number; (that's for duration). So a S or Ms postfix may be useful.

It might be worth putting both this and the existing TimeMs into a common time type file (even outside /charts). The latter could perhaps be renamed DurationMs.


/** @internal */
export function snapDateToInterval(
date: number | Date,
interval: 'calendar' | 'fixed',
unit: string,
monfera marked this conversation as resolved.
Show resolved Hide resolved
snapTo: 'start' | 'end',
timeZone?: string,
): UnixTimestamp {
const momentDate = getMomentWithTz(date, timeZone);

return interval === 'calendar'
? calendarIntervalSnap(momentDate, unit, snapTo).valueOf()
: fixedIntervalSnap(momentDate, unit, snapTo).valueOf();
}

function calendarIntervalSnap(date: moment.Moment, unit: string, snapTo: 'start' | 'end') {
monfera marked this conversation as resolved.
Show resolved Hide resolved
const momentUnitName = esCalendarIntervalsToMoment(unit);
if (!momentUnitName) {
return date;
}
return snapTo === 'start' ? date.startOf(momentUnitName) : date.endOf(momentUnitName);
}
function fixedIntervalSnap(date: moment.Moment, unit: string, snapTo: 'start' | 'end') {
const unitMultiplier = esFixedIntervalsToMomentUnit(unit);
if (isNaN(unitMultiplier)) {
monfera marked this conversation as resolved.
Show resolved Hide resolved
return date;
}

const roundedDate = Math.floor(date.valueOf() / unitMultiplier) * unitMultiplier;
return snapTo === 'start' ? roundedDate : roundedDate + unitMultiplier - 1;
}

function esFixedIntervalsToMomentUnit(unit: string): number {
if (unit.endsWith('ms')) {
monfera marked this conversation as resolved.
Show resolved Hide resolved
return getNumericalUnit(unit, 'ms');
}
if (unit.endsWith('s')) {
return getNumericalUnit(unit, 's') * 1000;
}
if (unit.endsWith('m')) {
return getNumericalUnit(unit, 'm') * 1000 * 60;
}
if (unit.endsWith('h')) {
return getNumericalUnit(unit, 'h') * 1000 * 60 * 60;
}
if (unit.endsWith('d')) {
return getNumericalUnit(unit, 'd') * 1000 * 60 * 60 * 24;
}
return NaN;
markov00 marked this conversation as resolved.
Show resolved Hide resolved
}

function getNumericalUnit(unit: string, intervalLabel: string) {
return Number.parseFloat(unit.slice(0, unit.indexOf(intervalLabel)));
}

function esCalendarIntervalsToMoment(unit: string): unitOfTime.StartOf | undefined {
if (unit === 'minute' || unit === '1m') {
return 'minutes';
}
if (unit === 'hour' || unit === '1h') {
return 'hour';
}
if (unit === 'day' || unit === '1d') {
return 'day';
}
if (unit === 'week' || unit === '1w') {
return 'week';
}
if (unit === 'month' || unit === '1M') {
return 'month';
}
if (unit === 'quarter' || unit === '1q') {
return 'quarter';
}
if (unit === 'year' || unit === '1y') {
return 'year';
}
}
nickofthyme marked this conversation as resolved.
Show resolved Hide resolved