From e64b671c2794bbb198becd1c22f7c672718333c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Fri, 12 Jun 2020 18:48:10 +0100 Subject: [PATCH] fix(ui): correctly check if @cluster label should be rendered --- .../Grid/AlertGrid/AlertGroup/Alert/index.js | 35 +++++++++---------- .../Grid/AlertGrid/AlertGroup/index.js | 12 +++---- .../Grid/AlertGrid/AlertGroup/index.test.js | 13 ++++++- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/ui/src/Components/Grid/AlertGrid/AlertGroup/Alert/index.js b/ui/src/Components/Grid/AlertGrid/AlertGroup/Alert/index.js index ab4c9634d..fdfadd8b3 100644 --- a/ui/src/Components/Grid/AlertGrid/AlertGroup/Alert/index.js +++ b/ui/src/Components/Grid/AlertGrid/AlertGroup/Alert/index.js @@ -38,7 +38,15 @@ const Alert = ({ ]; const silences = {}; + let clusters = []; + let isInhibited = false; for (const am of alert.alertmanager) { + if (!clusters.includes(am.cluster)) { + clusters.push(am.cluster); + } + if (am.inhibitedBy.length > 0) { + isInhibited = true; + } if (!silences[am.cluster]) { silences[am.cluster] = { alertmanager: am, @@ -79,9 +87,7 @@ const Alert = ({ silenceFormStore={silenceFormStore} setIsMenuOpen={setIsMenuOpen} /> - {alert.alertmanager - .map((am) => am.inhibitedBy.length) - .reduce((sum, x) => sum + x) > 0 ? ( + {isInhibited ? ( @@ -97,21 +103,14 @@ const Alert = ({ /> ))} {showAlertmanagers - ? alert.alertmanager - .map((am) => am.cluster) - .reduce( - (unique, item) => - unique.includes(item) ? unique : [...unique, item], - [] - ) - .map((cluster) => ( - - )) + ? clusters.map((cluster) => ( + + )) : null} {showReceiver ? ( 1 && AllAlertsAreUsingSameAlertmanagers(group.alerts); if (showAlertmanagersInFooter) { - footerAlertmanagers = group.alerts[0].alertmanager - .map((am) => am.cluster) - .reduce( - (unique, item) => - unique.includes(item) ? unique : [...unique, item], - [] - ); + for (const am of group.alerts[0].alertmanager) { + if (!footerAlertmanagers.includes(am.cluster)) { + footerAlertmanagers.push(am.cluster); + } + } } } diff --git a/ui/src/Components/Grid/AlertGrid/AlertGroup/index.test.js b/ui/src/Components/Grid/AlertGrid/AlertGroup/index.test.js index ba8407496..6aaf803c0 100644 --- a/ui/src/Components/Grid/AlertGrid/AlertGroup/index.test.js +++ b/ui/src/Components/Grid/AlertGrid/AlertGroup/index.test.js @@ -156,7 +156,18 @@ describe("", () => { expect(labels.at(2).text()).toBe("@receiver: by-name"); }); - it("doesn't render alertmanager labels in footer when they are unique", () => { + it("doesn't render @cluster labels with empty alertmanager array", () => { + MockAlerts(2); + for (let i = 0; i < group.alerts.length; i++) { + group.alerts[i].alertmanager = []; + } + const tree = MountedAlertGroup(jest.fn(), true).find("AlertGroup"); + const labels = tree.find("GroupFooter").find("FilteringLabel"); + expect(labels).toHaveLength(1); + expect(labels.at(0).text()).toBe("@receiver: by-name"); + }); + + it("doesn't render @cluster labels in footer when they are unique", () => { MockAlerts(5); for (let i = 0; i < group.alerts.length; i++) { group.alerts[i].alertmanager[0].name = `fakeAlertmanager${i}`;