summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Web/Navigation/Renderer/TotalProblemsBadge.php
blob: 703db653f5984327670bb1106e49b1f2a6e811b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php

/* Icinga DB Web | (c) 2021 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Web\Navigation\Renderer;

use Icinga\Web\Navigation\Renderer\BadgeNavigationItemRenderer;

class TotalProblemsBadge extends BadgeNavigationItemRenderer
{
    /**
     * Cached count
     *
     * @var int
     */
    protected $count;

    /**
     * State to severity map
     *
     * @var array
     */
    protected static $stateSeverityMap = [
        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 static $severityStateMap = [
        self::STATE_OK,
        self::STATE_PENDING,
        self::STATE_UNKNOWN,
        self::STATE_WARNING,
        self::STATE_CRITICAL
    ];

    public function getCount()
    {
        if ($this->count === null) {
            $countMap = array_fill(0, 5, 0);
            $maxSeverity = 0;
            foreach ($this->getItem()->getChildren() as $child) {
                $renderer = $child->getRenderer();
                if ($renderer instanceof ProblemsBadge) {
                    $count = $renderer->getProblemsCount();
                    if ($count) {
                        $severity = static::$stateSeverityMap[$renderer->getState()];
                        $countMap[$severity] += $count;
                        $maxSeverity = max($maxSeverity, $severity);
                    }
                }
            }
            $this->count = $countMap[$maxSeverity];
            $this->state = static::$severityStateMap[$maxSeverity];
        }

        return $this->count;
    }
}