1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Algatux\InfluxDbBundle\Events\Listeners; |
6
|
|
|
|
7
|
|
|
use Algatux\InfluxDbBundle\Events\DeferredInfluxDbEvent; |
8
|
|
|
use Algatux\InfluxDbBundle\Events\DeferredUdpEvent; |
9
|
|
|
use Algatux\InfluxDbBundle\Events\InfluxDbEvent; |
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
|
|
|
class InfluxDbEventListener |
19
|
|
|
{ |
20
|
|
|
const STORAGE_KEY_UDP = 'udp'; |
21
|
|
|
const STORAGE_KEY_HTTP = 'http'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Database |
25
|
|
|
*/ |
26
|
|
|
private $httpDatabase; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Database |
30
|
|
|
*/ |
31
|
|
|
private $udpDatabase; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $storage; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param Database $httpDatabase |
40
|
|
|
* @param Database $udpDatabase |
41
|
|
|
*/ |
42
|
4 |
|
public function __construct( |
43
|
|
|
Database $httpDatabase, |
44
|
|
|
Database $udpDatabase |
45
|
|
|
) { |
46
|
4 |
|
$this->httpDatabase = $httpDatabase; |
47
|
4 |
|
$this->udpDatabase = $udpDatabase; |
48
|
4 |
|
$this->initStorage(); |
49
|
4 |
|
} |
50
|
|
|
|
51
|
4 |
|
public function onPointsCollected(InfluxDbEvent $event): bool |
52
|
|
|
{ |
53
|
4 |
|
$points = $event->getPoints(); |
54
|
4 |
|
$precision = $event->getPrecision(); |
55
|
|
|
|
56
|
4 |
|
if ($event instanceof DeferredInfluxDbEvent) { |
57
|
2 |
|
$typeKey = $event instanceof DeferredUdpEvent ? static::STORAGE_KEY_UDP : static::STORAGE_KEY_HTTP; |
58
|
2 |
|
$this->storage[$typeKey][$precision] = |
59
|
2 |
|
array_key_exists($precision, $this->storage[$typeKey]) |
60
|
2 |
|
? array_merge($this->storage[$typeKey][$precision], $points) |
61
|
2 |
|
: $points; |
62
|
|
|
|
63
|
2 |
|
return true; |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
if ($event instanceof UdpEvent) { |
67
|
1 |
|
$this->writeUdpPoints($points, $precision); |
68
|
|
|
|
69
|
1 |
|
return true; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
$this->writeHttpPoints($points, $precision); |
73
|
|
|
|
74
|
1 |
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param Event $event |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
2 |
|
public function onKernelTerminate(Event $event): bool |
|
|
|
|
83
|
|
|
{ |
84
|
2 |
|
foreach ($this->storage[static::STORAGE_KEY_UDP] as $precision => $points) { |
85
|
1 |
|
$this->writeUdpPoints($points, $precision); |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
foreach ($this->storage[static::STORAGE_KEY_HTTP] as $precision => $points) { |
89
|
1 |
|
$this->writeHttpPoints($points, $precision); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// Reset the storage after writing points. |
93
|
2 |
|
$this->initStorage(); |
94
|
|
|
|
95
|
2 |
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
4 |
|
private function initStorage() |
99
|
|
|
{ |
100
|
4 |
|
$this->storage = [ |
101
|
4 |
|
static::STORAGE_KEY_UDP => [], |
102
|
4 |
|
static::STORAGE_KEY_HTTP => [], |
103
|
|
|
]; |
104
|
4 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param Point[] $points |
108
|
|
|
* @param string $precision |
109
|
|
|
*/ |
110
|
2 |
|
private function writeUdpPoints(array $points, string $precision) |
111
|
|
|
{ |
112
|
2 |
|
$this->udpDatabase->writePoints($points, $precision); |
113
|
2 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param Point[] $points |
117
|
|
|
* @param string $precision |
118
|
|
|
*/ |
119
|
2 |
|
private function writeHttpPoints(array $points, string $precision) |
120
|
|
|
{ |
121
|
2 |
|
$this->httpDatabase->writePoints($points, $precision); |
122
|
2 |
|
} |
123
|
|
|
} |
124
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.