2020-09-10 19:59:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace BeyondCode\LaravelWebSockets\Statistics\Collectors;
|
|
|
|
|
|
2020-09-10 19:59:49 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Contracts\ChannelManager;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\Contracts\StatisticsCollector;
|
2020-09-10 19:59:26 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Facades\StatisticsStore;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\Statistics\Statistic;
|
2020-09-10 19:59:49 +00:00
|
|
|
use React\Promise\FulfilledPromise;
|
|
|
|
|
use React\Promise\PromiseInterface;
|
2020-09-10 19:59:26 +00:00
|
|
|
|
|
|
|
|
class MemoryCollector implements StatisticsCollector
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* The list of stored statistics.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $statistics = [];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The Channel manager.
|
|
|
|
|
*
|
|
|
|
|
* @var \BeyondCode\LaravelWebSockets\Contracts\ChannelManager
|
|
|
|
|
*/
|
|
|
|
|
protected $channelManager;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize the logger.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->channelManager = app(ChannelManager::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle the incoming websocket message.
|
|
|
|
|
*
|
|
|
|
|
* @param string|int $appId
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function webSocketMessage($appId)
|
|
|
|
|
{
|
|
|
|
|
$this->findOrMake($appId)
|
|
|
|
|
->webSocketMessage();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle the incoming API message.
|
|
|
|
|
*
|
|
|
|
|
* @param string|int $appId
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function apiMessage($appId)
|
|
|
|
|
{
|
|
|
|
|
$this->findOrMake($appId)
|
|
|
|
|
->apiMessage();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle the new conection.
|
|
|
|
|
*
|
|
|
|
|
* @param string|int $appId
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function connection($appId)
|
|
|
|
|
{
|
|
|
|
|
$this->findOrMake($appId)
|
|
|
|
|
->connection();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle disconnections.
|
|
|
|
|
*
|
|
|
|
|
* @param string|int $appId
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function disconnection($appId)
|
|
|
|
|
{
|
|
|
|
|
$this->findOrMake($appId)
|
|
|
|
|
->disconnection();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Save all the stored statistics.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function save()
|
|
|
|
|
{
|
|
|
|
|
$this->getStatistics()->then(function ($statistics) {
|
|
|
|
|
foreach ($statistics as $appId => $statistic) {
|
|
|
|
|
if (! $statistic->isEnabled()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->createRecord($statistic, $appId);
|
|
|
|
|
|
|
|
|
|
$this->channelManager
|
|
|
|
|
->getGlobalConnectionsCount($appId)
|
|
|
|
|
->then(function ($connections) use ($statistic) {
|
|
|
|
|
$statistic->reset(
|
|
|
|
|
is_null($connections) ? 0 : $connections
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Flush the stored statistics.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function flush()
|
|
|
|
|
{
|
|
|
|
|
$this->statistics = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the saved statistics.
|
|
|
|
|
*
|
|
|
|
|
* @return PromiseInterface[array]
|
|
|
|
|
*/
|
|
|
|
|
public function getStatistics(): PromiseInterface
|
|
|
|
|
{
|
|
|
|
|
return new FulfilledPromise($this->statistics);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the saved statistics for an app.
|
|
|
|
|
*
|
|
|
|
|
* @param string|int $appId
|
|
|
|
|
* @return PromiseInterface[\BeyondCode\LaravelWebSockets\Statistics\Statistic|null]
|
|
|
|
|
*/
|
|
|
|
|
public function getAppStatistics($appId): PromiseInterface
|
|
|
|
|
{
|
|
|
|
|
return new FulfilledPromise(
|
|
|
|
|
$this->statistics[$appId] ?? null
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Find or create a defined statistic for an app.
|
|
|
|
|
*
|
|
|
|
|
* @param string|int $appId
|
|
|
|
|
* @return \BeyondCode\LaravelWebSockets\Statistics\Statistic
|
|
|
|
|
*/
|
|
|
|
|
protected function findOrMake($appId): Statistic
|
|
|
|
|
{
|
|
|
|
|
if (! isset($this->statistics[$appId])) {
|
2020-09-11 10:41:02 +00:00
|
|
|
$this->statistics[$appId] = Statistic::new($appId);
|
2020-09-10 19:59:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->statistics[$appId];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new record using the Statistic Store.
|
|
|
|
|
*
|
|
|
|
|
* @param \BeyondCode\LaravelWebSockets\Statistics\Statistic $statistic
|
|
|
|
|
* @param mixed $appId
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function createRecord(Statistic $statistic, $appId)
|
|
|
|
|
{
|
|
|
|
|
StatisticsStore::store($statistic->toArray());
|
|
|
|
|
}
|
|
|
|
|
}
|