summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Web/View/AppHealth.php
blob: c66ca05fd35546f2c56b85237a9b46dbaf725569 (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
<?php
/* Icinga Web 2 | (c) 2021 Icinga GmbH | GPLv2+ */

namespace Icinga\Web\View;

use Icinga\Application\Hook\HealthHook;
use ipl\Html\FormattedString;
use ipl\Html\HtmlElement;
use ipl\Html\Table;
use ipl\Web\Common\BaseTarget;
use ipl\Web\Widget\Link;
use Traversable;

class AppHealth extends Table
{
    use BaseTarget;

    protected $defaultAttributes = ['class' => ['app-health', 'common-table', 'table-row-selectable']];

    /** @var Traversable */
    protected $data;

    public function __construct(Traversable $data)
    {
        $this->data = $data;

        $this->setBaseTarget('_next');
    }

    protected function assemble()
    {
        foreach ($this->data as $row) {
            $this->add(Table::tr([
                Table::th(HtmlElement::create('span', ['class' => [
                    'ball',
                    'ball-size-xl',
                    $this->getStateClass($row->state)
                ]])),
                Table::td([
                    new HtmlElement('header', null, FormattedString::create(
                        t('%s by %s is %s', '<check> by <module> is <state-text>'),
                        $row->url
                            ? new Link(HtmlElement::create('span', null, $row->name), $row->url)
                            : HtmlElement::create('span', null, $row->name),
                        HtmlElement::create('span', null, $row->module),
                        HtmlElement::create('span', null, $this->getStateText($row->state))
                    )),
                    HtmlElement::create('section', null, $row->message)
                ])
            ]));
        }
    }

    protected function getStateClass($state)
    {
        if ($state === null) {
            $state = HealthHook::STATE_UNKNOWN;
        }

        switch ($state) {
            case HealthHook::STATE_OK:
                return 'state-ok';
            case HealthHook::STATE_WARNING:
                return 'state-warning';
            case HealthHook::STATE_CRITICAL:
                return 'state-critical';
            case HealthHook::STATE_UNKNOWN:
                return 'state-unknown';
        }
    }

    protected function getStateText($state)
    {
        if ($state === null) {
            $state = t('UNKNOWN');
        }

        switch ($state) {
            case HealthHook::STATE_OK:
                return t('OK');
            case HealthHook::STATE_WARNING:
                return t('WARNING');
            case HealthHook::STATE_CRITICAL:
                return t('CRITICAL');
            case HealthHook::STATE_UNKNOWN:
                return t('UNKNOWN');
        }
    }
}