-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathCustomVarDimension.php
More file actions
147 lines (125 loc) · 3.34 KB
/
Copy pathCustomVarDimension.php
File metadata and controls
147 lines (125 loc) · 3.34 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
// SPDX-FileCopyrightText: 2018 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-3.0-or-later
namespace Icinga\Module\Cube\Ido;
use Icinga\Module\Cube\Cube;
use Icinga\Module\Cube\Dimension;
/**
* CustomVarDimension
*
* This provides dimenstions for custom variables available in the IDO
*
* TODO: create safe aliases for special characters
*
* @package Icinga\Module\Cube\Ido
*/
class CustomVarDimension implements Dimension
{
public const TYPE_HOST = 'host';
public const TYPE_SERVICE = 'service';
/**
* @var string custom variable name
*/
protected $varName;
/**
* @var string custom variable label
*/
protected $varLabel;
/**
* @var bool Whether null values should be shown
*/
protected $wantNull = false;
/** @var string Type of the custom var */
protected $type;
/**
* CustomVarDimension constructor.
*
* @param $varName
* @param string $type Type of the custom var
*/
public function __construct($varName, $type = null)
{
$this->varName = $varName;
$this->type = $type;
}
/**
* Define whether null values should be shown
*
* @param bool $wantNull
* @return $this
*/
public function wantNull($wantNull = true)
{
$this->wantNull = $wantNull;
return $this;
}
/**
* @return string
*/
public function getName()
{
return $this->varName;
}
/**
* @return string
*/
public function getLabel()
{
return $this->varLabel ?: $this->getName();
}
public function setLabel($label)
{
$this->varLabel = $label;
return $this;
}
public function addLabel($label)
{
if ($this->varLabel === null) {
$this->setLabel($label);
} else {
$this->varLabel .= ' & ' . $label;
}
return $this;
}
public function getColumnExpression(Cube $cube)
{
/** @var IdoCube $cube */
if ($this->wantNull) {
return 'COALESCE(' . $cube->db()->quoteIdentifier(['c_' . $this->varName, 'varvalue']) . ", '-')";
} else {
return $cube->db()->quoteIdentifier(['c_' . $this->varName, 'varvalue']);
}
}
protected function safeVarname($name)
{
return $name;
}
public function addToCube(Cube $cube)
{
switch ($this->type) {
case self::TYPE_HOST:
$objectId = 'ho.object_id';
break;
case self::TYPE_SERVICE:
$objectId = 'so.object_id';
break;
default:
$objectId = 'o.object_id';
}
$name = $this->safeVarname($this->varName);
/** @var IdoCube $cube */
$alias = $cube->db()->quoteIdentifier(['c_' . $name]);
if ($cube->isPgsql()) {
$on = "LOWER($alias.varname) = ?";
$name = strtolower($name);
} else {
$on = $alias . '.varname = ? COLLATE latin1_general_ci';
}
$cube->innerQuery()->joinLeft(
array($alias => $cube->tableName('icinga_customvariablestatus')),
$cube->db()->quoteInto($on, $name)
. ' AND ' . $alias . '.object_id = ' . $objectId,
array()
)->group($alias . '.varvalue');
}
}