laravel-websockets/src/Statistics/Logging/StatisticsLogger.php

81 lines
2.3 KiB
PHP
Raw Normal View History

2018-12-03 12:02:51 +00:00
<?php
namespace BeyondCode\LaravelWebSockets\Statistics\Logging;
2018-12-03 13:24:06 +00:00
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController;
2018-12-03 12:02:51 +00:00
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
2018-12-03 13:24:06 +00:00
use GuzzleHttp\Client;
2018-12-03 12:02:51 +00:00
use Ratchet\ConnectionInterface;
2018-12-03 13:07:18 +00:00
class StatisticsLogger
2018-12-03 12:02:51 +00:00
{
/** @var Statistic[] */
protected $statistics = [];
2018-12-03 12:57:01 +00:00
/** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */
2018-12-03 12:02:51 +00:00
protected $channelManager;
2018-12-03 13:24:06 +00:00
public function __construct(ChannelManager $channelManager, Client $client)
2018-12-03 12:02:51 +00:00
{
$this->channelManager = $channelManager;
2018-12-03 13:24:06 +00:00
$this->client = $client;
2018-12-03 12:02:51 +00:00
}
2018-12-03 13:35:00 +00:00
public function logWebSocketMessage(ConnectionInterface $connection)
2018-12-03 12:02:51 +00:00
{
$this->initializeStatistics($connection->app->id);
2018-12-03 13:35:00 +00:00
$this->statistics[$connection->app->id]->logWebSocketMessage();
2018-12-03 12:02:51 +00:00
}
2018-12-03 13:35:00 +00:00
public function logApiMessage($appId)
2018-12-03 12:02:51 +00:00
{
2018-12-03 12:33:20 +00:00
$this->initializeStatistics($appId);
2018-12-03 12:02:51 +00:00
2018-12-03 13:35:00 +00:00
$this->statistics[$appId]->logApiMessage();
2018-12-03 12:02:51 +00:00
}
2018-12-03 13:35:00 +00:00
public function logConnection(ConnectionInterface $connection)
2018-12-03 12:02:51 +00:00
{
$this->initializeStatistics($connection->app->id);
2018-12-03 13:35:00 +00:00
$this->statistics[$connection->app->id]->logConnection();
2018-12-03 12:02:51 +00:00
}
2018-12-03 13:35:00 +00:00
public function logDisconnection(ConnectionInterface $connection)
2018-12-03 12:02:51 +00:00
{
$this->initializeStatistics($connection->app->id);
2018-12-03 13:35:00 +00:00
$this->statistics[$connection->app->id]->logDisconnection();
2018-12-03 12:02:51 +00:00
}
protected function initializeStatistics($id)
{
2018-12-03 12:27:45 +00:00
if (!isset($this->statistics[$id])) {
2018-12-03 12:02:51 +00:00
$this->statistics[$id] = new Statistic($id);
}
}
public function save()
{
foreach ($this->statistics as $appId => $statistic) {
2018-12-03 13:24:06 +00:00
if (! $statistic->isEnabled()) {
continue;
2018-12-03 12:37:30 +00:00
}
2018-12-03 12:02:51 +00:00
2018-12-03 13:24:06 +00:00
$this->client->postAsync(
action([WebsocketStatisticsEntriesController::class, 'store']),
$statistic->toArray()
);
2018-12-03 12:02:51 +00:00
// Reset connection and message count
2018-12-03 13:24:54 +00:00
$currentConnectionCount = collect($this->channelManager->getChannels($appId))
2018-12-03 13:24:06 +00:00
->sum(function ($channel) {
return count($channel->getSubscribedConnections());
});
2018-12-03 12:02:51 +00:00
2018-12-03 13:24:54 +00:00
$statistic->reset($currentConnectionCount);
2018-12-03 12:02:51 +00:00
}
}
}