laravel-websockets/src/Statistics/Statistic.php

138 lines
2.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
{
2020-08-18 17:21:22 +00:00
/**
* The app id.
*
* @var mixed
*/
2018-12-03 12:02:51 +00:00
protected $appId;
2020-08-18 17:21:22 +00:00
/**
* The current connections count ticker.
*
* @var int
*/
2018-12-03 13:35:00 +00:00
protected $currentConnectionCount = 0;
2018-12-03 12:02:51 +00:00
2020-08-18 17:21:22 +00:00
/**
* The peak connections count ticker.
*
* @var int
*/
2018-12-03 13:35:00 +00:00
protected $peakConnectionCount = 0;
2018-12-03 12:02:51 +00:00
2020-08-18 17:21:22 +00:00
/**
* The websockets connections count ticker.
*
* @var int
*/
2018-12-03 12:02:51 +00:00
protected $webSocketMessageCount = 0;
2020-08-18 17:21:22 +00:00
/**
* The api messages connections count ticker.
*
* @var int
*/
2018-12-03 12:02:51 +00:00
protected $apiMessageCount = 0;
2020-08-18 17:21:22 +00:00
/**
* Create a new statistic.
*
* @param mixed $appId
* @return void
*/
2018-12-03 12:02:51 +00:00
public function __construct($appId)
{
$this->appId = $appId;
2018-12-03 12:35:17 +00:00
}
2020-08-18 17:21:22 +00:00
/**
* Check if the app has statistics enabled.
*
* @return bool
*/
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
}
2020-08-18 17:21:22 +00:00
/**
* Handle a new connection increment.
*
* @return void
*/
2018-12-03 15:23:20 +00:00
public function connection()
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
}
2020-08-18 17:21:22 +00:00
/**
* Handle a disconnection decrement.
*
* @return void
*/
2018-12-03 15:23:20 +00:00
public function disconnection()
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
}
2020-08-18 17:21:22 +00:00
/**
* Handle a new websocket message.
*
* @return void
*/
2018-12-03 15:23:20 +00:00
public function webSocketMessage()
2018-12-03 12:02:51 +00:00
{
$this->webSocketMessageCount++;
}
2020-08-18 17:21:22 +00:00
/**
* Handle a new api message.
*
* @return void
*/
2018-12-03 15:23:20 +00:00
public function apiMessage()
2018-12-03 12:02:51 +00:00
{
$this->apiMessageCount++;
}
2020-08-18 17:21:22 +00:00
/**
* Reset all the connections to a specific count.
*
* @param int $currentConnectionCount
* @return void
*/
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;
}
2020-08-18 17:21:22 +00:00
/**
* Transform the statistic to array.
*
* @return array
*/
2018-12-03 12:02:51 +00:00
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,
];
}
2018-12-04 21:22:33 +00:00
}