Skip to content

Commit

Permalink
Investigate failing sample data API integration test (#169638)
Browse files Browse the repository at this point in the history
## 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.

<!--
### Checklist

Delete any items that are not applicable to this PR.

- [ ] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [ ] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [ ] Any UI touched in this PR is usable by keyboard only (learn more
about [keyboard accessibility](https://webaim.org/techniques/keyboard/))
- [ ] Any UI touched in this PR does not create any new axe failures
(run axe in browser:
[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),
[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))
- [ ] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [ ] This renders correctly on smaller devices using a responsive
layout. (You can test this [in your
browser](https://www.browserstack.com/guide/responsive-testing-on-local-server))
- [ ] This was checked for [cross-browser
compatibility](https://www.elastic.co/support/matrix#matrix_browsers)

### Risk Matrix

Delete this section if it is not applicable to this PR.

Before closing this PR, invite QA, stakeholders, and other developers to
identify risks that should be tested prior to the change/feature
release.

When forming the risk matrix, consider some of the following examples
and how they may potentially impact the change:

| Risk | Probability | Severity | Mitigation/Notes |

|---------------------------|-------------|----------|-------------------------|
| Multiple Spaces&mdash;unexpected behavior in non-default Kibana Space.
| Low | High | Integration tests will verify that all features are still
supported in non-default Kibana Space and when user switches between
spaces. |
| Multiple nodes&mdash;Elasticsearch polling might have race conditions
when multiple Kibana nodes are polling for the same tasks. | High | Low
| Tasks are idempotent, so executing them multiple times will not result
in logical error, but will degrade performance. To test for this case we
add plenty of unit tests around this logic and document manual testing
procedure. |
| Code should gracefully handle cases when feature X or plugin Y are
disabled. | Medium | High | Unit tests will verify that any feature flag
or plugin combination still results in our service operational. |
| [See more potential risk
examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) |

### For maintainers

- [ ] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

-->

(cherry picked from commit cfe0d1c)
  • Loading branch information
eokoneyo committed Dec 8, 2023
1 parent 2307225 commit 5255bb7
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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('-');
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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)
);
Expand Down
19 changes: 13 additions & 6 deletions test/api_integration/apis/home/sample_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@

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) {
const supertest = getService('supertest');
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 =
Expand Down Expand Up @@ -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',
Expand All @@ -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 () => {
Expand All @@ -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);
});
});
});
Expand Down

0 comments on commit 5255bb7

Please sign in to comment.