From 4527748e238ce17403f6aabfc0176356ce81c84a Mon Sep 17 00:00:00 2001 From: Justin Kambic Date: Tue, 17 Sep 2024 08:08:51 -0400 Subject: [PATCH] [Synthetics] Fix issue where heatmap UI crashes on undefined histogram data (#192508) ## Summary Recently, [we fixed](https://github.com/elastic/kibana/pull/184177) an [issue](https://github.com/elastic/kibana/issues/180076) where the heatmap data on the detail and monitor history pages would not fill up. A side effect of this fix was a new regression that caused certain rarer cases to see the page crash because of an unhandled case of calling a function on a potentially-null object; our histogram data used to populate this heatmap can be `undefined` in certain cases. This patch introduces a change that will handle this case, and adds unit tests for the module in question. ### 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 - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [ ] 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—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—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) --------- Co-authored-by: Shahzad (cherry picked from commit da2f7f6dae265e29713e5c6586c542499b03bcd6) --- .../monitor_status_data.test.ts | 87 +++++++++++++++++++ .../monitor_status/monitor_status_data.ts | 13 ++- .../monitor_status/use_monitor_status_data.ts | 2 +- .../synthetics/state/status_heatmap/index.ts | 2 +- 4 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/components/monitor_details/monitor_status/monitor_status_data.test.ts diff --git a/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/components/monitor_details/monitor_status/monitor_status_data.test.ts b/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/components/monitor_details/monitor_status/monitor_status_data.test.ts new file mode 100644 index 0000000000000..488db9c2a112b --- /dev/null +++ b/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/components/monitor_details/monitor_status/monitor_status_data.test.ts @@ -0,0 +1,87 @@ +/* + * 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; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { createStatusTimeBins, getStatusEffectiveValue } from './monitor_status_data'; + +describe('createStatusTimeBins', () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should return default values when `heatmapData` is `undefined`', () => { + const timeBuckets = [ + { start: 1000, end: 2000 }, + { start: 2000, end: 3000 }, + ]; + + const result = createStatusTimeBins(timeBuckets, undefined); + + expect(result).toEqual([ + { start: 1000, end: 2000, ups: 0, downs: 0, value: 0 }, + { start: 2000, end: 3000, ups: 0, downs: 0, value: 0 }, + ]); + }); + + it('should calculate `ups` and `downs` correctly from `heatmapData`', () => { + const timeBuckets = [ + { start: 1000, end: 2000 }, + { start: 2000, end: 3000 }, + ]; + + const heatmapData = [ + { key: 1500, key_as_string: '1500', up: { value: 1 }, down: { value: 2 }, doc_count: 3 }, + { key: 2500, key_as_string: '2500', up: { value: 3 }, down: { value: 1 }, doc_count: 4 }, + ]; + + const result = createStatusTimeBins(timeBuckets, heatmapData); + + expect(result).toEqual([ + { start: 1000, end: 2000, ups: 1, downs: 2, value: getStatusEffectiveValue(1, 2) }, + { start: 2000, end: 3000, ups: 3, downs: 1, value: getStatusEffectiveValue(3, 1) }, + ]); + }); + + it('should return value 0 when ups + downs is 0', () => { + const timeBuckets = [ + { start: 1000, end: 2000 }, + { start: 2000, end: 3000 }, + ]; + + const heatmapData = [ + { key: 1500, key_as_string: '1500', up: { value: 0 }, down: { value: 0 }, doc_count: 0 }, + { key: 2500, key_as_string: '2500', up: { value: 0 }, down: { value: 0 }, doc_count: 0 }, + ]; + + const result = createStatusTimeBins(timeBuckets, heatmapData); + + expect(result).toEqual([ + { start: 1000, end: 2000, ups: 0, downs: 0, value: 0 }, + { start: 2000, end: 3000, ups: 0, downs: 0, value: 0 }, + ]); + }); + + it('should filter heatmapData correctly based on start and end values', () => { + const timeBuckets = [ + { start: 1000, end: 2000 }, + { start: 2000, end: 3000 }, + ]; + + const heatmapData = [ + { key: 500, key_as_string: '500', doc_count: 2, up: { value: 1 }, down: { value: 1 } }, + { key: 1500, key_as_string: '1500', doc_count: 5, up: { value: 2 }, down: { value: 3 } }, + { key: 2500, key_as_string: '2500', doc_count: 9, up: { value: 4 }, down: { value: 5 } }, + { key: 3500, key_as_string: '3500', doc_count: 1, up: { value: 6 }, down: { value: 7 } }, + ]; + + const result = createStatusTimeBins(timeBuckets, heatmapData); + + expect(result).toEqual([ + { start: 1000, end: 2000, ups: 2, downs: 3, value: getStatusEffectiveValue(2, 3) }, + { start: 2000, end: 3000, ups: 4, downs: 5, value: getStatusEffectiveValue(4, 5) }, + ]); + }); +}); diff --git a/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/components/monitor_details/monitor_status/monitor_status_data.ts b/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/components/monitor_details/monitor_status/monitor_status_data.ts index e5ee43aa04f8d..0a76badc574ab 100644 --- a/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/components/monitor_details/monitor_status/monitor_status_data.ts +++ b/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/components/monitor_details/monitor_status/monitor_status_data.ts @@ -114,9 +114,18 @@ export function createTimeBuckets(intervalMinutes: number, from: number, to: num export function createStatusTimeBins( timeBuckets: MonitorStatusTimeBucket[], - heatmapData: MonitorStatusHeatmapBucket[] + heatmapData?: MonitorStatusHeatmapBucket[] ): MonitorStatusTimeBin[] { return timeBuckets.map(({ start, end }) => { + if (!Array.isArray(heatmapData)) { + return { + start, + end, + ups: 0, + downs: 0, + value: 0, + }; + } const { ups, downs } = heatmapData .filter(({ key }) => key >= start && key <= end) .reduce( @@ -163,7 +172,7 @@ export function getBrushData(e: BrushEvent) { return { from, to, fromUtc, toUtc }; } -function getStatusEffectiveValue(ups: number, downs: number): number { +export function getStatusEffectiveValue(ups: number, downs: number): number { if (ups === downs) { return -0.1; } diff --git a/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/components/monitor_details/monitor_status/use_monitor_status_data.ts b/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/components/monitor_details/monitor_status/use_monitor_status_data.ts index 59d807cb3bef8..efb001f1776b7 100644 --- a/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/components/monitor_details/monitor_status/use_monitor_status_data.ts +++ b/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/components/monitor_details/monitor_status/use_monitor_status_data.ts @@ -59,7 +59,7 @@ export const useMonitorStatusData = ({ from, to, initialSizeRef }: Props) => { }, [binsAvailableByWidth, initialSizeRef]); useEffect(() => { - if (monitor?.id && location?.label && debouncedBinsCount !== null && minsPerBin !== null) { + if (monitor?.id && location?.label && debouncedBinsCount !== null && !!minsPerBin) { dispatch( quietGetMonitorStatusHeatmapAction.get({ monitorId: monitor.id, diff --git a/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/state/status_heatmap/index.ts b/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/state/status_heatmap/index.ts index 29f8a1ba87345..b01b7d0e0b918 100644 --- a/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/state/status_heatmap/index.ts +++ b/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/state/status_heatmap/index.ts @@ -18,7 +18,7 @@ import { } from './actions'; export interface MonitorStatusHeatmap { - heatmap: MonitorStatusHeatmapBucket[]; + heatmap?: MonitorStatusHeatmapBucket[]; loading: boolean; error: IHttpSerializedFetchError | null; }