1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Algatux\InfluxDbBundle\Events\Listeners; |
6
|
|
|
|
7
|
|
|
use Algatux\InfluxDbBundle\Events\AbstractDeferredInfluxDbEvent; |
8
|
|
|
use Algatux\InfluxDbBundle\Events\AbstractInfluxDbEvent; |
9
|
|
|
use Algatux\InfluxDbBundle\Events\DeferredUdpEvent; |
10
|
|
|
use Algatux\InfluxDbBundle\Events\UdpEvent; |
11
|
|
|
use InfluxDB\Database; |
12
|
|
|
use InfluxDB\Point; |
13
|
|
|
use Symfony\Component\EventDispatcher\Event; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @internal |
17
|
|
|
*/ |
18
|
|
|
final class InfluxDbEventListener |
19
|
|
|
{ |
20
|
|
|
const STORAGE_KEY_UDP = 'udp'; |
21
|
|
|
const STORAGE_KEY_HTTP = 'http'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $connection; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var bool |
30
|
|
|
*/ |
31
|
|
|
private $isDefault; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Database |
35
|
|
|
*/ |
36
|
|
|
private $httpDatabase; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var Database |
40
|
|
|
*/ |
41
|
|
|
private $udpDatabase; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
private $storage; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $connection |
50
|
|
|
* @param bool $isDefault |
51
|
|
|
* @param Database $httpDatabase |
52
|
|
|
* @param Database $udpDatabase |
53
|
|
|
*/ |
54
|
7 |
|
public function __construct(string $connection, bool $isDefault, Database $httpDatabase, Database $udpDatabase = null) |
55
|
|
|
{ |
56
|
7 |
|
$this->connection = $connection; |
57
|
7 |
|
$this->isDefault = $isDefault; |
58
|
7 |
|
$this->httpDatabase = $httpDatabase; |
59
|
7 |
|
$this->udpDatabase = $udpDatabase; |
60
|
7 |
|
$this->initStorage(); |
61
|
7 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param AbstractInfluxDbEvent $event |
65
|
|
|
* |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
7 |
|
public function onPointsCollected(AbstractInfluxDbEvent $event): bool |
69
|
|
|
{ |
70
|
7 |
|
if (!$this->isConcerned($event)) { |
71
|
1 |
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
6 |
|
if (!$this->udpDatabase && ($event instanceof UdpEvent || $event instanceof DeferredUdpEvent)) { |
75
|
2 |
|
throw new \RuntimeException( |
76
|
2 |
|
'No UDP connection available for database "'.$this->httpDatabase->getName().'". ' |
77
|
2 |
|
.'You must enable it on the configuration to use it.' |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
4 |
|
$points = $event->getPoints(); |
82
|
4 |
|
$precision = $event->getPrecision(); |
83
|
|
|
|
84
|
4 |
|
if ($event instanceof AbstractDeferredInfluxDbEvent) { |
85
|
2 |
|
$typeKey = $event instanceof DeferredUdpEvent ? static::STORAGE_KEY_UDP : static::STORAGE_KEY_HTTP; |
86
|
2 |
|
$this->addPointsToStorage($typeKey, $precision, $points); |
87
|
|
|
|
88
|
2 |
|
return true; |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
if ($event instanceof UdpEvent) { |
92
|
1 |
|
$this->writeUdpPoints($points, $precision); |
93
|
|
|
|
94
|
1 |
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
$this->writeHttpPoints($points, $precision); |
98
|
|
|
|
99
|
1 |
|
return true; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param Event $event |
104
|
|
|
* |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
2 |
|
public function onKernelTerminate(Event $event): bool |
|
|
|
|
108
|
|
|
{ |
109
|
2 |
|
foreach ($this->storage[static::STORAGE_KEY_UDP] as $precision => $points) { |
110
|
1 |
|
$this->writeUdpPoints($points, $precision); |
111
|
|
|
} |
112
|
|
|
|
113
|
2 |
|
foreach ($this->storage[static::STORAGE_KEY_HTTP] as $precision => $points) { |
114
|
1 |
|
$this->writeHttpPoints($points, $precision); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// Reset the storage after writing points. |
118
|
2 |
|
$this->initStorage(); |
119
|
|
|
|
120
|
2 |
|
return true; |
121
|
|
|
} |
122
|
|
|
|
123
|
7 |
|
private function initStorage() |
124
|
|
|
{ |
125
|
7 |
|
$this->storage = [ |
126
|
7 |
|
static::STORAGE_KEY_UDP => [], |
127
|
7 |
|
static::STORAGE_KEY_HTTP => [], |
128
|
|
|
]; |
129
|
7 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param Point[] $points |
133
|
|
|
* @param string $precision |
134
|
|
|
*/ |
135
|
2 |
|
private function writeUdpPoints(array $points, string $precision) |
136
|
|
|
{ |
137
|
2 |
|
$this->udpDatabase->writePoints($points, $precision); |
138
|
2 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param Point[] $points |
142
|
|
|
* @param string $precision |
143
|
|
|
*/ |
144
|
2 |
|
private function writeHttpPoints(array $points, string $precision) |
145
|
|
|
{ |
146
|
2 |
|
$this->httpDatabase->writePoints($points, $precision); |
147
|
2 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string $typeKey |
151
|
|
|
* @param string $precision |
152
|
|
|
* @param array $points |
153
|
|
|
*/ |
154
|
2 |
|
private function addPointsToStorage(string $typeKey, string $precision, array $points) |
155
|
|
|
{ |
156
|
2 |
|
if (array_key_exists($precision, $this->storage[$typeKey])) { |
157
|
2 |
|
$this->storage[$typeKey][$precision] = array_merge($this->storage[$typeKey][$precision], $points); |
158
|
|
|
|
159
|
2 |
|
return; |
160
|
|
|
} |
161
|
|
|
|
162
|
2 |
|
$this->storage[$typeKey][$precision] = $points; |
163
|
2 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param AbstractInfluxDbEvent $event |
167
|
|
|
* |
168
|
|
|
* @return bool |
169
|
|
|
*/ |
170
|
7 |
|
private function isConcerned(AbstractInfluxDbEvent $event): bool |
171
|
|
|
{ |
172
|
7 |
|
return $this->connection === $event->getConnection() || is_null($event->getConnection()) && $this->isDefault; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.