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
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
|
use Ratchet\ConnectionInterface;
|
|
|
|
|
|
|
|
|
|
class HttpStatisticsLogger implements StatisticsLogger
|
|
|
|
|
{
|
|
|
|
|
/** @var Statistic[] */
|
|
|
|
|
protected $statistics = [];
|
|
|
|
|
|
|
|
|
|
/** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */
|
|
|
|
|
protected $channelManager;
|
|
|
|
|
|
|
|
|
|
public function __construct(ChannelManager $channelManager, Client $client)
|
|
|
|
|
{
|
|
|
|
|
$this->channelManager = $channelManager;
|
|
|
|
|
|
|
|
|
|
$this->client = $client;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 15:23:20 +00:00
|
|
|
public function webSocketMessage(ConnectionInterface $connection)
|
2018-12-03 13:44:26 +00:00
|
|
|
{
|
|
|
|
|
$this->initializeStatistics($connection->app->id);
|
|
|
|
|
|
2018-12-03 15:23:20 +00:00
|
|
|
$this->statistics[$connection->app->id]->webSocketMessage();
|
2018-12-03 13:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
2018-12-03 15:23:20 +00:00
|
|
|
public function apiMessage($appId)
|
2018-12-03 13:44:26 +00:00
|
|
|
{
|
|
|
|
|
$this->initializeStatistics($appId);
|
|
|
|
|
|
2018-12-03 15:23:20 +00:00
|
|
|
$this->statistics[$appId]->apiMessage();
|
2018-12-03 13:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
2018-12-03 15:23:20 +00:00
|
|
|
public function connection(ConnectionInterface $connection)
|
2018-12-03 13:44:26 +00:00
|
|
|
{
|
|
|
|
|
$this->initializeStatistics($connection->app->id);
|
|
|
|
|
|
2018-12-03 15:23:20 +00:00
|
|
|
$this->statistics[$connection->app->id]->connection();
|
2018-12-03 13:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
2018-12-03 15:23:20 +00:00
|
|
|
public function disconnection(ConnectionInterface $connection)
|
2018-12-03 13:44:26 +00:00
|
|
|
{
|
|
|
|
|
$this->initializeStatistics($connection->app->id);
|
|
|
|
|
|
2018-12-03 15:23:20 +00:00
|
|
|
$this->statistics[$connection->app->id]->disconnection();
|
2018-12-03 13:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function initializeStatistics($id)
|
|
|
|
|
{
|
|
|
|
|
if (!isset($this->statistics[$id])) {
|
|
|
|
|
$this->statistics[$id] = new Statistic($id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function save()
|
|
|
|
|
{
|
2018-12-03 15:50:06 +00:00
|
|
|
echo 'in actual save method';
|
|
|
|
|
|
2018-12-03 13:44:26 +00:00
|
|
|
foreach ($this->statistics as $appId => $statistic) {
|
2018-12-03 15:50:06 +00:00
|
|
|
echo "stats of ${appId} " . $statistic->isEnabled() ? 'enabled' : 'DISABLED!';
|
|
|
|
|
if (!$statistic->isEnabled()) {
|
2018-12-03 13:44:26 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 15:50:06 +00:00
|
|
|
echo 'posted';
|
|
|
|
|
$this->client
|
|
|
|
|
->postAsync(
|
|
|
|
|
action([WebsocketStatisticsEntriesController::class, 'store']),
|
|
|
|
|
$statistic->toArray()
|
|
|
|
|
)
|
|
|
|
|
->then(function() {
|
|
|
|
|
echo 'fulfilled';
|
|
|
|
|
})
|
|
|
|
|
->otherwise(function() {
|
|
|
|
|
echo 'rejected!';
|
|
|
|
|
var_dump(func_get_args());
|
|
|
|
|
});
|
2018-12-03 13:44:26 +00:00
|
|
|
|
|
|
|
|
// Reset connection and message count
|
|
|
|
|
$currentConnectionCount = collect($this->channelManager->getChannels($appId))
|
|
|
|
|
->sum(function ($channel) {
|
|
|
|
|
return count($channel->getSubscribedConnections());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$statistic->reset($currentConnectionCount);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|