summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Widget/ItemTable/StateRowItem.php
blob: f62286b74ddfa90a4e0c4ee6fceaa1872ca525b7 (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
<?php

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

namespace Icinga\Module\Icingadb\Widget\ItemTable;

use Icinga\Module\Icingadb\Common\HostStates;
use Icinga\Module\Icingadb\Common\ServiceStates;
use Icinga\Module\Icingadb\Model\Host;
use Icinga\Module\Icingadb\Util\PerfDataSet;
use Icinga\Module\Icingadb\Util\PluginOutput;
use Icinga\Module\Icingadb\Widget\CheckAttempt;
use Icinga\Module\Icingadb\Widget\IconImage;
use Icinga\Module\Icingadb\Widget\PluginOutputContainer;
use ipl\Html\Attributes;
use ipl\Html\BaseHtmlElement;
use ipl\Html\HtmlElement;
use ipl\Html\HtmlString;
use ipl\Html\Text;
use ipl\Web\Widget\EmptyState;
use ipl\Web\Widget\Icon;
use ipl\Web\Widget\StateBall;
use ipl\Web\Widget\TimeSince;
use ipl\Web\Widget\TimeUntil;

abstract class StateRowItem extends BaseStateRowItem
{
    /** @var StateItemTable */
    protected $list;

    protected function assembleVisual(BaseHtmlElement $visual)
    {
        $stateBall = new StateBall($this->item->state->getStateText(), StateBall::SIZE_LARGE);
        $stateBall->add($this->item->state->getIcon());

        if ($this->item->state->is_handled) {
            $stateBall->getAttributes()->add('class', 'handled');
        }

        $visual->addHtml($stateBall);
        if ($this->item->state->state_type === 'soft') {
            $visual->addHtml(new CheckAttempt(
                (int) $this->item->state->check_attempt,
                (int) $this->item->max_check_attempts
            ));
        }
    }

    protected function assembleCell(BaseHtmlElement $cell, string $path, $value)
    {
        switch (true) {
            case $path === 'state.output':
            case $path === 'state.long_output':
                if (empty($value)) {
                    $pluginOutput = new EmptyState(t('Output unavailable.'));
                } else {
                    $pluginOutput = new PluginOutputContainer(PluginOutput::fromObject($this->item));
                }

                $cell->addHtml($pluginOutput)
                    ->getAttributes()
                    ->add('class', 'has-plugin-output');
                break;
            case $path === 'state.soft_state':
            case $path === 'state.hard_state':
            case $path === 'state.previous_soft_state':
            case $path === 'state.previous_hard_state':
                $stateType = substr($path, 6);
                if ($this->item instanceof Host) {
                    $stateName = HostStates::translated($this->item->state->$stateType);
                } else {
                    $stateName = ServiceStates::translated($this->item->state->$stateType);
                }

                $cell->addHtml(Text::create($stateName));
                break;
            case $path === 'state.last_update':
            case $path === 'state.last_state_change':
                $column = substr($path, 6);
                $cell->addHtml(new TimeSince($this->item->state->$column->getTimestamp()));
                break;
            case $path === 'state.next_check':
            case $path === 'state.next_update':
                $column = substr($path, 6);
                $cell->addHtml(new TimeUntil($this->item->state->$column->getTimestamp()));
                break;
            case $path === 'state.performance_data':
            case $path === 'state.normalized_performance_data':
                $perfdataContainer = new HtmlElement('div', Attributes::create(['class' => 'performance-data']));

                $pieChartData = PerfDataSet::fromString($this->item->state->normalized_performance_data)->asArray();
                foreach ($pieChartData as $perfdata) {
                    if ($perfdata->isVisualizable()) {
                        $perfdataContainer->addHtml(new HtmlString($perfdata->asInlinePie()->render()));
                    }
                }

                $cell->addHtml($perfdataContainer)
                    ->getAttributes()
                    ->add('class', 'has-performance-data');
                break;
            case $path === 'is_volatile':
            case $path === 'host.is_volatile':
            case substr($path, -8) == '_enabled':
            case (bool) preg_match('/state\.(is_|in_)/', $path):
                if ($value) {
                    $cell->addHtml(new Icon('check'));
                }

                break;
            case $path === 'icon_image.icon_image':
                $cell->addHtml(new IconImage($value, $this->item->icon_image_alt))
                    ->getAttributes()
                    ->add('class', 'has-icon-images');
                break;
            default:
                if (preg_match('/(^id|_id|.id|_checksum|_bin)$/', $path)) {
                    $value = bin2hex($value);
                }

                $cell->addHtml(Text::create($value));
        }
    }
}