summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Widget/Detail/PerfDataTable.php
blob: aaee7c95e7ba7c3a1cc251fffa0a7f8097a4c738 (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
<?php

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

namespace Icinga\Module\Icingadb\Widget\Detail;

use Icinga\Module\Icingadb\Util\PerfData;
use Icinga\Module\Icingadb\Util\PerfDataSet;
use ipl\Html\Attributes;
use ipl\Html\HtmlElement;
use ipl\Html\HtmlString;
use ipl\Html\Table;
use ipl\Html\Text;
use ipl\I18n\Translation;
use ipl\Web\Widget\EmptyState;
use ipl\Web\Widget\Icon;

class PerfDataTable extends Table
{
    use Translation;

    protected $defaultAttributes = [
        'class' => 'performance-data-table collapsible',
        'data-visible-rows' => 6
    ];

    /** @var string The perfdata string  */
    protected $perfdataStr;

    /** @var int Max labels to show; 0 for no limit */
    protected $limit;

    /** @var string The color indicating the perfdata state */
    protected $color;

    /**
     * Display the given perfdata string to the user
     *
     * @param   string  $perfdataStr    The perfdata string
     * @param   int     $limit          Max labels to show; 0 for no limit
     * @param   string  $color          The color indicating the perfdata state
     */
    public function __construct(string $perfdataStr, int $limit = 0, string $color = PerfData::PERFDATA_OK)
    {
        $this->perfdataStr = $perfdataStr;
        $this->limit = $limit;
        $this->color = $color;
    }

    public function assemble()
    {
        $pieChartData = PerfDataSet::fromString($this->perfdataStr)->asArray();
        $keys = [
            ''      => '',
            'label' => t('Label'),
            'value' => t('Value'),
            'min'   => t('Min'),
            'max'   => t('Max'),
            'warn'  => t('Warning'),
            'crit'  => t('Critical')
        ];

        $containsSparkline = false;
        foreach ($pieChartData as $perfdata) {
            if ($perfdata->isVisualizable()  || ! $perfdata->isValid()) {
                $containsSparkline = true;
                break;
            }
        }

        $headerRow = new HtmlElement('tr');
        foreach ($keys as $key => $col) {
            if (! $containsSparkline && $key === '') {
                continue;
            }

            $headerRow->addHtml(new HtmlElement('th', Attributes::create([
                'class' => $key === 'label' ? 'title' : null
            ]), Text::create($col)));
        }

        $this->getHeader()->addHtml($headerRow);

        $count = 0;
        foreach ($pieChartData as $perfdata) {
            if ($this->limit > 0 && $count === $this->limit) {
                break;
            }

            $count++;
            $cols = [];
            if ($containsSparkline) {
                if ($perfdata->isVisualizable()) {
                    $cols[] = Table::td(
                        HtmlString::create($perfdata->asInlinePie($this->color)->render()),
                        ['class' => 'sparkline-col']
                    );
                } elseif (! $perfdata->isValid()) {
                    $cols[] = Table::td(
                        new Icon(
                            'triangle-exclamation',
                            [
                                'title' => $this->translate(
                                    'Evaluation failed. Performance data is invalid.'
                                ),
                                'class' => ['invalid-perfdata']
                            ]
                        ),
                        ['class' => 'sparkline-col']
                    );
                } else {
                    $cols[] = Table::td('');
                }
            }

            foreach ($perfdata->toArray() as $column => $value) {
                $cols[] = Table::td(
                    new HtmlElement(
                        'span',
                        Attributes::create(['class' => $value ? null : 'no-value']),
                        $value ? Text::create($value) : new EmptyState(t('None', 'value'))
                    ),
                    ['class' => $column === 'label' ? 'title' : null]
                );
            }

            $this->addHtml(Table::tr($cols));
        }
    }
}