2018-12-03 13:44:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
2018-12-03 13:45:50 +00:00
|
|
|
namespace BeyondCode\LaravelWebSockets\Statistics\Logger;
|
2018-12-03 13:44:26 +00:00
|
|
|
|
2018-12-04 09:15:37 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Apps\App;
|
2020-08-19 19:39:38 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\Statistics\Events\StatisticsUpdated;
|
2018-12-03 13:45:50 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Statistics\Statistic;
|
2018-12-03 13:44:26 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
|
2020-08-13 06:49:09 +00:00
|
|
|
use Ratchet\ConnectionInterface;
|
2018-12-03 13:44:26 +00:00
|
|
|
|
2020-08-19 06:00:53 +00:00
|
|
|
class MemoryStatisticsLogger implements StatisticsLogger
|
2018-12-03 13:44:26 +00:00
|
|
|
{
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* The list of stored statistics.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2018-12-03 13:44:26 +00:00
|
|
|
protected $statistics = [];
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* The Channel manager.
|
|
|
|
|
*
|
|
|
|
|
* @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager
|
|
|
|
|
*/
|
2018-12-03 13:44:26 +00:00
|
|
|
protected $channelManager;
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
2020-08-19 19:39:38 +00:00
|
|
|
* The statistics driver instance.
|
2020-08-18 17:21:22 +00:00
|
|
|
*
|
2020-08-19 19:39:38 +00:00
|
|
|
* @var \BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver
|
2020-08-18 17:21:22 +00:00
|
|
|
*/
|
2020-08-19 19:39:38 +00:00
|
|
|
protected $driver;
|
2018-12-03 20:06:49 +00:00
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Initialize the logger.
|
|
|
|
|
*
|
|
|
|
|
* @param \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager $channelManager
|
2020-08-19 19:39:38 +00:00
|
|
|
* @param \BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver $driver
|
2020-08-18 17:21:22 +00:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2020-08-19 19:39:38 +00:00
|
|
|
public function __construct(ChannelManager $channelManager, StatisticsDriver $driver)
|
2018-12-03 13:44:26 +00:00
|
|
|
{
|
|
|
|
|
$this->channelManager = $channelManager;
|
2020-08-19 19:39:38 +00:00
|
|
|
$this->driver = $driver;
|
2018-12-03 13:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Handle the incoming websocket message.
|
|
|
|
|
*
|
|
|
|
|
* @param \Ratchet\ConnectionInterface $connection
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2018-12-03 15:23:20 +00:00
|
|
|
public function webSocketMessage(ConnectionInterface $connection)
|
2018-12-03 13:44:26 +00:00
|
|
|
{
|
2020-08-18 17:21:22 +00:00
|
|
|
$this->findOrMakeStatisticForAppId($connection->app->id)
|
2018-12-03 22:45:18 +00:00
|
|
|
->webSocketMessage();
|
2018-12-03 13:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Handle the incoming API message.
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $appId
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2018-12-03 15:23:20 +00:00
|
|
|
public function apiMessage($appId)
|
2018-12-03 13:44:26 +00:00
|
|
|
{
|
2020-08-18 17:21:22 +00:00
|
|
|
$this->findOrMakeStatisticForAppId($appId)
|
2018-12-03 22:45:18 +00:00
|
|
|
->apiMessage();
|
2018-12-03 13:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Handle the new conection.
|
|
|
|
|
*
|
|
|
|
|
* @param \Ratchet\ConnectionInterface $connection
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2018-12-03 15:23:20 +00:00
|
|
|
public function connection(ConnectionInterface $connection)
|
2018-12-03 13:44:26 +00:00
|
|
|
{
|
2020-08-18 17:21:22 +00:00
|
|
|
$this->findOrMakeStatisticForAppId($connection->app->id)
|
2018-12-03 22:45:18 +00:00
|
|
|
->connection();
|
2018-12-03 13:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Handle disconnections.
|
|
|
|
|
*
|
|
|
|
|
* @param \Ratchet\ConnectionInterface $connection
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2018-12-03 15:23:20 +00:00
|
|
|
public function disconnection(ConnectionInterface $connection)
|
2018-12-03 13:44:26 +00:00
|
|
|
{
|
2020-08-18 17:21:22 +00:00
|
|
|
$this->findOrMakeStatisticForAppId($connection->app->id)
|
2018-12-03 22:45:18 +00:00
|
|
|
->disconnection();
|
2018-12-03 13:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Save all the stored statistics.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2018-12-03 13:44:26 +00:00
|
|
|
public function save()
|
|
|
|
|
{
|
|
|
|
|
foreach ($this->statistics as $appId => $statistic) {
|
2018-12-04 21:22:33 +00:00
|
|
|
if (! $statistic->isEnabled()) {
|
2018-12-03 13:44:26 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-19 19:39:38 +00:00
|
|
|
broadcast(new StatisticsUpdated(
|
|
|
|
|
$this->driver::create($statistic->toArray())
|
|
|
|
|
));
|
2018-12-03 13:44:26 +00:00
|
|
|
|
2018-12-03 22:52:39 +00:00
|
|
|
$currentConnectionCount = $this->channelManager->getConnectionCount($appId);
|
2020-08-18 17:21:22 +00:00
|
|
|
|
2018-12-03 13:44:26 +00:00
|
|
|
$statistic->reset($currentConnectionCount);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-18 17:21:22 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Find or create a defined statistic for an app.
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $appId
|
|
|
|
|
* @return Statistic
|
|
|
|
|
*/
|
|
|
|
|
protected function findOrMakeStatisticForAppId($appId): Statistic
|
|
|
|
|
{
|
|
|
|
|
if (! isset($this->statistics[$appId])) {
|
|
|
|
|
$this->statistics[$appId] = new Statistic($appId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->statistics[$appId];
|
|
|
|
|
}
|
2018-12-04 21:22:33 +00:00
|
|
|
}
|