From 5255bb78afa106c36f052cbe4844fdf5db2aa9fa Mon Sep 17 00:00:00 2001 From: "Eyo O. Eyo" <7893459+eokoneyo@users.noreply.github.com> Date: Fri, 8 Dec 2023 16:38:07 +0100 Subject: [PATCH] Investigate failing sample data API integration test (#169638) ## Summary Investigate failing sample data integration test. Changes expectation to consider the entire interval in milliseconds of the sample data, in place of the value in weeks. (cherry picked from commit cfe0d1cbc8c9659f17536f9d188b8ee62eec8b55) --- .../lib/translate_timestamp.test.ts | 94 +++++++++++++++++++ .../sample_data/lib/translate_timestamp.ts | 17 ++-- test/api_integration/apis/home/sample_data.ts | 19 ++-- 3 files changed, 116 insertions(+), 14 deletions(-) create mode 100644 src/plugins/home/server/services/sample_data/lib/translate_timestamp.test.ts diff --git a/src/plugins/home/server/services/sample_data/lib/translate_timestamp.test.ts b/src/plugins/home/server/services/sample_data/lib/translate_timestamp.test.ts new file mode 100644 index 0000000000000..f30e59605952e --- /dev/null +++ b/src/plugins/home/server/services/sample_data/lib/translate_timestamp.test.ts @@ -0,0 +1,94 @@ +/* + * 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 { translateTimeRelativeToWeek } from './translate_timestamp'; + +describe('translateTimeRelativeToWeek', () => { + const sourceReference = '2018-01-02T00:00:00'; // Tuesday + const targetReference = '2018-04-25T18:24:58.650'; // Wednesday + + describe('2 weeks before', () => { + test('should properly adjust timestamp when day is before targetReference day of week', () => { + const source = '2017-12-18T23:50:00'; // Monday, -2 week relative to sourceReference + const timestamp = translateTimeRelativeToWeek(source, sourceReference, targetReference); + expect(timestamp).toBe('2018-04-09T23:50:00'); // Monday 2 week before targetReference week + }); + + test('should properly adjust timestamp when day is same as targetReference day of week', () => { + const source = '2017-12-20T23:50:00'; // Wednesday, -2 week relative to sourceReference + const timestamp = translateTimeRelativeToWeek(source, sourceReference, targetReference); + expect(timestamp).toBe('2018-04-11T23:50:00'); // Wednesday 2 week before targetReference week + }); + + test('should properly adjust timestamp when day is after targetReference day of week', () => { + const source = '2017-12-22T16:16:50'; // Friday, -2 week relative to sourceReference + const timestamp = translateTimeRelativeToWeek(source, sourceReference, targetReference); + expect(timestamp).toBe('2018-04-13T16:16:50'); // Friday 2 week before targetReference week + }); + }); + + describe('week before', () => { + test('should properly adjust timestamp when day is before targetReference day of week', () => { + const source = '2017-12-25T23:50:00'; // Monday, -1 week relative to sourceReference + const timestamp = translateTimeRelativeToWeek(source, sourceReference, targetReference); + expect(timestamp).toBe('2018-04-16T23:50:00'); // Monday 1 week before targetReference week + }); + + test('should properly adjust timestamp when day is same as targetReference day of week', () => { + const source = '2017-12-27T23:50:00'; // Wednesday, -1 week relative to sourceReference + const timestamp = translateTimeRelativeToWeek(source, sourceReference, targetReference); + expect(timestamp).toBe('2018-04-18T23:50:00'); // Wednesday 1 week before targetReference week + }); + + test('should properly adjust timestamp when day is after targetReference day of week', () => { + const source = '2017-12-29T16:16:50'; // Friday, -1 week relative to sourceReference + const timestamp = translateTimeRelativeToWeek(source, sourceReference, targetReference); + expect(timestamp).toBe('2018-04-20T16:16:50'); // Friday 1 week before targetReference week + }); + }); + + describe('same week', () => { + test('should properly adjust timestamp when day is before targetReference day of week', () => { + const source = '2018-01-01T23:50:00'; // Monday, same week relative to sourceReference + const timestamp = translateTimeRelativeToWeek(source, sourceReference, targetReference); + expect(timestamp).toBe('2018-04-23T23:50:00'); // Monday same week as targetReference + }); + + test('should properly adjust timestamp when day is same as targetReference day of week', () => { + const source = '2018-01-03T23:50:00'; // Wednesday, same week relative to sourceReference + const timestamp = translateTimeRelativeToWeek(source, sourceReference, targetReference); + expect(timestamp).toBe('2018-04-25T23:50:00'); // Wednesday same week as targetReference + }); + + test('should properly adjust timestamp when day is after targetReference day of week', () => { + const source = '2018-01-05T16:16:50'; // Friday, same week relative to sourceReference + const timestamp = translateTimeRelativeToWeek(source, sourceReference, targetReference); + expect(timestamp).toBe('2018-04-27T16:16:50'); // Friday same week as targetReference + }); + }); + + describe('week after', () => { + test('should properly adjust timestamp when day is before targetReference day of week', () => { + const source = '2018-01-08T23:50:00'; // Monday, 1 week after relative to sourceReference + const timestamp = translateTimeRelativeToWeek(source, sourceReference, targetReference); + expect(timestamp).toBe('2018-04-30T23:50:00'); // Monday 1 week after targetReference week + }); + + test('should properly adjust timestamp when day is same as targetReference day of week', () => { + const source = '2018-01-10T23:50:00'; // Wednesday, same week relative to sourceReference + const timestamp = translateTimeRelativeToWeek(source, sourceReference, targetReference); + expect(timestamp).toBe('2018-05-02T23:50:00'); // Wednesday 1 week after targetReference week + }); + + test('should properly adjust timestamp when day is after targetReference day of week', () => { + const source = '2018-01-12T16:16:50'; // Friday, same week relative to sourceReference + const timestamp = translateTimeRelativeToWeek(source, sourceReference, targetReference); + expect(timestamp).toBe('2018-05-04T16:16:50'); // Friday 1 week after targetReference week + }); + }); +}); diff --git a/src/plugins/home/server/services/sample_data/lib/translate_timestamp.ts b/src/plugins/home/server/services/sample_data/lib/translate_timestamp.ts index b8bd7122cc89e..6142a2aa00709 100644 --- a/src/plugins/home/server/services/sample_data/lib/translate_timestamp.ts +++ b/src/plugins/home/server/services/sample_data/lib/translate_timestamp.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -const MILLISECONDS_IN_DAY = 86400000; +const MILLISECONDS_IN_DAY = 1000 * 60 * 60 * 24; function iso8601ToDateIgnoringTime(iso8601: string) { const split = iso8601.split('-'); @@ -24,13 +24,13 @@ export function dateToIso8601IgnoringTime(date: Date) { const dateItem = new Date(date); const year = dateItem.getFullYear(); const month = dateItem.getMonth() + 1; - const monthString = month < 10 ? `0${month}` : `${month}`; - const dateString = dateItem.getDate() < 10 ? `0${dateItem.getDate()}` : `${dateItem.getDate()}`; + const monthString = String.prototype.padStart.call(month, 2, '0'); + const dateString = String.prototype.padStart.call(dateItem.getDate(), 2, '0'); return `${year}-${monthString}-${dateString}`; } // Translate source timestamp by targetReference timestamp, -// perserving the distance between source and sourceReference +// preserving the distance between source and sourceReference export function translateTimeRelativeToDifference( source: string, sourceReference: any, @@ -47,7 +47,7 @@ export function translateTimeRelativeToDifference( } // Translate source timestamp by targetReference timestamp, -// perserving the week distance between source and sourceReference and day of week of the source timestamp +// preserving the week distance between source and sourceReference and day of week of the source timestamp export function translateTimeRelativeToWeek( source: string, sourceReference: any, @@ -59,10 +59,11 @@ export function translateTimeRelativeToWeek( // If these dates were in the same week, how many days apart would they be? const dayOfWeekDelta = sourceReferenceDate.getDay() - targetReferenceDate.getDay(); - // If we pretend that the targetReference is actually the same day of the week as the - // sourceReference, then we can translate the source to the target while preserving their - // days of the week. + // Given that we assume the target reference is in the same week as the source reference + // and we'd computed how many days apart they'd be apart. + // We then compute the value of the days apart in milliseconds to normalize our target reference const normalizationDelta = dayOfWeekDelta * MILLISECONDS_IN_DAY; + const normalizedTargetReference = dateToIso8601IgnoringTime( new Date(targetReferenceDate.getTime() + normalizationDelta) ); diff --git a/test/api_integration/apis/home/sample_data.ts b/test/api_integration/apis/home/sample_data.ts index c455eed849c4b..9858dee31cd49 100644 --- a/test/api_integration/apis/home/sample_data.ts +++ b/test/api_integration/apis/home/sample_data.ts @@ -8,6 +8,7 @@ import expect from '@kbn/expect'; import type { Response } from 'superagent'; +import differenceInMilliseconds from 'date-fns/differenceInMilliseconds'; import { FtrProviderContext } from '../../ftr_provider_context'; export default function ({ getService }: FtrProviderContext) { @@ -15,11 +16,10 @@ export default function ({ getService }: FtrProviderContext) { const esArchiver = getService('esArchiver'); const es = getService('es'); - const MILLISECOND_IN_WEEK = 1000 * 60 * 60 * 24 * 7; const SPACES = ['default', 'other']; /** * default ID of the flights overview dashboard - * @see src/plugins/home/server/services/sample_data/data_sets/flights/index.ts + * @see {@link src/plugins/home/server/services/sample_data/data_sets/flights/index.ts} */ const FLIGHTS_OVERVIEW_DASHBOARD_ID = '7adfa750-4c81-11e8-b3d7-01146121b73d'; const FLIGHTS_CANVAS_APPLINK_PATH = @@ -72,8 +72,14 @@ export default function ({ getService }: FtrProviderContext) { }); }); - // FLAKY: https://github.com/elastic/kibana/issues/166572 - describe.skip('dates', () => { + describe('dates', () => { + // dates being compared are not arbitrary, but rather the dates of the earliest and latest timestamp of the flight sample data + // this can be verified in the flight data archive here {@link src/plugins/home/server/services/sample_data/data_sets/flights/flights.json.gz} + const sampleDataTimeIntervalInMS = differenceInMilliseconds( + new Date('2018-02-11T14:54:34'), + new Date('2018-01-01T00:00:00') + ); + it('should load elasticsearch index containing sample data with dates relative to current time', async () => { const resp = await es.search<{ timestamp: string }>({ index: 'kibana_sample_data_flights', @@ -83,8 +89,9 @@ export default function ({ getService }: FtrProviderContext) { const doc = resp.hits.hits[0]; const docMilliseconds = Date.parse(doc._source!.timestamp); const nowMilliseconds = Date.now(); + const delta = Math.abs(nowMilliseconds - docMilliseconds); - expect(delta).to.be.lessThan(MILLISECOND_IN_WEEK * 5); + expect(delta).to.be.lessThan(sampleDataTimeIntervalInMS); }); it('should load elasticsearch index containing sample data with dates relative to now parameter', async () => { @@ -100,7 +107,7 @@ export default function ({ getService }: FtrProviderContext) { const docMilliseconds = Date.parse(doc._source!.timestamp); const nowMilliseconds = Date.parse(nowString); const delta = Math.abs(nowMilliseconds - docMilliseconds); - expect(delta).to.be.lessThan(MILLISECOND_IN_WEEK * 5); + expect(delta).to.be.lessThan(sampleDataTimeIntervalInMS); }); }); });