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
|
7 |
|
public function onPointsCollected(AbstractInfluxDbEvent $event): bool |
64
|
|
|
{ |
65
|
7 |
|
$isConcerned = $this->connection === $event->getConnection() || !$event->getConnection() && $this->isDefault; |
|
|
|
|
66
|
7 |
|
if (!$isConcerned) { |
67
|
1 |
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
6 |
|
if (!$this->udpDatabase && ($event instanceof UdpEvent || $event instanceof DeferredUdpEvent)) { |
71
|
2 |
|
throw new \RuntimeException( |
72
|
2 |
|
'No UDP connection available for database "'.$this->httpDatabase->getName().'". ' |
73
|
2 |
|
.'You must enable it on the configuration to use it.' |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
4 |
|
$points = $event->getPoints(); |
78
|
4 |
|
$precision = $event->getPrecision(); |
79
|
|
|
|
80
|
4 |
|
if ($event instanceof AbstractDeferredInfluxDbEvent) { |
81
|
2 |
|
$typeKey = $event instanceof DeferredUdpEvent ? static::STORAGE_KEY_UDP : static::STORAGE_KEY_HTTP; |
82
|
2 |
|
$this->addPointsToStorage($typeKey, $precision, $points); |
83
|
|
|
|
84
|
2 |
|
return true; |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
if ($event instanceof UdpEvent) { |
88
|
1 |
|
$this->writeUdpPoints($points, $precision); |
89
|
|
|
|
90
|
1 |
|
return true; |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
$this->writeHttpPoints($points, $precision); |
94
|
|
|
|
95
|
1 |
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param Event $event |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
2 |
|
public function onKernelTerminate(Event $event): bool |
|
|
|
|
104
|
|
|
{ |
105
|
2 |
|
foreach ($this->storage[static::STORAGE_KEY_UDP] as $precision => $points) { |
106
|
1 |
|
$this->writeUdpPoints($points, $precision); |
107
|
|
|
} |
108
|
|
|
|
109
|
2 |
|
foreach ($this->storage[static::STORAGE_KEY_HTTP] as $precision => $points) { |
110
|
1 |
|
$this->writeHttpPoints($points, $precision); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Reset the storage after writing points. |
114
|
2 |
|
$this->initStorage(); |
115
|
|
|
|
116
|
2 |
|
return true; |
117
|
|
|
} |
118
|
|
|
|
119
|
7 |
|
private function initStorage() |
120
|
|
|
{ |
121
|
7 |
|
$this->storage = [ |
122
|
7 |
|
static::STORAGE_KEY_UDP => [], |
123
|
7 |
|
static::STORAGE_KEY_HTTP => [], |
124
|
|
|
]; |
125
|
7 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param Point[] $points |
129
|
|
|
* @param string $precision |
130
|
|
|
*/ |
131
|
2 |
|
private function writeUdpPoints(array $points, string $precision) |
132
|
|
|
{ |
133
|
2 |
|
$this->udpDatabase->writePoints($points, $precision); |
134
|
2 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param Point[] $points |
138
|
|
|
* @param string $precision |
139
|
|
|
*/ |
140
|
2 |
|
private function writeHttpPoints(array $points, string $precision) |
141
|
|
|
{ |
142
|
2 |
|
$this->httpDatabase->writePoints($points, $precision); |
143
|
2 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $typeKey |
147
|
|
|
* @param string $precision |
148
|
|
|
* @param array $points |
149
|
|
|
*/ |
150
|
2 |
|
private function addPointsToStorage(string $typeKey, string $precision, array $points) |
151
|
|
|
{ |
152
|
2 |
|
if (array_key_exists($precision, $this->storage[$typeKey])) { |
153
|
2 |
|
$this->storage[$typeKey][$precision] = array_merge($this->storage[$typeKey][$precision], $points); |
154
|
|
|
|
155
|
2 |
|
return; |
156
|
|
|
} |
157
|
|
|
|
158
|
2 |
|
$this->storage[$typeKey][$precision] = $points; |
159
|
2 |
|
} |
160
|
|
|
} |
161
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: