summaryrefslogtreecommitdiffstats
path: root/library/Cube/IcingaDb/IcingaDbHostStatusCube.php
blob: 028d4d7d044f45917bacb0ad86ea7cc1e1e71be3 (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
<?php

// Icinga Web 2 Cube Module | (c) 2022 Icinga GmbH | GPLv2

namespace Icinga\Module\Cube\IcingaDb;

use Icinga\Module\Cube\CubeRenderer\HostStatusCubeRenderer;
use Icinga\Module\Icingadb\Model\CustomvarFlat;
use Icinga\Module\Icingadb\Model\Host;
use Icinga\Module\Icingadb\Model\HoststateSummary;
use ipl\Stdlib\Filter;

class IcingaDbHostStatusCube extends IcingaDbCube
{
    public function getRenderer()
    {
        return new HostStatusCubeRenderer($this);
    }

    public function getAvailableFactColumns()
    {
        return [
            'hosts_cnt' => 'hosts_total',
            'hosts_down' => 'hosts_down_handled + f.hosts_down_unhandled',
            'hosts_unhandled_down' => 'hosts_down_unhandled',
            'hosts_unreachable' => 'hosts_unreachable',
            'hosts_unhandled_unreachable' => 'hosts_unreachable_unhandled'
        ];
    }

    public function createDimension($name)
    {
        $this->registerAvailableDimensions();

        if (isset($this->availableDimensions[$name])) {
            return clone $this->availableDimensions[$name];
        }

        return new CustomVariableDimension($name);
    }

    public function listAvailableDimensions()
    {
        $db = $this->getDb();

        $query = CustomvarFlat::on($db);
        $this->applyRestrictions($query);

        $query
            ->columns('flatname')
            ->orderBy('flatname')
            ->filter(Filter::like('host.id', '*'));
        $query->getSelectBase()->groupBy('flatname');

        $dimensions = [];
        foreach ($query as $row) {
            // Replaces array index notations with [*] to get results for arbitrary indexes
            $name = preg_replace('/\\[\d+](?=\\.|$)/', '[*]', $row->flatname);
            $dimensions[$name] = $name;
        }

        return $dimensions;
    }

    public function prepareInnerQuery()
    {
        $query = HoststateSummary::on($this->getDb());
        $query->columns(array_diff_key($query->getModel()->getColumns(), (new Host())->getColumns()));
        $query->disableDefaultSort();
        $this->applyRestrictions($query);

        $this->innerQuery = $query;
        return $this->innerQuery;
    }

    /**
     * Return Filter for Hosts cube.
     *
     * @return Filter\Any|Filter\Chain
     */
    public function getObjectsFilter()
    {
        if ($this->objectsFilter === null) {
            $this->finalizeInnerQuery();

            $hosts = $this->innerQuery()->columns(['host' => 'host.name']);
            $hosts->getSelectBase()->resetGroupBy();

            $filter = Filter::any();

            foreach ($hosts as $object) {
                $filter->add(Filter::equal('host.name', $object->host));
            }

            $this->objectsFilter = $filter;
        }

        return $this->objectsFilter;
    }
}