|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the `tvi/monitor-bundle` project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) https://github.com/turnaev/monitor-bundle/graphs/contributors |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace Tvi\MonitorBundle\Check; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
|
|
|
|
|
17
|
|
|
* @author Vladimir Turnaev <[email protected]> |
|
18
|
|
|
*/ |
|
|
|
|
|
|
19
|
|
|
class CheckManager implements \ArrayAccess, \Iterator, \Countable |
|
20
|
|
|
{ |
|
21
|
|
|
use ContainerAwareTrait; |
|
22
|
|
|
use CheckArraybleTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
|
|
|
|
|
25
|
|
|
* @var Group[] |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $groups = []; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
|
|
|
|
|
30
|
|
|
* @var Group[] |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $tags = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
|
|
|
|
|
35
|
|
|
* @param ?string|string[] $tags |
|
|
|
|
|
|
36
|
|
|
* @param ?string|string[] $groups |
|
|
|
|
|
|
37
|
|
|
* @param null|mixed $ids |
|
|
|
|
|
|
38
|
|
|
* |
|
39
|
|
|
* @return Tag[] |
|
40
|
|
|
*/ |
|
41
|
74 |
|
public function findChecks($ids = null, $groups = null, $tags = null): array |
|
42
|
|
|
{ |
|
43
|
74 |
|
$ids = (array) (null === $ids ? [] : (\is_string($ids) ? [$ids] : $ids)); |
|
44
|
74 |
|
$groups = (array) (null === $groups ? [] : (\is_string($groups) ? [$groups] : $groups)); |
|
45
|
74 |
|
$tags = (array) (null === $tags ? [] : (\is_string($tags) ? [$tags] : $tags)); |
|
46
|
|
|
|
|
47
|
74 |
|
$check = array_filter($this->toArray(), static function (CheckInterface $c) use ($ids, $groups, $tags) { |
|
|
|
|
|
|
48
|
73 |
|
$inIds = ($ids) ? \in_array($c->getId(), $ids, true) : true; |
|
49
|
73 |
|
$inGroups = ($groups) ? \in_array($c->getGroup(), $groups, true) : true; |
|
50
|
73 |
|
$inTags = ($tags) ? (bool) array_intersect($c->getTags(), $tags) : true; |
|
51
|
|
|
|
|
52
|
73 |
|
return $inIds && $inGroups && $inTags; |
|
53
|
74 |
|
}); |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
74 |
|
return $check; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
|
|
|
|
|
59
|
|
|
* @param ?string|string[] $groups |
|
|
|
|
|
|
60
|
|
|
* |
|
61
|
|
|
* @return Group[] |
|
62
|
|
|
*/ |
|
63
|
12 |
|
public function findGroups($groups = null): array |
|
64
|
|
|
{ |
|
65
|
12 |
|
if ($groups) { |
|
66
|
10 |
|
$groups = \is_string($groups) ? [$groups] : $groups; |
|
67
|
|
|
|
|
68
|
10 |
|
return array_filter($this->groups, static function ($t) use ($groups) { |
|
|
|
|
|
|
69
|
10 |
|
return \in_array($t->getName(), $groups, true); |
|
70
|
10 |
|
}); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
3 |
|
return $this->groups; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
|
|
|
|
|
77
|
|
|
* @param ?string|string[] $tags |
|
|
|
|
|
|
78
|
|
|
* |
|
79
|
|
|
* @return Group[] |
|
80
|
|
|
*/ |
|
81
|
15 |
|
public function findTags($tags = null): array |
|
82
|
|
|
{ |
|
83
|
15 |
|
if ($tags) { |
|
84
|
12 |
|
$tags = \is_string($tags) ? [$tags] : $tags; |
|
85
|
|
|
|
|
86
|
12 |
|
return array_filter($this->tags, static function ($t) use ($tags) { |
|
87
|
12 |
|
return \in_array($t->getName(), $tags, true); |
|
88
|
12 |
|
}); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
5 |
|
return $this->tags; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
137 |
|
public function add(array $checkConfs, array $tagConfs, array $groupConfs) |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
137 |
|
$this->addTags($tagConfs); |
|
97
|
137 |
|
$this->addGroups($groupConfs); |
|
98
|
137 |
|
$this->addChecks($checkConfs); |
|
99
|
137 |
|
} |
|
100
|
|
|
|
|
101
|
99 |
|
public function addIfTag(Tag $tag): Tag |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
99 |
|
return empty($this->tags[$tag->getName()]) ? $this->tags[$tag->getName()] = $tag : $this->tags[$tag->getName()]; |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
132 |
|
public function addIfGroup(Group $group): Group |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
132 |
|
return empty($this->groups[$group->getName()]) ? $this->groups[$group->getName()] = $group : $this->groups[$group->getName()]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function addIfCheck(CheckInterface $check) |
|
|
|
|
|
|
112
|
|
|
{ |
|
113
|
|
|
$this->checks[$check->getId()] = $check; |
|
114
|
|
|
|
|
115
|
|
|
foreach ($check->getTags() as $tag) { |
|
116
|
|
|
$tag = $this->addIfTag(new Tag($tag)); |
|
117
|
|
|
$tag->addCheck($check->getId(), $this->checks[$check->getId()]); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$group = $this->addIfGroup(new Group($check->getGroup())); |
|
121
|
|
|
$group->addCheck($check->getId(), $this->checks[$check->getId()]); |
|
122
|
|
|
|
|
123
|
|
|
$this->groups[$group->getId()] = $group; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
137 |
|
protected function addTags(array $tagConfs) |
|
|
|
|
|
|
127
|
|
|
{ |
|
128
|
137 |
|
foreach ($tagConfs as $id => $setting) { |
|
129
|
3 |
|
$tag = new Tag($id, !empty($setting['name']) ? $setting['name'] : null, !empty($setting['descr']) ? $setting['descr'] : null); |
|
130
|
3 |
|
$this->addIfTag($tag); |
|
131
|
|
|
} |
|
132
|
137 |
|
} |
|
133
|
|
|
|
|
134
|
137 |
|
protected function addGroups(array $gropConfs) |
|
|
|
|
|
|
135
|
|
|
{ |
|
136
|
137 |
|
foreach ($gropConfs as $id => $setting) { |
|
137
|
1 |
|
$group = new Group($id, !empty($setting['name']) ? $setting['name'] : null, !empty($setting['descr']) ? $setting['descr'] : null); |
|
138
|
1 |
|
$this->addIfGroup($group); |
|
139
|
|
|
} |
|
140
|
137 |
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
|
|
|
|
|
143
|
|
|
* @param array $checkConfs |
|
|
|
|
|
|
144
|
|
|
*/ |
|
|
|
|
|
|
145
|
137 |
|
protected function addChecks($checkConfs) |
|
146
|
|
|
{ |
|
147
|
137 |
|
foreach ($checkConfs as $id => $setting) { |
|
148
|
132 |
|
$serviceId = $setting['serviceId']; |
|
149
|
132 |
|
$checkProxy = new Proxy(function () use ($serviceId, $id) { |
|
|
|
|
|
|
150
|
113 |
|
$this->checks[$id] = $this->container->get($serviceId); |
|
151
|
|
|
|
|
152
|
113 |
|
return $this->checks[$id]; |
|
153
|
132 |
|
}); |
|
|
|
|
|
|
154
|
|
|
|
|
155
|
132 |
|
$this->checks[$id] = $checkProxy; |
|
156
|
|
|
|
|
157
|
132 |
|
foreach ($setting['tags'] as $tagId) { |
|
158
|
98 |
|
$tag = $this->addIfTag(new Tag($tagId)); |
|
159
|
98 |
|
$tag->addCheck($id, $this->checks[$id]); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
132 |
|
$group = $this->addIfGroup(new Group($setting['group'])); |
|
163
|
132 |
|
$group->addCheck($id, $this->checks[$id]); |
|
164
|
|
|
|
|
165
|
132 |
|
$this->groups[$group->getId()] = $group; |
|
166
|
|
|
} |
|
167
|
137 |
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|