summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Widget/ItemList/ServiceListItemDetailed.php
blob: 16135998b952360318c1ac612891af4699d1dd02 (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
<?php

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

namespace Icinga\Module\Icingadb\Widget\ItemList;

use Icinga\Module\Icingadb\Common\ListItemDetailedLayout;
use Icinga\Module\Icingadb\Util\PerfDataSet;
use Icinga\Module\Icingadb\Widget\ItemList\CommentList;
use ipl\Html\Attributes;
use ipl\Html\BaseHtmlElement;
use ipl\Html\HtmlElement;
use ipl\Html\HtmlString;
use ipl\Html\Text;
use ipl\Web\Widget\Icon;
use ipl\Web\Widget\StateBall;

class ServiceListItemDetailed extends BaseServiceListItem
{
    use ListItemDetailedLayout;

    /** @var int Max pie charts to be shown */
    const PIE_CHART_LIMIT = 5;

    protected function getStateBallSize(): string
    {
        return StateBall::SIZE_LARGE;
    }

    protected function assembleFooter(BaseHtmlElement $footer): void
    {
        $statusIcons = new HtmlElement('div', Attributes::create(['class' => 'status-icons']));

        if ($this->item->state->last_comment->service_id === $this->item->id) {
            $comment = $this->item->state->last_comment;
            $comment->service = $this->item;
            $comment = (new CommentList([$comment]))
                ->setNoSubjectLink()
                ->setObjectLinkDisabled()
                ->setDetailActionsDisabled();

            $statusIcons->addHtml(
                new HtmlElement(
                    'div',
                    Attributes::create(['class' => 'comment-wrapper']),
                    new HtmlElement('div', Attributes::create(['class' => 'comment-popup']), $comment),
                    (new Icon('comments', ['class' => 'comment-icon']))
                )
            );
        }

        if ($this->item->state->is_flapping) {
            $statusIcons->addHtml(new Icon(
                'random',
                [
                    'title' => sprintf(
                        t('Service "%s" on "%s" is in flapping state'),
                        $this->item->display_name,
                        $this->item->host->display_name
                    ),
                ]
            ));
        }

        if (! $this->item->notifications_enabled) {
            $statusIcons->addHtml(new Icon('bell-slash', ['title' => t('Notifications disabled')]));
        }

        if (! $this->item->active_checks_enabled) {
            $statusIcons->addHtml(new Icon('eye-slash', ['title' => t('Active checks disabled')]));
        }

        $performanceData = new HtmlElement('div', Attributes::create(['class' => 'performance-data']));
        if ($this->item->state->performance_data) {
            $pieChartData = PerfDataSet::fromString($this->item->state->normalized_performance_data)->asArray();

            $pies = [];
            foreach ($pieChartData as $i => $perfdata) {
                if ($perfdata->isVisualizable()) {
                    $pies[] = $perfdata->asInlinePie()->render();
                }

                // Check if number of visualizable pie charts is larger than PIE_CHART_LIMIT
                if (count($pies) > ServiceListItemDetailed::PIE_CHART_LIMIT) {
                    break;
                }
            }

            $maxVisiblePies = ServiceListItemDetailed::PIE_CHART_LIMIT - 2;
            $numOfPies = count($pies);
            foreach ($pies as $i => $pie) {
                if (
                    // Show max. 5 elements: if there are more than 5, show 4 + `…`
                    $i > $maxVisiblePies && $numOfPies > ServiceListItemDetailed::PIE_CHART_LIMIT
                ) {
                    $performanceData->addHtml(new HtmlElement('span', null, Text::create('…')));
                    break;
                }

                $performanceData->addHtml(HtmlString::create($pie));
            }
        }

        if (! $statusIcons->isEmpty()) {
            $footer->addHtml($statusIcons);
        }

        if (! $performanceData->isEmpty()) {
            $footer->addHtml($performanceData);
        }
    }
}