summaryrefslogtreecommitdiffstats
path: root/library/Cube/IcingaDb/CustomVariableDimension.php
blob: 226639580ca37b240a3cb269c6d020f8dd480389 (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
<?php

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

namespace Icinga\Module\Cube\IcingaDb;

use Icinga\Module\Cube\Cube;
use Icinga\Module\Cube\Dimension;
use Icinga\Module\Icingadb\Model\CustomvarFlat;
use ipl\Sql\Expression;
use ipl\Stdlib\Filter;

class CustomVariableDimension implements Dimension
{
    protected $name;

    protected $label;

    protected $wantNull = false;

    public function __construct($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }

    public function getLabel()
    {
        return $this->label ?: $this->getName();
    }

    /**
     * Set the label
     *
     * @param string $label
     * @return $this
     */
    public function setLabel($label)
    {
        $this->label = $label;

        return $this;
    }

    /**
     * Add a label
     *
     * @param string $label
     * @return $this
     */
    public function addLabel($label)
    {
        if ($this->label === null) {
            $this->setLabel($label);
        } else {
            $this->label .= ' & ' . $label;
        }

        return $this;
    }

    /**
     * Define whether null values should be shown
     *
     * @param bool $wantNull
     * @return $this
     */
    public function wantNull($wantNull = true)
    {
        $this->wantNull = $wantNull;

        return $this;
    }

    /**
     * @param IcingaDbCube $cube
     * @return Expression|string
     */
    public function getColumnExpression(Cube $cube)
    {
        $expression = $cube->getDb()->quoteIdentifier(['c_' . $this->getName(), 'flatvalue']);

        if ($this->wantNull) {
            return new Expression("COALESCE($expression, '-')");
        }

        return $expression;
    }

    public function addToCube(Cube $cube)
    {
        /** @var IcingaDbCube $cube */
        $name = $this->getName();
        $innerQuery = $cube->innerQuery();
        $sourceTable = $innerQuery->getModel()->getTableName();

        $subQuery = $innerQuery->createSubQuery(new CustomvarFlat(), $sourceTable . '.vars');
        $subQuery->getSelectBase()->resetWhere(); // The link to the outer query is the ON condition
        $subQuery->columns(['flatvalue', 'object_id' => $sourceTable . '.id']);
        $subQuery->filter(Filter::like('flatname', $name));

        // Values might not be unique (wildcard dimensions)
        $subQuery->getSelectBase()->groupBy([
            $subQuery->getResolver()->getAlias($subQuery->getModel()) . '.flatvalue',
            'object_id'
        ]);

        $subQueryAlias = $cube->getDb()->quoteIdentifier(['c_' . $name]);
        $innerQuery->getSelectBase()->groupBy($subQueryAlias . '.flatvalue');
        $innerQuery->getSelectBase()->join(
            [$subQueryAlias => $subQuery->assembleSelect()],
            [$subQueryAlias . '.object_id = ' . $innerQuery->getResolver()->getAlias($innerQuery->getModel()) . '.id']
        );
    }
}