-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathUnreachableParent.php
192 lines (174 loc) · 6.02 KB
/
UnreachableParent.php
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/* Icinga DB Web | (c) 2024 Icinga GmbH | GPLv2 */
namespace Icinga\Module\Icingadb\Model;
use Icinga\Module\Icingadb\Model\Behavior\ReRoute;
use InvalidArgumentException;
use ipl\Orm\Behavior\Binary;
use ipl\Orm\Behaviors;
use ipl\Orm\Model;
use ipl\Orm\Query;
use ipl\Orm\Relations;
use ipl\Sql\Connection;
use ipl\Sql\Expression;
use ipl\Sql\Select;
use ipl\Stdlib\Filter;
/**
* Class UnreachableParent
*
* @property string $id
* @property int $level
* @property string $child_id
* @property ?string $host_id
* @property ?string $service_id
* @property ?string $redundancy_group_id
* @property int $is_group_member
*
* @property (?Host)|Query $host
* @property (?Service)|Query $service
* @property (?RedundancyGroup)|Query $redundancy_group
*/
class UnreachableParent extends DependencyNode
{
public function getTableName(): string
{
return 'unreachable_parent';
}
public function getKeyName(): string
{
return 'id';
}
public function getColumns(): array
{
return [
'id',
'child_id',
'level',
'host_id',
'service_id',
'redundancy_group_id',
'is_group_member'
];
}
public function createRelations(Relations $relations): void
{
$relations->belongsTo('host', Host::class)
->setJoinType('LEFT');
$relations->belongsTo('service', Service::class)
->setJoinType('LEFT');
$relations->belongsTo('redundancy_group', RedundancyGroup::class)
->setJoinType('LEFT');
}
public function createBehaviors(Behaviors $behaviors): void
{
$behaviors->add(new Binary([
'id',
'host_id',
'service_id',
'redundancy_group_id'
]));
$behaviors->add(new ReRoute([
'hostgroup' => 'host.hostgroup',
'servicegroup' => 'service.servicegroup'
]));
}
public static function on(Connection $db, Model $root = null): Query
{
if ($root === null) {
throw new InvalidArgumentException('Root node must not be null');
}
$query = parent::on($db);
$query->getSelectBase()->with(
self::selectNodes($db, $root),
'unreachable_parent',
true
);
$query->filter(Filter::all(
Filter::greaterThan('level', 0),
Filter::equal('is_group_member', 0),
Filter::any(
Filter::equal('service.state.affects_children', 'y'),
Filter::all(
Filter::unlike('service_id', '*'),
Filter::equal('host.state.affects_children', 'y')
),
Filter::all(
Filter::equal('redundancy_group.state.failed', 'y'),
Filter::equal('redundancy_group.state.is_reachable', 'y')
)
)
));
return $query;
}
private static function selectNodes(Connection $db, Model $root): Select
{
$rootQuery = DependencyNode::on($db)
->columns([
'id' => 'id',
'child_id' => 'id',
'level' => new Expression('0'),
'host_id' => 'host_id',
'service_id' => new Expression("COALESCE(%s, CAST('' as binary(20)))", ['service_id']),
'redundancy_group_id' => new Expression("CAST('' as binary(20))"),
'is_group_member' => new Expression('0')
]);
if ($root instanceof Host) {
$rootQuery->filter(Filter::all(
Filter::equal('host_id', $root->id),
Filter::unlike('service_id', '*')
));
} elseif ($root instanceof Service) {
$rootQuery->filter(Filter::all(
Filter::equal('host_id', $root->host_id),
Filter::equal('service_id', $root->id)
));
} elseif ($root instanceof RedundancyGroup) {
$rootQuery->filter(Filter::all(Filter::equal('redundancy_group_id', $root->id)));
} else {
throw new InvalidArgumentException('Root node must be either a host, service or a redundancy group');
}
$nodeQuery = DependencyEdge::on($db)
->columns([
'id' => 'to_node_id',
'child_id' => 'from_node_id',
'level' => new Expression('urn.level + 1'),
'host_id' => 'to.host_id',
'service_id' => 'to.service_id',
'redundancy_group_id' => 'to.redundancy_group_id',
'is_group_member' => new Expression('urn.redundancy_group_id IS NOT NULL AND urn.level > 0')
]);
$nodeQuery->filter(Filter::any(
Filter::equal('state.failed', 'y'),
Filter::equal('to.redundancy_group.state.failed', 'y')
));
$nodeSelect = $nodeQuery->assembleSelect();
// TODO: ipl-orm doesn't preserve key order :'(
$columnsProperty = (new \ReflectionClass($nodeSelect))->getProperty('columns');
$columnsProperty->setAccessible(true);
$columnsProperty->setValue($nodeSelect, array_merge(
[
'id' => null,
'child_id' => null,
'level' => null,
'host_id' => null,
'service_id' => null,
'redundancy_group_id' => null,
'is_group_member' => null
],
$nodeSelect->getColumns()
));
return $rootQuery->assembleSelect()->union(
$nodeSelect
->join(['urn' => 'unreachable_parent'], sprintf(
'urn.id = %s',
$nodeQuery
->getResolver()
->qualifyColumn(
'from_node_id',
$nodeQuery
->getResolver()
->getAlias($nodeQuery->getModel())
)
))
);
}
}