Merge branch 'master' of github.com:beyondcode/laravel-websockets

This commit is contained in:
freek 2018-12-03 13:06:00 +01:00
commit f9af6133df
2 changed files with 137 additions and 0 deletions

View File

@ -0,0 +1,70 @@
<?php
namespace BeyondCode\LaravelWebSockets\Statistics\Logging;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use Illuminate\Support\Collection;
use Ratchet\ConnectionInterface;
class Logger
{
/** @var Statistic[] */
protected $statistics = [];
/** @var ChannelManager */
protected $channelManager;
public function __construct(ChannelManager $channelManager)
{
$this->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);
}
}
}

View File

@ -0,0 +1,67 @@
<?php
namespace BeyondCode\LaravelWebSockets\Statistics\Logging;
class Statistic
{
protected $appId;
/** @var int */
protected $connections = 0;
/** @var int */
protected $peakConnections = 0;
/** @var int */
protected $webSocketMessageCount = 0;
/** @var int */
protected $apiMessageCount = 0;
public function __construct($appId)
{
$this->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,
];
}
}