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-13 06:49:09 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController;
|
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 Clue\React\Buzz\Browser;
|
|
|
|
|
use function GuzzleHttp\Psr7\stream_for;
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
|
* The Browser instance.
|
|
|
|
|
*
|
|
|
|
|
* @var \Clue\React\Buzz\Browser
|
|
|
|
|
*/
|
2018-12-03 20:06:49 +00:00
|
|
|
protected $browser;
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Initialize the logger.
|
|
|
|
|
*
|
|
|
|
|
* @param \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager $channelManager
|
|
|
|
|
* @param \Clue\React\Buzz\Browser $browser
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2018-12-03 20:06:49 +00:00
|
|
|
public function __construct(ChannelManager $channelManager, Browser $browser)
|
2018-12-03 13:44:26 +00:00
|
|
|
{
|
|
|
|
|
$this->channelManager = $channelManager;
|
2018-12-03 20:06:49 +00:00
|
|
|
$this->browser = $browser;
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 09:15:37 +00:00
|
|
|
$postData = array_merge($statistic->toArray(), [
|
2018-12-04 21:22:33 +00:00
|
|
|
'secret' => App::findById($appId)->secret,
|
2018-12-04 09:15:37 +00:00
|
|
|
]);
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
$this->browser
|
2018-12-03 20:06:49 +00:00
|
|
|
->post(
|
2020-08-13 07:09:09 +00:00
|
|
|
action([WebSocketStatisticsEntriesController::class, 'store']),
|
2018-12-03 20:41:41 +00:00
|
|
|
['Content-Type' => 'application/json'],
|
2018-12-04 09:15:37 +00:00
|
|
|
stream_for(json_encode($postData))
|
2018-12-03 20:41:41 +00:00
|
|
|
);
|
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
|
|
|
}
|