diff --git a/src/Statistics/Logging/Logger.php b/src/Statistics/Logging/Logger.php new file mode 100644 index 0000000..4776ddc --- /dev/null +++ b/src/Statistics/Logging/Logger.php @@ -0,0 +1,70 @@ +channelManager = $channelManager; + } + + public function webSocketMessage(ConnectionInterface $connection) + { + $this->initializeStatistics($connection->app->id); + + $this->statistics[$connection->app->id]->webSocketMessage(); + } + + public function apiMessage(ConnectionInterface $connection) + { + $this->initializeStatistics($connection->app->id); + + $this->statistics[$connection->app->id]->apiMessage(); + } + + public function connection(ConnectionInterface $connection) + { + $this->initializeStatistics($connection->app->id); + + $this->statistics[$connection->app->id]->connection(); + } + + public function disconnection(ConnectionInterface $connection) + { + $this->initializeStatistics($connection->app->id); + + $this->statistics[$connection->app->id]->disconnection(); + } + + protected function initializeStatistics($id) + { + if (!isset($this->statistics[$connection->app->id])) { + $this->statistics[$id] = new Statistic($id); + } + } + + public function save() + { + foreach ($this->statistics as $appId => $statistic) { + // TODO: perform http request + + // Reset connection and message count + $connections = Collection::make($this->channelManager->getChannels($appId))->sum(function ($channel) { + return count($channel->getSubscribedConnections()); + }); + + $statistic->reset($connections); + } + } +} \ No newline at end of file diff --git a/src/Statistics/Logging/Statistic.php b/src/Statistics/Logging/Statistic.php new file mode 100644 index 0000000..1fde485 --- /dev/null +++ b/src/Statistics/Logging/Statistic.php @@ -0,0 +1,67 @@ +appId = $appId; + } + + public function connection() + { + $this->connections++; + + $this->peakConnections = max($this->connections, $this->peakConnections); + } + + public function disconnection() + { + $this->connections--; + + $this->peakConnections = max($this->connections, $this->peakConnections); + } + + public function webSocketMessage() + { + $this->webSocketMessageCount++; + } + + public function apiMessage() + { + $this->apiMessageCount++; + } + + public function reset(int $connections) + { + $this->connections = $connections; + $this->peakConnections = $connections; + $this->webSocketMessageCount = 0; + $this->apiMessageCount = 0; + } + + public function toArray() + { + return [ + 'app_id' => $this->appId, + 'peak_connections' => $this->peakConnections, + 'websocket_message_count' => $this->webSocketMessageCount, + 'api_message_count' => $this->apiMessageCount, + ]; + } +} \ No newline at end of file