From 6c96bb8edc9a7eb1bfa8a4224e8b705fa3d987cc Mon Sep 17 00:00:00 2001 From: freek Date: Mon, 3 Dec 2018 23:52:39 +0100 Subject: [PATCH] wip --- src/Statistics/Logger/HttpStatisticsLogger.php | 14 +++++--------- src/WebSockets/Channels/ChannelManager.php | 6 ++++++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Statistics/Logger/HttpStatisticsLogger.php b/src/Statistics/Logger/HttpStatisticsLogger.php index 4b14d42..4985ed9 100644 --- a/src/Statistics/Logger/HttpStatisticsLogger.php +++ b/src/Statistics/Logger/HttpStatisticsLogger.php @@ -57,7 +57,7 @@ class HttpStatisticsLogger implements StatisticsLogger protected function findOrMakeStatisticForAppId($appId): Statistic { - if (! isset($this->statistics[$appId])) { + if (!isset($this->statistics[$appId])) { $this->statistics[$appId] = new Statistic($appId); } @@ -68,23 +68,19 @@ class HttpStatisticsLogger implements StatisticsLogger { foreach ($this->statistics as $appId => $statistic) { - if (! $statistic->isEnabled()) { + if (!$statistic->isEnabled()) { continue; } - $this->browser + $this + ->browser ->post( action([WebSocketStatisticsEntriesController::class, 'store']), ['Content-Type' => 'application/json'], stream_for(json_encode($statistic->toArray())) ); - // Reset connection and message count - $currentConnectionCount = collect($this->channelManager->getChannels($appId)) - ->sum(function ($channel) { - return count($channel->getSubscribedConnections()); - }); - + $currentConnectionCount = $this->channelManager->getConnectionCount($appId); $statistic->reset($currentConnectionCount); } } diff --git a/src/WebSockets/Channels/ChannelManager.php b/src/WebSockets/Channels/ChannelManager.php index fa77442..9c7c265 100644 --- a/src/WebSockets/Channels/ChannelManager.php +++ b/src/WebSockets/Channels/ChannelManager.php @@ -46,6 +46,12 @@ class ChannelManager return $this->channels[$appId] ?? []; } + public function getConnectionCount(string $appId): int + { + return collect($this->getChannels($appId)) + ->sum->getSubscribedConnections(); + } + public function removeFromAllChannels(ConnectionInterface $connection) { if (!isset($connection->app)) {