summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Web/Navigation/Renderer/ProblemsBadge.php
blob: 5e0f55576d9736abe57423fcc2b6778a958fa68c (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php

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

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

use Exception;
use Icinga\Application\Logger;
use Icinga\Module\Icingadb\Common\Database;
use Icinga\Web\Navigation\NavigationItem;
use Icinga\Web\Navigation\Renderer\NavigationItemRenderer;
use ipl\Html\HtmlDocument;
use ipl\Html\HtmlString;
use ipl\Web\Widget\Link;
use ipl\Web\Widget\StateBadge;

abstract class ProblemsBadge extends NavigationItemRenderer
{
    use Database;

    const STATE_CRITICAL = 'critical';
    const STATE_UNKNOWN = 'unknown';

    /** @var int Count cache */
    protected $count;

    /** @var string State text */
    protected $state;

    /** @var string Title */
    protected $title;

    protected $linkDisabled;

    abstract protected function fetchProblemsCount();

    abstract protected function getUrl();

    public function getProblemsCount()
    {
        if ($this->count === null) {
            try {
                $count = $this->fetchProblemsCount();
            } catch (Exception $e) {
                Logger::debug($e);

                $this->count = 1;

                $this->setState(static::STATE_UNKNOWN);
                $this->setTitle($e->getMessage());

                return $this->count;
            }

            $this->count = $count;

            $this->setState(static::STATE_CRITICAL);
        }

        return $this->count;
    }

    /**
     * Set the state text
     *
     * @param string $state
     *
     * @return $this
     */
    public function setState(string $state): self
    {
        $this->state = $state;

        return $this;
    }

    /**
     * Get the state text
     *
     * @return string
     */
    public function getState(): string
    {
        if ($this->state === null) {
            throw new \LogicException(
                'You are accessing an unset property. Please make sure to set it beforehand.'
            );
        }

        return $this->state;
    }

    /**
     * Set the title
     *
     * @param string $title
     *
     * @return $this
     */
    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }

    /**
     * Get the title
     *
     * @return ?string
     */
    public function getTitle()
    {
        return $this->title;
    }

    public function render(NavigationItem $item = null): string
    {
        if ($item === null) {
            $item = $this->getItem();
        }

        $item->setCssClass('badge-nav-item icinga-module module-icingadb');

        $html = new HtmlDocument();

        $badge = $this->createBadge();
        if ($badge !== null) {
            if ($this->linkDisabled) {
                $badge->addAttributes(['class' => 'disabled']);
                $this->setEscapeLabel(false);
                $label = $this->view()->escape($item->getLabel());
                $item->setLabel($badge . $label);
            } else {
                $html->add(new Link($badge, $this->getUrl(), ['title' => $this->getTitle()]));
            }
        }

        return $html
            ->prepend(new HtmlString(parent::render($item)))
            ->render();
    }

    protected function createBadge()
    {
        $count = $this->getProblemsCount();

        if ($count) {
            return (new StateBadge($this->round($count), $this->getState()))
                    ->addAttributes(['class' => 'badge', 'title' => $this->getTitle()]);
        }

        return null;
    }

    protected function round($count)
    {
        if ($count > 1000000) {
            $count = round($count, -6) / 1000000 . 'M';
        } elseif ($count > 1000) {
            $count = round($count, -3) / 1000 . 'k';
        }

        return $count;
    }

    public function disableLink()
    {
        $this->linkDisabled = true;

        return $this;
    }
}