From cda66ca82e91a105ba44f8e05479a01df2e86ea9 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 1 Jul 2025 14:34:19 +0200 Subject: [PATCH] menu: Only count the worst states in badges fixes #365 --- .../Renderer/ProcessProblemsBadge.php | 29 ++++++++++++++++--- .../Renderer/ProcessesProblemsBadge.php | 15 ++++++++-- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/library/Businessprocess/Web/Navigation/Renderer/ProcessProblemsBadge.php b/library/Businessprocess/Web/Navigation/Renderer/ProcessProblemsBadge.php index 3794422..c3e8e77 100644 --- a/library/Businessprocess/Web/Navigation/Renderer/ProcessProblemsBadge.php +++ b/library/Businessprocess/Web/Navigation/Renderer/ProcessProblemsBadge.php @@ -10,6 +10,19 @@ use Throwable; class ProcessProblemsBadge extends BadgeNavigationItemRenderer { + /** + * Icinga state to badge state mapping + * + * @var array + */ + public const NODE_STATE_TO_BADGE_STATE = [ + Node::ICINGA_OK => self::STATE_OK, + Node::ICINGA_WARNING => self::STATE_WARNING, + Node::ICINGA_CRITICAL => self::STATE_CRITICAL, + Node::ICINGA_UNKNOWN => self::STATE_UNKNOWN, + Node::ICINGA_PENDING => self::STATE_PENDING + ]; + /** * Cached count * @@ -24,6 +37,7 @@ class ProcessProblemsBadge extends BadgeNavigationItemRenderer { if ($this->count === null) { $count = 0; + $state = Node::ICINGA_OK; try { $storage = LegacyStorage::getInstance(); @@ -33,9 +47,16 @@ class ProcessProblemsBadge extends BadgeNavigationItemRenderer // Probably, but it is how it is for a very long time. So it is unlikely to change. (Finger crossed.) $bp = $storage->loadProcess($this->getBpConfigName()); foreach ($bp->getRootNodes() as $rootNode) { + $nodeState = $rootNode->getState(); if (! $rootNode->isEmpty() && - ! in_array($rootNode->getState(), [Node::ICINGA_OK, Node::ICINGA_PENDING], true)) { - $count++; + ! in_array($nodeState, [Node::ICINGA_OK, Node::ICINGA_PENDING], true) + ) { + if ($nodeState === $state) { + $count++; + } elseif ($nodeState > $state) { + $count = 1; + $state = $nodeState; + } } } } catch (Throwable $e) { @@ -44,9 +65,9 @@ class ProcessProblemsBadge extends BadgeNavigationItemRenderer $this->count = $count; - $this->setState(self::STATE_CRITICAL); + $this->setState(self::NODE_STATE_TO_BADGE_STATE[$state] ?? self::STATE_UNKNOWN); $this->setTitle(sprintf( - tp('One unhandled root node critical', '%d unhandled root nodes critical', $count), + tp('One unhandled root node', '%d unhandled root nodes', $count), $count )); } diff --git a/library/Businessprocess/Web/Navigation/Renderer/ProcessesProblemsBadge.php b/library/Businessprocess/Web/Navigation/Renderer/ProcessesProblemsBadge.php index a1fc075..af2c63b 100644 --- a/library/Businessprocess/Web/Navigation/Renderer/ProcessesProblemsBadge.php +++ b/library/Businessprocess/Web/Navigation/Renderer/ProcessesProblemsBadge.php @@ -25,6 +25,7 @@ class ProcessesProblemsBadge extends BadgeNavigationItemRenderer { if ($this->count === null) { $count = 0; + $state = Node::ICINGA_OK; try { $storage = LegacyStorage::getInstance(); @@ -40,9 +41,17 @@ class ProcessesProblemsBadge extends BadgeNavigationItemRenderer } foreach ($bp->getRootNodes() as $rootNode) { + $nodeState = $rootNode->getState(); if (! $rootNode->isEmpty() && - ! in_array($rootNode->getState(), [Node::ICINGA_OK, Node::ICINGA_PENDING], true)) { - $count++; + ! in_array($nodeState, [Node::ICINGA_OK, Node::ICINGA_PENDING], true) + ) { + if ($nodeState === $state) { + $count++; + } elseif ($nodeState > $state) { + $count = 1; + $state = $nodeState; + } + break; } } @@ -52,7 +61,7 @@ class ProcessesProblemsBadge extends BadgeNavigationItemRenderer } $this->count = $count; - $this->setState(self::STATE_CRITICAL); + $this->setState(ProcessProblemsBadge::NODE_STATE_TO_BADGE_STATE[$state] ?? self::STATE_UNKNOWN); } return $this->count;