summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Hook/Common/TotalSlaReportUtils.php
blob: 1006056ae9822f71492f707443e3ba6242b868ab (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
<?php

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

namespace Icinga\Module\Icingadb\Hook\Common;

use Icinga\Module\Icingadb\ProvidedHook\Reporting\HostSlaReport;
use Icinga\Module\Reporting\Timerange;
use ipl\Html\Html;
use ipl\Web\Widget\EmptyState;

use function ipl\I18n\t;

trait TotalSlaReportUtils
{
    public function getHtml(Timerange $timerange, array $config = null)
    {
        $data = $this->getData($timerange, $config);
        $count = $data->count();

        if (! $count) {
            return new EmptyState(t('No data found.'));
        }

        $threshold =  (float) ($config['threshold'] ?? static::DEFAULT_THRESHOLD);

        $tableRows = [];
        $precision = $config['sla_precision'] ?? static::DEFAULT_REPORT_PRECISION;

        // We only have one average
        $average = $data->getAverages()[0];

        if ($average < $threshold) {
            $slaClass = 'nok';
        } else {
            $slaClass = 'ok';
        }

        $total = $this instanceof HostSlaReport
            ? sprintf(t('Total (%d Hosts)'), $count)
            : sprintf(t('Total (%d Services)'), $count);

        $tableRows[] = Html::tag('tr', null, [
            Html::tag('td', ['colspan' => count($data->getDimensions())], $total),
            Html::tag('td', ['class' => "sla-column $slaClass"], round($average, $precision))
        ]);

        $table = Html::tag(
            'table',
            ['class' => 'common-table sla-table'],
            [Html::tag('tbody', null, $tableRows)]
        );

        return $table;
    }
}