Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit c6ab778

Browse files
committed
improved statistics
1 parent 015f6f4 commit c6ab778

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

src/Statistics/Collectors/MemoryCollector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public function getAppStatistics($appId): PromiseInterface
151151
protected function findOrMake($appId): Statistic
152152
{
153153
if (! isset($this->statistics[$appId])) {
154-
$this->statistics[$appId] = new Statistic($appId);
154+
$this->statistics[$appId] = Statistic::new($appId);
155155
}
156156

157157
return $this->statistics[$appId];

src/Statistics/Collectors/RedisCollector.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ public function save()
177177
return;
178178
}
179179

180-
$statistic = $this->listToStatisticInstance(
181-
$appId, $list
180+
$statistic = $this->arrayToStatisticInstance(
181+
$appId, $this->redisListToArray($list)
182182
);
183183

184184
$this->createRecord($statistic, $appId);
@@ -228,8 +228,8 @@ public function getStatistics(): PromiseInterface
228228
->getPublishClient()
229229
->hgetall($this->channelManager->getRedisKey($appId, null, ['stats']))
230230
->then(function ($list) use ($appId, &$appsWithStatistics) {
231-
$appsWithStatistics[$appId] = $this->listToStatisticInstance(
232-
$appId, $list
231+
$appsWithStatistics[$appId] = $this->arrayToStatisticInstance(
232+
$appId, $this->redisListToArray($list)
233233
);
234234
});
235235
}
@@ -250,6 +250,8 @@ public function getAppStatistics($appId): PromiseInterface
250250
->getPublishClient()
251251
->hgetall($this->channelManager->getRedisKey($appId, null, ['stats']))
252252
->then(function ($list) use ($appId) {
253+
return $this->arrayToStatisticInstance(
254+
$appId, $this->redisListToArray($list)
253255
);
254256
});
255257
}
@@ -366,13 +368,12 @@ protected function lock()
366368
* @param array $list
367369
* @return array
368370
*/
369-
protected function listToKeyValue(array $list)
371+
protected function redisListToArray(array $list)
370372
{
371373
// Redis lists come into a format where the keys are on even indexes
372374
// and the values are on odd indexes. This way, we know which
373375
// ones are keys and which ones are values and their get combined
374376
// later to form the key => value array.
375-
376377
[$keys, $values] = collect($list)->partition(function ($value, $key) {
377378
return $key % 2 === 0;
378379
});
@@ -381,21 +382,18 @@ protected function listToKeyValue(array $list)
381382
}
382383

383384
/**
384-
* Transform a list coming from a Redis list
385-
* to a Statistic instance.
385+
* Transform a key-value pair to a Statistic instance.
386386
*
387387
* @param string|int $appId
388-
* @param array $list
388+
* @param array $stats
389389
* @return \BeyondCode\LaravelWebSockets\Statistics\Statistic
390390
*/
391-
protected function listToStatisticInstance($appId, array $list)
391+
protected function arrayToStatisticInstance($appId, array $stats)
392392
{
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);
400398
}
401399
}

src/Statistics/Statistic.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ public function __construct($appId)
5252
$this->appId = $appId;
5353
}
5454

55+
/**
56+
* Create a new statistic instance.
57+
*
58+
* @param string|int $appId
59+
* @return \BeyondCode\LaravelWebSockets\Statistics\Statistic
60+
*/
61+
public static function new($appId)
62+
{
63+
return new static($appId);
64+
}
65+
5566
/**
5667
* Set the current connections count.
5768
*

0 commit comments

Comments
 (0)