1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets\Statistics\Collectors; |
4
|
|
|
|
5
|
|
|
use BeyondCode\LaravelWebSockets\Contracts\ChannelManager; |
6
|
|
|
use BeyondCode\LaravelWebSockets\Contracts\StatisticsCollector; |
7
|
|
|
use BeyondCode\LaravelWebSockets\Facades\StatisticsStore; |
8
|
|
|
use BeyondCode\LaravelWebSockets\Statistics\Statistic; |
9
|
|
|
use React\Promise\FulfilledPromise; |
10
|
|
|
use React\Promise\PromiseInterface; |
11
|
|
|
|
12
|
|
|
class MemoryCollector implements StatisticsCollector |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The list of stored statistics. |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $statistics = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The Channel manager. |
23
|
|
|
* |
24
|
|
|
* @var \BeyondCode\LaravelWebSockets\Contracts\ChannelManager |
25
|
|
|
*/ |
26
|
|
|
protected $channelManager; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Initialize the logger. |
30
|
|
|
* |
31
|
|
|
* @return void |
|
|
|
|
32
|
|
|
*/ |
33
|
|
|
public function __construct() |
34
|
|
|
{ |
35
|
|
|
$this->channelManager = app(ChannelManager::class); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Handle the incoming websocket message. |
40
|
|
|
* |
41
|
|
|
* @param string|int $appId |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function webSocketMessage($appId) |
45
|
|
|
{ |
46
|
|
|
$this->findOrMake($appId) |
47
|
|
|
->webSocketMessage(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Handle the incoming API message. |
52
|
|
|
* |
53
|
|
|
* @param string|int $appId |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
|
|
public function apiMessage($appId) |
57
|
|
|
{ |
58
|
|
|
$this->findOrMake($appId) |
59
|
|
|
->apiMessage(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Handle the new conection. |
64
|
|
|
* |
65
|
|
|
* @param string|int $appId |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
public function connection($appId) |
69
|
|
|
{ |
70
|
|
|
$this->findOrMake($appId) |
71
|
|
|
->connection(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Handle disconnections. |
76
|
|
|
* |
77
|
|
|
* @param string|int $appId |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
public function disconnection($appId) |
81
|
|
|
{ |
82
|
|
|
$this->findOrMake($appId) |
83
|
|
|
->disconnection(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Save all the stored statistics. |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
public function save() |
92
|
|
|
{ |
93
|
|
|
$this->getStatistics()->then(function ($statistics) { |
94
|
|
|
foreach ($statistics as $appId => $statistic) { |
95
|
|
|
if (! $statistic->isEnabled()) { |
96
|
|
|
continue; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->createRecord($statistic, $appId); |
100
|
|
|
|
101
|
|
|
$this->channelManager |
102
|
|
|
->getGlobalConnectionsCount($appId) |
103
|
|
|
->then(function ($connections) use ($statistic) { |
104
|
|
|
$statistic->reset( |
105
|
|
|
is_null($connections) ? 0 : $connections |
106
|
|
|
); |
107
|
|
|
}); |
108
|
|
|
} |
109
|
|
|
}); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Flush the stored statistics. |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
public function flush() |
118
|
|
|
{ |
119
|
|
|
$this->statistics = []; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get the saved statistics. |
124
|
|
|
* |
125
|
|
|
* @return PromiseInterface[array] |
|
|
|
|
126
|
|
|
*/ |
127
|
|
|
public function getStatistics(): PromiseInterface |
128
|
|
|
{ |
129
|
|
|
return new FulfilledPromise($this->statistics); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get the saved statistics for an app. |
134
|
|
|
* |
135
|
|
|
* @param string|int $appId |
136
|
|
|
* @return PromiseInterface[\BeyondCode\LaravelWebSockets\Statistics\Statistic|null] |
|
|
|
|
137
|
|
|
*/ |
138
|
|
|
public function getAppStatistics($appId): PromiseInterface |
139
|
|
|
{ |
140
|
|
|
return new FulfilledPromise( |
|
|
|
|
141
|
|
|
$this->statistics[$appId] ?? null |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Find or create a defined statistic for an app. |
147
|
|
|
* |
148
|
|
|
* @param string|int $appId |
149
|
|
|
* @return \BeyondCode\LaravelWebSockets\Statistics\Statistic |
150
|
|
|
*/ |
151
|
|
|
protected function findOrMake($appId): Statistic |
152
|
|
|
{ |
153
|
|
|
if (! isset($this->statistics[$appId])) { |
154
|
|
|
$this->statistics[$appId] = Statistic::new($appId); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $this->statistics[$appId]; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Create a new record using the Statistic Store. |
162
|
|
|
* |
163
|
|
|
* @param \BeyondCode\LaravelWebSockets\Statistics\Statistic $statistic |
164
|
|
|
* @param mixed $appId |
165
|
|
|
* @return void |
166
|
|
|
*/ |
167
|
|
|
public function createRecord(Statistic $statistic, $appId) |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
StatisticsStore::store($statistic->toArray()); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.