[ML] Fix actual and typical formatting for the time_of_week function (#134353)

* fix formatting for time_of_week detector

* udpate tests

* update comments for tests
This commit is contained in:
Dima Arnautov 2022-06-15 12:58:18 +02:00 committed by GitHub
parent d02d12b859
commit becbd08702
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 11 deletions

View file

@ -19,6 +19,7 @@ describe('ML - formatValue formatter', () => {
bucket_span: 900,
detector_index: 0,
is_interim: false,
// '2018-06-28 03:00' in UTC
timestamp: 1530155700000,
by_field_name: 'clientip',
by_field_value: '65.55.215.39',
@ -51,12 +52,15 @@ describe('ML - formatValue formatter', () => {
moment.tz.setDefault('Browser');
});
// For time_of_day and time_of_week test values which are offsets in seconds
// from UTC start of week / day are formatted correctly using the test timezone.
// For time_of_week test that values which are offsets seconds after a whole number of weeks after 1/1/1970 in UTC
// are formatted correctly using the test timezone.
test('correctly formats time_of_week value from numeric input', () => {
expect(formatValue(359739, 'time_of_week', undefined, timeOfWeekRecord)).toBe('Wed 23:55');
// ~100h offset
expect(formatValue(359739, 'time_of_week', undefined, timeOfWeekRecord)).toBe('Sun 23:55');
});
// For time_of_day test values which are offsets in seconds
// from UTC start of day are formatted correctly using the test timezone.
test('correctly formats time_of_day value from numeric input', () => {
expect(formatValue(73781, 'time_of_day', undefined, timeOfDayRecord)).toBe('15:29');
});
@ -72,7 +76,7 @@ describe('ML - formatValue formatter', () => {
});
test('correctly formats time_of_week value from array input', () => {
expect(formatValue([359739], 'time_of_week', undefined, timeOfWeekRecord)).toBe('Wed 23:55');
expect(formatValue([359739], 'time_of_week', undefined, timeOfWeekRecord)).toBe('Sun 23:55');
});
test('correctly formats time_of_day value from array input', () => {

View file

@ -64,19 +64,26 @@ export function formatSingleValue(
return '';
}
// If the analysis function is time_of_week/day, format as day/time.
// For time_of_week / day, actual / typical is the UTC offset in seconds from the
// start of the week / day, so need to manipulate to UTC moment of the start of the week / day
// that the anomaly occurred using record timestamp if supplied, add on the offset, and finally
// revert back to configured timezone for formatting.
if (mlFunction === 'time_of_week') {
const d =
const date =
record !== undefined && record.timestamp !== undefined
? new Date(record.timestamp)
: new Date();
const utcMoment = moment.utc(d).startOf('week').add(value, 's');
/**
* For time_of_week we model "time in UTC" modulo "duration of week in seconds".
* This means the numbers we output from the backend are seconds after a whole number of weeks after 1/1/1970 in UTC.
*/
const remainder = moment(date).unix() % moment.duration(1, 'week').asSeconds();
const offset = moment.duration(remainder, 'seconds');
const utcMoment = moment.utc(date).subtract(offset).startOf('day').add(value, 's');
return moment(utcMoment.valueOf()).format('ddd HH:mm');
} else if (mlFunction === 'time_of_day') {
/**
* For time_of_day, actual / typical is the UTC offset in seconds from the
* start of the day, so need to manipulate to UTC moment of the start of the day
* that the anomaly occurred using record timestamp if supplied, add on the offset, and finally
* revert to configured timezone for formatting.
*/
const d =
record !== undefined && record.timestamp !== undefined
? new Date(record.timestamp)