laravel-websockets/src/Statistics/Statistic.php

190 lines
3.9 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
*/
2020-09-10 19:59:26 +00:00
protected $currentConnectionsCount = 0;
2018-12-03 12:02:51 +00:00
2020-08-18 17:21:22 +00:00
/**
* The peak connections count ticker.
*
* @var int
*/
2020-09-10 19:59:26 +00:00
protected $peakConnectionsCount = 0;
2018-12-03 12:02:51 +00:00
2020-08-18 17:21:22 +00:00
/**
* The websockets connections count ticker.
*
* @var int
*/
2020-09-10 19:59:26 +00:00
protected $webSocketMessagesCount = 0;
2018-12-03 12:02:51 +00:00
2020-08-18 17:21:22 +00:00
/**
* The api messages connections count ticker.
*
* @var int
*/
2020-09-10 19:59:26 +00:00
protected $apiMessagesCount = 0;
2018-12-03 12:02:51 +00:00
2020-08-18 17:21:22 +00:00
/**
* Create a new statistic.
*
2020-09-10 19:59:26 +00:00
* @param string|int $appId
2020-08-18 17:21:22 +00:00
* @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-09-10 19:59:26 +00:00
/**
* Set the current connections count.
*
* @param int $currentConnectionsCount
* @return $this
*/
public function setCurrentConnectionsCount(int $currentConnectionsCount)
{
$this->currentConnectionsCount = $currentConnectionsCount;
return $this;
}
/**
* Set the peak connections count.
*
* @param int $peakConnectionsCount
* @return $this
*/
public function setPeakConnectionsCount(int $peakConnectionsCount)
{
$this->peakConnectionsCount = $peakConnectionsCount;
return $this;
}
/**
* Set the peak connections count.
*
* @param int $webSocketMessagesCount
* @return $this
*/
public function setWebSocketMessagesCount(int $webSocketMessagesCount)
{
$this->webSocketMessagesCount = $webSocketMessagesCount;
return $this;
}
/**
* Set the peak connections count.
*
* @param int $apiMessagesCount
* @return $this
*/
public function setApiMessagesCount(int $apiMessagesCount)
{
$this->apiMessagesCount = $apiMessagesCount;
return $this;
}
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
{
2020-09-10 19:59:26 +00:00
$this->currentConnectionsCount++;
2018-12-03 12:02:51 +00:00
2020-09-10 19:59:26 +00:00
$this->peakConnectionsCount = max($this->currentConnectionsCount, $this->peakConnectionsCount);
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
{
2020-09-10 19:59:26 +00:00
$this->currentConnectionsCount--;
2018-12-03 12:02:51 +00:00
2020-09-10 19:59:26 +00:00
$this->peakConnectionsCount = max($this->currentConnectionsCount, $this->peakConnectionsCount);
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
{
2020-09-10 19:59:26 +00:00
$this->webSocketMessagesCount++;
2018-12-03 12:02:51 +00:00
}
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
{
2020-09-10 19:59:26 +00:00
$this->apiMessagesCount++;
2018-12-03 12:02:51 +00:00
}
2020-08-18 17:21:22 +00:00
/**
* Reset all the connections to a specific count.
*
2020-09-10 19:59:26 +00:00
* @param int $currentConnectionsCount
2020-08-18 17:21:22 +00:00
* @return void
*/
2020-09-10 19:59:26 +00:00
public function reset(int $currentConnectionsCount)
2018-12-03 12:02:51 +00:00
{
2020-09-10 19:59:26 +00:00
$this->currentConnectionsCount = $currentConnectionsCount;
$this->peakConnectionsCount = $currentConnectionsCount;
$this->webSocketMessagesCount = 0;
$this->apiMessagesCount = 0;
2018-12-03 12:02:51 +00:00
}
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,
2020-09-10 19:59:26 +00:00
'peak_connections_count' => $this->peakConnectionsCount,
'websocket_messages_count' => $this->webSocketMessagesCount,
'api_messages_count' => $this->apiMessagesCount,
2018-12-03 12:02:51 +00:00
];
}
2018-12-04 21:22:33 +00:00
}