1 | <?php |
||
18 | 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 | 5 | public function __construct(string $connection, bool $isDefault, Database $httpDatabase, Database $udpDatabase) |
|
55 | { |
||
56 | 5 | $this->connection = $connection; |
|
57 | 5 | $this->isDefault = $isDefault; |
|
58 | 5 | $this->httpDatabase = $httpDatabase; |
|
59 | 5 | $this->udpDatabase = $udpDatabase; |
|
60 | 5 | $this->initStorage(); |
|
61 | 5 | } |
|
62 | |||
63 | 5 | public function onPointsCollected(InfluxDbEvent $event): bool |
|
64 | { |
||
65 | 5 | $isConcerned = $this->connection === $event->getConnection() || !$event->getConnection() && $this->isDefault; |
|
|
|||
66 | 5 | if (!$isConcerned) { |
|
67 | 1 | return false; |
|
68 | } |
||
69 | |||
70 | 4 | $points = $event->getPoints(); |
|
71 | 4 | $precision = $event->getPrecision(); |
|
72 | |||
73 | 4 | if ($event instanceof DeferredInfluxDbEvent) { |
|
74 | 2 | $typeKey = $event instanceof DeferredUdpEvent ? static::STORAGE_KEY_UDP : static::STORAGE_KEY_HTTP; |
|
75 | 2 | $this->addPointsToStorage($typeKey, $precision, $points); |
|
76 | |||
77 | 2 | return true; |
|
78 | } |
||
79 | |||
80 | 2 | if ($event instanceof UdpEvent) { |
|
81 | 1 | $this->writeUdpPoints($points, $precision); |
|
82 | |||
83 | 1 | return true; |
|
84 | } |
||
85 | |||
86 | 1 | $this->writeHttpPoints($points, $precision); |
|
87 | |||
88 | 1 | return true; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param Event $event |
||
93 | * |
||
94 | * @return bool |
||
95 | */ |
||
96 | 2 | public function onKernelTerminate(Event $event): bool |
|
111 | |||
112 | 5 | private function initStorage() |
|
113 | { |
||
114 | 5 | $this->storage = [ |
|
115 | 5 | static::STORAGE_KEY_UDP => [], |
|
116 | 5 | static::STORAGE_KEY_HTTP => [], |
|
117 | ]; |
||
118 | 5 | } |
|
119 | |||
120 | /** |
||
121 | * @param Point[] $points |
||
122 | * @param string $precision |
||
123 | */ |
||
124 | 2 | private function writeUdpPoints(array $points, string $precision) |
|
128 | |||
129 | /** |
||
130 | * @param Point[] $points |
||
131 | * @param string $precision |
||
132 | */ |
||
133 | 2 | private function writeHttpPoints(array $points, string $precision) |
|
137 | |||
138 | /** |
||
139 | * @param string $typeKey |
||
140 | * @param string $precision |
||
141 | * @param array $points |
||
142 | */ |
||
143 | 2 | private function addPointsToStorage(string $typeKey, string $precision, array $points) |
|
153 | } |
||
154 |
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: