@@ -177,8 +177,8 @@ public function save()
177
177
return ;
178
178
}
179
179
180
- $ statistic = $ this ->listToStatisticInstance (
181
- $ appId , $ list
180
+ $ statistic = $ this ->arrayToStatisticInstance (
181
+ $ appId , $ this -> redisListToArray ( $ list)
182
182
);
183
183
184
184
$ this ->createRecord ($ statistic , $ appId );
@@ -228,8 +228,8 @@ public function getStatistics(): PromiseInterface
228
228
->getPublishClient ()
229
229
->hgetall ($ this ->channelManager ->getRedisKey ($ appId , null , ['stats ' ]))
230
230
->then (function ($ list ) use ($ appId , &$ appsWithStatistics ) {
231
- $ appsWithStatistics [$ appId ] = $ this ->listToStatisticInstance (
232
- $ appId , $ list
231
+ $ appsWithStatistics [$ appId ] = $ this ->arrayToStatisticInstance (
232
+ $ appId , $ this -> redisListToArray ( $ list)
233
233
);
234
234
});
235
235
}
@@ -250,6 +250,8 @@ public function getAppStatistics($appId): PromiseInterface
250
250
->getPublishClient ()
251
251
->hgetall ($ this ->channelManager ->getRedisKey ($ appId , null , ['stats ' ]))
252
252
->then (function ($ list ) use ($ appId ) {
253
+ return $ this ->arrayToStatisticInstance (
254
+ $ appId , $ this ->redisListToArray ($ list )
253
255
);
254
256
});
255
257
}
@@ -366,13 +368,12 @@ protected function lock()
366
368
* @param array $list
367
369
* @return array
368
370
*/
369
- protected function listToKeyValue (array $ list )
371
+ protected function redisListToArray (array $ list )
370
372
{
371
373
// Redis lists come into a format where the keys are on even indexes
372
374
// and the values are on odd indexes. This way, we know which
373
375
// ones are keys and which ones are values and their get combined
374
376
// later to form the key => value array.
375
-
376
377
[$ keys , $ values ] = collect ($ list )->partition (function ($ value , $ key ) {
377
378
return $ key % 2 === 0 ;
378
379
});
@@ -381,21 +382,18 @@ protected function listToKeyValue(array $list)
381
382
}
382
383
383
384
/**
384
- * Transform a list coming from a Redis list
385
- * to a Statistic instance.
385
+ * Transform a key-value pair to a Statistic instance.
386
386
*
387
387
* @param string|int $appId
388
- * @param array $list
388
+ * @param array $stats
389
389
* @return \BeyondCode\LaravelWebSockets\Statistics\Statistic
390
390
*/
391
- protected function listToStatisticInstance ($ appId , array $ list )
391
+ protected function arrayToStatisticInstance ($ appId , array $ stats )
392
392
{
393
- $ list = $ this ->listToKeyValue ($ list );
394
-
395
- return (new Statistic ($ appId ))
396
- ->setCurrentConnectionsCount ($ list ['current_connections_count ' ] ?? 0 )
397
- ->setPeakConnectionsCount ($ list ['peak_connections_count ' ] ?? 0 )
398
- ->setWebSocketMessagesCount ($ list ['websocket_messages_count ' ] ?? 0 )
399
- ->setApiMessagesCount ($ list ['api_messages_count ' ] ?? 0 );
393
+ return Statistic::new ($ appId )
394
+ ->setCurrentConnectionsCount ($ stats ['current_connections_count ' ] ?? 0 )
395
+ ->setPeakConnectionsCount ($ stats ['peak_connections_count ' ] ?? 0 )
396
+ ->setWebSocketMessagesCount ($ stats ['websocket_messages_count ' ] ?? 0 )
397
+ ->setApiMessagesCount ($ stats ['api_messages_count ' ] ?? 0 );
400
398
}
401
399
}
0 commit comments