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 21:22:33 +00:00
|
|
|
use Clue\React\Buzz\Browser;
|
|
|
|
|
use Ratchet\ConnectionInterface;
|
|
|
|
|
use function GuzzleHttp\Psr7\stream_for;
|
2018-12-04 09:15:37 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Apps\App;
|
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;
|
2018-12-04 21:22:33 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController;
|
2018-12-03 13:44:26 +00:00
|
|
|
|
|
|
|
|
class HttpStatisticsLogger implements StatisticsLogger
|
|
|
|
|
{
|
2018-12-03 22:45:18 +00:00
|
|
|
/** @var \BeyondCode\LaravelWebSockets\Statistics\Statistic[] */
|
2018-12-03 13:44:26 +00:00
|
|
|
protected $statistics = [];
|
|
|
|
|
|
|
|
|
|
/** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */
|
|
|
|
|
protected $channelManager;
|
|
|
|
|
|
2018-12-03 22:45:18 +00:00
|
|
|
/** @var \Clue\React\Buzz\Browser */
|
2018-12-03 20:06:49 +00:00
|
|
|
protected $browser;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2018-12-03 15:23:20 +00:00
|
|
|
public function webSocketMessage(ConnectionInterface $connection)
|
2018-12-03 13:44:26 +00:00
|
|
|
{
|
2018-12-03 22:45:18 +00:00
|
|
|
$this
|
|
|
|
|
->findOrMakeStatisticForAppId($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
|
|
|
{
|
2018-12-03 22:45:18 +00:00
|
|
|
$this
|
|
|
|
|
->findOrMakeStatisticForAppId($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
|
|
|
{
|
2018-12-03 22:45:18 +00:00
|
|
|
$this
|
|
|
|
|
->findOrMakeStatisticForAppId($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
|
|
|
{
|
2018-12-03 22:45:18 +00:00
|
|
|
$this
|
|
|
|
|
->findOrMakeStatisticForAppId($connection->app->id)
|
|
|
|
|
->disconnection();
|
2018-12-03 13:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
2018-12-03 22:45:18 +00:00
|
|
|
protected function findOrMakeStatisticForAppId($appId): Statistic
|
2018-12-03 13:44:26 +00:00
|
|
|
{
|
2018-12-04 21:22:33 +00:00
|
|
|
if (! isset($this->statistics[$appId])) {
|
2018-12-03 22:45:18 +00:00
|
|
|
$this->statistics[$appId] = new Statistic($appId);
|
2018-12-03 13:44:26 +00:00
|
|
|
}
|
2018-12-03 22:45:18 +00:00
|
|
|
|
|
|
|
|
return $this->statistics[$appId];
|
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
|
|
|
]);
|
|
|
|
|
|
2018-12-03 22:52:39 +00:00
|
|
|
$this
|
|
|
|
|
->browser
|
2018-12-03 20:06:49 +00:00
|
|
|
->post(
|
2020-08-13 05:32:20 +00:00
|
|
|
$this->storeStatisticsUrl(),
|
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);
|
2018-12-03 13:44:26 +00:00
|
|
|
$statistic->reset($currentConnectionCount);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-13 05:32:20 +00:00
|
|
|
|
|
|
|
|
protected function storeStatisticsUrl(): string
|
|
|
|
|
{
|
|
|
|
|
$action = [WebSocketStatisticsEntriesController::class, 'store'];
|
|
|
|
|
|
|
|
|
|
$overridenUrl = config('websockets.statistics.base_url_override');
|
|
|
|
|
|
2020-08-13 06:47:55 +00:00
|
|
|
return $overridentUrl
|
|
|
|
|
? $overridenUrl.action($action, [], false)
|
|
|
|
|
: action($action);
|
2020-08-13 05:32:20 +00:00
|
|
|
}
|
2018-12-04 21:22:33 +00:00
|
|
|
}
|