2018-12-03 12:02:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace BeyondCode\LaravelWebSockets\Statistics\Logging;
|
|
|
|
|
|
2018-12-03 12:35:17 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Apps\App;
|
|
|
|
|
|
2018-12-03 12:02:51 +00:00
|
|
|
class Statistic
|
|
|
|
|
{
|
2018-12-03 12:33:00 +00:00
|
|
|
/** @var int|string */
|
2018-12-03 12:02:51 +00:00
|
|
|
protected $appId;
|
|
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
|
protected $connections = 0;
|
|
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
|
protected $peakConnections = 0;
|
|
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
|
protected $webSocketMessageCount = 0;
|
|
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
|
protected $apiMessageCount = 0;
|
|
|
|
|
|
|
|
|
|
public function __construct($appId)
|
|
|
|
|
{
|
|
|
|
|
$this->appId = $appId;
|
2018-12-03 12:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function isEnabled(): bool
|
|
|
|
|
{
|
|
|
|
|
return App::findById($this->appId)->statisticsEnabled;
|
2018-12-03 12:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function connection()
|
|
|
|
|
{
|
|
|
|
|
$this->connections++;
|
|
|
|
|
|
|
|
|
|
$this->peakConnections = max($this->connections, $this->peakConnections);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function disconnection()
|
|
|
|
|
{
|
|
|
|
|
$this->connections--;
|
|
|
|
|
|
|
|
|
|
$this->peakConnections = max($this->connections, $this->peakConnections);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function webSocketMessage()
|
|
|
|
|
{
|
|
|
|
|
$this->webSocketMessageCount++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function apiMessage()
|
|
|
|
|
{
|
|
|
|
|
$this->apiMessageCount++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function reset(int $connections)
|
|
|
|
|
{
|
|
|
|
|
$this->connections = $connections;
|
|
|
|
|
$this->peakConnections = $connections;
|
|
|
|
|
$this->webSocketMessageCount = 0;
|
|
|
|
|
$this->apiMessageCount = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function toArray()
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'app_id' => $this->appId,
|
|
|
|
|
'peak_connections' => $this->peakConnections,
|
|
|
|
|
'websocket_message_count' => $this->webSocketMessageCount,
|
|
|
|
|
'api_message_count' => $this->apiMessageCount,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|