wip
This commit is contained in:
parent
121b818da1
commit
333da893dd
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue