improved statistics

This commit is contained in:
Alex Renoki 2020-09-11 13:41:02 +03:00
parent 015f6f4abb
commit c6ab7786d8
3 changed files with 27 additions and 18 deletions

View File

@ -151,7 +151,7 @@ class MemoryCollector implements StatisticsCollector
protected function findOrMake($appId): Statistic protected function findOrMake($appId): Statistic
{ {
if (! isset($this->statistics[$appId])) { if (! isset($this->statistics[$appId])) {
$this->statistics[$appId] = new Statistic($appId); $this->statistics[$appId] = Statistic::new($appId);
} }
return $this->statistics[$appId]; return $this->statistics[$appId];

View File

@ -177,8 +177,8 @@ class RedisCollector extends MemoryCollector
return; return;
} }
$statistic = $this->listToStatisticInstance( $statistic = $this->arrayToStatisticInstance(
$appId, $list $appId, $this->redisListToArray($list)
); );
$this->createRecord($statistic, $appId); $this->createRecord($statistic, $appId);
@ -228,8 +228,8 @@ class RedisCollector extends MemoryCollector
->getPublishClient() ->getPublishClient()
->hgetall($this->channelManager->getRedisKey($appId, null, ['stats'])) ->hgetall($this->channelManager->getRedisKey($appId, null, ['stats']))
->then(function ($list) use ($appId, &$appsWithStatistics) { ->then(function ($list) use ($appId, &$appsWithStatistics) {
$appsWithStatistics[$appId] = $this->listToStatisticInstance( $appsWithStatistics[$appId] = $this->arrayToStatisticInstance(
$appId, $list $appId, $this->redisListToArray($list)
); );
}); });
} }
@ -250,6 +250,8 @@ class RedisCollector extends MemoryCollector
->getPublishClient() ->getPublishClient()
->hgetall($this->channelManager->getRedisKey($appId, null, ['stats'])) ->hgetall($this->channelManager->getRedisKey($appId, null, ['stats']))
->then(function ($list) use ($appId) { ->then(function ($list) use ($appId) {
return $this->arrayToStatisticInstance(
$appId, $this->redisListToArray($list)
); );
}); });
} }
@ -366,13 +368,12 @@ class RedisCollector extends MemoryCollector
* @param array $list * @param array $list
* @return array * @return array
*/ */
protected function listToKeyValue(array $list) protected function redisListToArray(array $list)
{ {
// Redis lists come into a format where the keys are on even indexes // Redis lists come into a format where the keys are on even indexes
// and the values are on odd indexes. This way, we know which // and the values are on odd indexes. This way, we know which
// ones are keys and which ones are values and their get combined // ones are keys and which ones are values and their get combined
// later to form the key => value array. // later to form the key => value array.
[$keys, $values] = collect($list)->partition(function ($value, $key) { [$keys, $values] = collect($list)->partition(function ($value, $key) {
return $key % 2 === 0; return $key % 2 === 0;
}); });
@ -381,21 +382,18 @@ class RedisCollector extends MemoryCollector
} }
/** /**
* Transform a list coming from a Redis list * Transform a key-value pair to a Statistic instance.
* to a Statistic instance.
* *
* @param string|int $appId * @param string|int $appId
* @param array $list * @param array $stats
* @return \BeyondCode\LaravelWebSockets\Statistics\Statistic * @return \BeyondCode\LaravelWebSockets\Statistics\Statistic
*/ */
protected function listToStatisticInstance($appId, array $list) protected function arrayToStatisticInstance($appId, array $stats)
{ {
$list = $this->listToKeyValue($list); return Statistic::new($appId)
->setCurrentConnectionsCount($stats['current_connections_count'] ?? 0)
return (new Statistic($appId)) ->setPeakConnectionsCount($stats['peak_connections_count'] ?? 0)
->setCurrentConnectionsCount($list['current_connections_count'] ?? 0) ->setWebSocketMessagesCount($stats['websocket_messages_count'] ?? 0)
->setPeakConnectionsCount($list['peak_connections_count'] ?? 0) ->setApiMessagesCount($stats['api_messages_count'] ?? 0);
->setWebSocketMessagesCount($list['websocket_messages_count'] ?? 0)
->setApiMessagesCount($list['api_messages_count'] ?? 0);
} }
} }

View File

@ -52,6 +52,17 @@ class Statistic
$this->appId = $appId; $this->appId = $appId;
} }
/**
* Create a new statistic instance.
*
* @param string|int $appId
* @return \BeyondCode\LaravelWebSockets\Statistics\Statistic
*/
public static function new($appId)
{
return new static($appId);
}
/** /**
* Set the current connections count. * Set the current connections count.
* *