laravel-websockets/src/Statistics/Statistic.php

75 lines
1.7 KiB
PHP
Raw Normal View History

2018-12-03 12:02:51 +00:00
<?php
2018-12-03 13:45:50 +00:00
namespace BeyondCode\LaravelWebSockets\Statistics;
2018-12-03 12:02:51 +00:00
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 */
2018-12-03 13:35:00 +00:00
protected $currentConnectionCount = 0;
2018-12-03 12:02:51 +00:00
/** @var int */
2018-12-03 13:35:00 +00:00
protected $peakConnectionCount = 0;
2018-12-03 12:02:51 +00:00
/** @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
}
2018-12-03 13:35:00 +00:00
public function logConnection()
2018-12-03 12:02:51 +00:00
{
2018-12-03 13:35:00 +00:00
$this->currentConnectionCount++;
2018-12-03 12:02:51 +00:00
2018-12-03 13:35:00 +00:00
$this->peakConnectionCount = max($this->currentConnectionCount, $this->peakConnectionCount);
2018-12-03 12:02:51 +00:00
}
2018-12-03 13:35:00 +00:00
public function logDisconnection()
2018-12-03 12:02:51 +00:00
{
2018-12-03 13:35:00 +00:00
$this->currentConnectionCount--;
2018-12-03 12:02:51 +00:00
2018-12-03 13:35:00 +00:00
$this->peakConnectionCount = max($this->currentConnectionCount, $this->peakConnectionCount);
2018-12-03 12:02:51 +00:00
}
2018-12-03 13:35:00 +00:00
public function logWebSocketMessage()
2018-12-03 12:02:51 +00:00
{
$this->webSocketMessageCount++;
}
2018-12-03 13:35:00 +00:00
public function logApiMessage()
2018-12-03 12:02:51 +00:00
{
$this->apiMessageCount++;
}
2018-12-03 13:24:54 +00:00
public function reset(int $currentConnectionCount)
2018-12-03 12:02:51 +00:00
{
2018-12-03 13:35:00 +00:00
$this->currentConnectionCount = $currentConnectionCount;
$this->peakConnectionCount = $currentConnectionCount;
2018-12-03 12:02:51 +00:00
$this->webSocketMessageCount = 0;
$this->apiMessageCount = 0;
}
public function toArray()
{
return [
'app_id' => $this->appId,
2018-12-03 13:35:00 +00:00
'peak_connection_count' => $this->peakConnectionCount,
2018-12-03 12:02:51 +00:00
'websocket_message_count' => $this->webSocketMessageCount,
'api_message_count' => $this->apiMessageCount,
];
}
}