[icinga-checkins] icinga.org: icingaweb2/master: Let the SummaryNavigationItemRenderer show the worst state

git at icinga.org git at icinga.org
Thu Dec 8 10:40:33 CET 2016


Module: icingaweb2
Branch: master
Commit: b3bc1b6f81c2fe1ec2d23401872826e7499bdc93
URL:    https://git.icinga.org/?p=icingaweb2.git;a=commit;h=b3bc1b6f81c2fe1ec2d23401872826e7499bdc93

Author: Eric Lippmann <eric.lippmann at icinga.com>
Date:   Tue Dec  6 14:47:03 2016 +0100

Let the SummaryNavigationItemRenderer show the worst state

fixes #11185

---

 .../Renderer/SummaryNavigationItemRenderer.php     |   68 ++++++++++++++------
 1 file changed, 47 insertions(+), 21 deletions(-)

diff --git a/library/Icinga/Web/Navigation/Renderer/SummaryNavigationItemRenderer.php b/library/Icinga/Web/Navigation/Renderer/SummaryNavigationItemRenderer.php
index 1c6d230..2916f4e 100644
--- a/library/Icinga/Web/Navigation/Renderer/SummaryNavigationItemRenderer.php
+++ b/library/Icinga/Web/Navigation/Renderer/SummaryNavigationItemRenderer.php
@@ -3,44 +3,70 @@
 
 namespace Icinga\Web\Navigation\Renderer;
 
-use Icinga\Web\Navigation\Renderer\BadgeNavigationItemRenderer;
-
 /**
- * Summary badge adding up all badges in the navigation's children that have the same state
+ * Badge renderer summing up the worst state of its children
  */
 class SummaryNavigationItemRenderer extends BadgeNavigationItemRenderer
 {
     /**
-     * The title of each summarized child
+     * Cached count
+     *
+     * @var int
+     */
+    protected $count;
+
+    /**
+     * State to severity map
+     *
+     * @var array
+     */
+    protected static $stateSeverityMap = array(
+        self::STATE_OK          => 0,
+        self::STATE_PENDING     => 1,
+        self::STATE_UNKNOWN     => 2,
+        self::STATE_WARNING     => 3,
+        self::STATE_CRITICAL    => 4,
+    );
+
+    /**
+     * Severity to state map
      *
      * @var array
      */
-    protected $titles;
+    protected static $severityStateMap = array(
+        self::STATE_OK,
+        self::STATE_PENDING,
+        self::STATE_UNKNOWN,
+        self::STATE_WARNING,
+        self::STATE_CRITICAL
+    );
 
     /**
      * {@inheritdoc}
      */
     public function getCount()
     {
-        $count = 0;
-        foreach ($this->getItem()->getChildren() as $child) {
-            $renderer = $child->getRenderer();
-            if ($renderer instanceof BadgeNavigationItemRenderer) {
-                if ($renderer->getState() === $this->getState()) {
-                    $this->titles[] = $renderer->getTitle();
-                    $count += $renderer->getCount();
+        if ($this->count === null) {
+            $countMap = array_fill(0, 5, 0);
+            $maxSeverity = 0;
+            $titles = array();
+            foreach ($this->getItem()->getChildren() as $child) {
+                $renderer = $child->getRenderer();
+                if ($renderer instanceof BadgeNavigationItemRenderer) {
+                    $count = $renderer->getCount();
+                    if ($count) {
+                        $severity = static::$stateSeverityMap[$renderer->getState()];
+                        $countMap[$severity] += $count;
+                        $titles[] = $renderer->getTitle();
+                        $maxSeverity = max($maxSeverity, $severity);
+                    }
                 }
             }
+            $this->count = $countMap[$maxSeverity];
+            $this->state = static::$severityStateMap[$maxSeverity];
+            $this->title = implode('. ', $titles);
         }
 
-        return $count;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function getTitle()
-    {
-        return ! empty($this->titles) ? join(', ', $this->titles) : '';
+        return $this->count;
     }
 }



More information about the icinga-checkins mailing list