From ae015fb8744c2cf3e865eb15f44b8939ee447088 Mon Sep 17 00:00:00 2001 From: chrisronline Date: Sat, 12 Dec 2020 08:31:42 -0500 Subject: [PATCH] Fix state filtering in the category list --- ...get_alert_panels_by_category.test.tsx.snap | 126 ++++++++++++++++++ .../lib/get_alert_panels_by_category.test.tsx | 13 ++ .../lib/get_alert_panels_by_category.tsx | 9 +- 3 files changed, 144 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/monitoring/public/alerts/lib/__snapshots__/get_alert_panels_by_category.test.tsx.snap b/x-pack/plugins/monitoring/public/alerts/lib/__snapshots__/get_alert_panels_by_category.test.tsx.snap index e530d29684fdf..546be4a7ac9f9 100644 --- a/x-pack/plugins/monitoring/public/alerts/lib/__snapshots__/get_alert_panels_by_category.test.tsx.snap +++ b/x-pack/plugins/monitoring/public/alerts/lib/__snapshots__/get_alert_panels_by_category.test.tsx.snap @@ -1,5 +1,131 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`getAlertPanelsByCategory non setup mode should allow for state filtering 1`] = ` +Array [ + Object { + "id": 0, + "items": Array [ + Object { + "name": + + Resource utilization + ( + 1 + ) + + , + "panel": 1, + }, + ], + "title": "Alerts", + }, + Object { + "id": 1, + "items": Array [ + Object { + "name": + monitoring_alert_cpu_usage_label + ( + 1 + ) + , + "panel": 2, + }, + ], + "title": "Resource utilization", + }, + Object { + "id": 2, + "items": Array [ + Object { + "name": + + + triggered:0 + + + + es_name_0 + + , + "panel": 3, + }, + Object { + "isSeparator": true, + }, + ], + "title": "monitoring_alert_cpu_usage_label", + }, + Object { + "content": , + "id": 3, + "title": "monitoring_alert_cpu_usage_label", + "width": 400, + }, +] +`; + exports[`getAlertPanelsByCategory non setup mode should not show any alert if none are firing 1`] = ` Array [ Object { diff --git a/x-pack/plugins/monitoring/public/alerts/lib/get_alert_panels_by_category.test.tsx b/x-pack/plugins/monitoring/public/alerts/lib/get_alert_panels_by_category.test.tsx index 86cb9b981dea3..d325c92f14c65 100644 --- a/x-pack/plugins/monitoring/public/alerts/lib/get_alert_panels_by_category.test.tsx +++ b/x-pack/plugins/monitoring/public/alerts/lib/get_alert_panels_by_category.test.tsx @@ -181,6 +181,19 @@ describe('getAlertPanelsByCategory', () => { ); expect(result).toMatchSnapshot(); }); + + it('should allow for state filtering', () => { + const alerts = [getAlert(ALERT_CPU_USAGE, 2)]; + const customStateFilter = (state: AlertState) => state.nodeName === 'es_name_0'; + const result = getAlertPanelsByCategory( + panelTitle, + false, + alerts, + alertsContext, + customStateFilter + ); + expect(result).toMatchSnapshot(); + }); }); describe('setup mode', () => { diff --git a/x-pack/plugins/monitoring/public/alerts/lib/get_alert_panels_by_category.tsx b/x-pack/plugins/monitoring/public/alerts/lib/get_alert_panels_by_category.tsx index 748da28a66d97..82a1a1f841a22 100644 --- a/x-pack/plugins/monitoring/public/alerts/lib/get_alert_panels_by_category.tsx +++ b/x-pack/plugins/monitoring/public/alerts/lib/get_alert_panels_by_category.tsx @@ -61,7 +61,7 @@ export function getAlertPanelsByCategory( states: foundAlert.states, alertName, }); - categoryFiringAlertCount += foundAlert.states.length; + categoryFiringAlertCount += states.length; } } } @@ -144,12 +144,13 @@ export function getAlertPanelsByCategory( id: nodeIndex + 1, title: `${category.label}`, items: category.alerts.map(({ alertName, states }) => { + const filteredStates = states.filter(({ state }) => stateFilter(state)); const alertStatus = alertsContext.allAlerts[alertName]; const name = inSetupMode ? ( {alertStatus.rawAlert.name} ) : ( - {alertStatus.rawAlert.name} ({states.length}) + {alertStatus.rawAlert.name} ({filteredStates.length}) ); return { @@ -169,7 +170,7 @@ export function getAlertPanelsByCategory( for (const category of menu) { for (const { alert, states } of category.alerts) { const items = []; - for (const alertState of states) { + for (const alertState of states.filter(({ state }) => stateFilter(state))) { items.push({ name: ( @@ -211,7 +212,7 @@ export function getAlertPanelsByCategory( }, menu.length); for (const category of menu) { for (const { alert, states } of category.alerts) { - for (const state of states) { + for (const state of states.filter(({ state: _state }) => stateFilter(_state))) { panels.push({ id: ++tertiaryPanelIndex2, title: `${alert.name}`,