summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/ProvidedHook/Reporting/ServiceSlaReport.php
blob: 46a068437ca825dd181cb75c05b851d71a36cc23 (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
<?php

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

namespace Icinga\Module\Icingadb\ProvidedHook\Reporting;

use Icinga\Application\Icinga;
use Icinga\Module\Icingadb\Model\Service;
use Icinga\Module\Reporting\ReportData;
use Icinga\Module\Reporting\ReportRow;
use Icinga\Module\Reporting\Timerange;
use ipl\Sql\Expression;
use ipl\Stdlib\Filter\Rule;

use function ipl\I18n\t;

class ServiceSlaReport extends SlaReport
{
    public function getName()
    {
        $name = t('Service SLA');
        if (Icinga::app()->getModuleManager()->hasEnabled('idoreports')) {
            $name .= ' (Icinga DB)';
        }

        return $name;
    }

    protected function createReportData()
    {
        return (new ReportData())
            ->setDimensions([t('Hostname'), t('Service Name')])
            ->setValues([t('SLA in %')]);
    }

    protected function createReportRow($row)
    {
        if ($row->sla === null) {
            return null;
        }

        return (new ReportRow())
            ->setDimensions([$row->host->display_name, $row->display_name])
            ->setValues([(float) $row->sla]);
    }

    protected function fetchSla(Timerange $timerange, Rule $filter = null)
    {
        $sla = Service::on($this->getDb())
            ->columns([
                'host.display_name',
                'display_name',
                'sla' => new Expression(sprintf(
                    "get_sla_ok_percent(%s, %s, '%s', '%s')",
                    'service.host_id',
                    'service.id',
                    $timerange->getStart()->format('Uv'),
                    $timerange->getEnd()->format('Uv')
                ))
            ]);

        $sla->resetOrderBy()->orderBy('host.display_name')->orderBy('display_name');

        $this->applyRestrictions($sla);

        if ($filter !== null) {
            $sla->filter($filter);
        }

        return $sla;
    }
}