wip
This commit is contained in:
parent
95e519e499
commit
3d94b97158
|
|
@ -14,7 +14,7 @@ class CreateWebSocketsStatisticsEntriesTable extends Migration
|
|||
Schema::create('websockets_statistics_entries', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('app_id');
|
||||
$table->integer('peak_connections');
|
||||
$table->integer('peak_connection_count');
|
||||
$table->integer('websocket_message_count');
|
||||
$table->integer('api_message_count');
|
||||
$table->nullableTimestamps();
|
||||
|
|
|
|||
|
|
@ -7,10 +7,13 @@ use BeyondCode\LaravelWebSockets\Facades\WebSocketsRouter;
|
|||
use BeyondCode\LaravelWebSockets\Server\Logger\ConnectionLogger;
|
||||
use BeyondCode\LaravelWebSockets\Server\Logger\HttpLogger;
|
||||
use BeyondCode\LaravelWebSockets\Server\Logger\WebsocketsLogger;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use Illuminate\Console\Command;
|
||||
use BeyondCode\LaravelWebSockets\Server\WebSocketServerFactory;
|
||||
|
||||
use React\EventLoop\Factory as LoopFactory;
|
||||
use WyriHaximus\React\GuzzlePsr7\HttpClientAdapter;
|
||||
|
||||
class StartWebSocketServer extends Command
|
||||
{
|
||||
|
|
@ -74,10 +77,10 @@ class StartWebSocketServer extends Command
|
|||
|
||||
protected function configureStatisticsLogger()
|
||||
{
|
||||
$handler = new \WyriHaximus\React\GuzzlePsr7\HttpClientAdapter($this->loop);
|
||||
$handler = new HttpClientAdapter($this->loop);
|
||||
|
||||
$client = new \GuzzleHttp\Client([
|
||||
'handler' => \GuzzleHttp\HandlerStack::create($handler),
|
||||
$client = new Client([
|
||||
'handler' => HandlerStack::create($handler),
|
||||
]);
|
||||
|
||||
app()->singleton('websockets.statisticslogger', function() use ($client) {
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ class TriggerEventController extends Controller
|
|||
$request->json()->get('data')
|
||||
);
|
||||
|
||||
StatisticsLogger::apiMessage($request->appId);
|
||||
StatisticsLogger::logApiMessage($request->appId);
|
||||
}
|
||||
|
||||
return $request->json()->all();
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ class WebsocketStatisticsEntriesController
|
|||
{
|
||||
$validatedAttributes = $request->validate([
|
||||
'app_id' => ['required', new AppId()],
|
||||
'peak_connections' => 'required|integer',
|
||||
'peak_connection_count' => 'required|integer',
|
||||
'websocket_message_count' => 'required|integer',
|
||||
'api_message_count' => 'required|integer',
|
||||
]);
|
||||
|
|
|
|||
|
|
@ -10,10 +10,10 @@ class Statistic
|
|||
protected $appId;
|
||||
|
||||
/** @var int */
|
||||
protected $connections = 0;
|
||||
protected $currentConnectionCount = 0;
|
||||
|
||||
/** @var int */
|
||||
protected $peakConnections = 0;
|
||||
protected $peakConnectionCount = 0;
|
||||
|
||||
/** @var int */
|
||||
protected $webSocketMessageCount = 0;
|
||||
|
|
@ -31,34 +31,34 @@ class Statistic
|
|||
return App::findById($this->appId)->statisticsEnabled;
|
||||
}
|
||||
|
||||
public function connection()
|
||||
public function logConnection()
|
||||
{
|
||||
$this->connections++;
|
||||
$this->currentConnectionCount++;
|
||||
|
||||
$this->peakConnections = max($this->connections, $this->peakConnections);
|
||||
$this->peakConnectionCount = max($this->currentConnectionCount, $this->peakConnectionCount);
|
||||
}
|
||||
|
||||
public function disconnection()
|
||||
public function logDisconnection()
|
||||
{
|
||||
$this->connections--;
|
||||
$this->currentConnectionCount--;
|
||||
|
||||
$this->peakConnections = max($this->connections, $this->peakConnections);
|
||||
$this->peakConnectionCount = max($this->currentConnectionCount, $this->peakConnectionCount);
|
||||
}
|
||||
|
||||
public function webSocketMessage()
|
||||
public function logWebSocketMessage()
|
||||
{
|
||||
$this->webSocketMessageCount++;
|
||||
}
|
||||
|
||||
public function apiMessage()
|
||||
public function logApiMessage()
|
||||
{
|
||||
$this->apiMessageCount++;
|
||||
}
|
||||
|
||||
public function reset(int $currentConnectionCount)
|
||||
{
|
||||
$this->connections = $currentConnectionCount;
|
||||
$this->peakConnections = $currentConnectionCount;
|
||||
$this->currentConnectionCount = $currentConnectionCount;
|
||||
$this->peakConnectionCount = $currentConnectionCount;
|
||||
$this->webSocketMessageCount = 0;
|
||||
$this->apiMessageCount = 0;
|
||||
}
|
||||
|
|
@ -67,7 +67,7 @@ class Statistic
|
|||
{
|
||||
return [
|
||||
'app_id' => $this->appId,
|
||||
'peak_connections' => $this->peakConnections,
|
||||
'peak_connection_count' => $this->peakConnectionCount,
|
||||
'websocket_message_count' => $this->webSocketMessageCount,
|
||||
'api_message_count' => $this->apiMessageCount,
|
||||
];
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ namespace BeyondCode\LaravelWebSockets\Statistics\Logging;
|
|||
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController;
|
||||
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
|
||||
use GuzzleHttp\Client;
|
||||
use Illuminate\Support\Collection;
|
||||
use Ratchet\ConnectionInterface;
|
||||
|
||||
class StatisticsLogger
|
||||
|
|
@ -23,32 +22,32 @@ class StatisticsLogger
|
|||
$this->client = $client;
|
||||
}
|
||||
|
||||
public function webSocketMessage(ConnectionInterface $connection)
|
||||
public function logWebSocketMessage(ConnectionInterface $connection)
|
||||
{
|
||||
$this->initializeStatistics($connection->app->id);
|
||||
|
||||
$this->statistics[$connection->app->id]->webSocketMessage();
|
||||
$this->statistics[$connection->app->id]->logWebSocketMessage();
|
||||
}
|
||||
|
||||
public function apiMessage($appId)
|
||||
public function logApiMessage($appId)
|
||||
{
|
||||
$this->initializeStatistics($appId);
|
||||
|
||||
$this->statistics[$appId]->apiMessage();
|
||||
$this->statistics[$appId]->logApiMessage();
|
||||
}
|
||||
|
||||
public function connection(ConnectionInterface $connection)
|
||||
public function logConnection(ConnectionInterface $connection)
|
||||
{
|
||||
$this->initializeStatistics($connection->app->id);
|
||||
|
||||
$this->statistics[$connection->app->id]->connection();
|
||||
$this->statistics[$connection->app->id]->logConnection();
|
||||
}
|
||||
|
||||
public function disconnection(ConnectionInterface $connection)
|
||||
public function logDisconnection(ConnectionInterface $connection)
|
||||
{
|
||||
$this->initializeStatistics($connection->app->id);
|
||||
|
||||
$this->statistics[$connection->app->id]->disconnection();
|
||||
$this->statistics[$connection->app->id]->logDisconnection();
|
||||
}
|
||||
|
||||
protected function initializeStatistics($id)
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ class WebSocketHandler implements MessageComponentInterface
|
|||
|
||||
$message->respond();
|
||||
|
||||
StatisticsLogger::webSocketMessage($connection);
|
||||
StatisticsLogger::logWebSocketMessage($connection);
|
||||
}
|
||||
|
||||
public function onClose(ConnectionInterface $connection)
|
||||
|
|
@ -48,7 +48,7 @@ class WebSocketHandler implements MessageComponentInterface
|
|||
|
||||
DashboardLogger::disconnection($connection);
|
||||
|
||||
StatisticsLogger::disconnection($connection);
|
||||
StatisticsLogger::logDisconnection($connection);
|
||||
}
|
||||
|
||||
public function onError(ConnectionInterface $connection, Exception $exception)
|
||||
|
|
@ -94,7 +94,7 @@ class WebSocketHandler implements MessageComponentInterface
|
|||
|
||||
DashboardLogger::connection($connection);
|
||||
|
||||
StatisticsLogger::connection($connection);
|
||||
StatisticsLogger::logConnection($connection);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ class WebSocketsStatisticsControllerTest extends TestCase
|
|||
{
|
||||
return [
|
||||
'app_id' => config('websockets.apps.0.id'),
|
||||
'peak_connections' => 1,
|
||||
'peak_connection_count' => 1,
|
||||
'websocket_message_count' => 2,
|
||||
'api_message_count' => 3,
|
||||
];
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ namespace BeyondCode\LaravelWebSockets\Tests;
|
|||
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
|
||||
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
|
||||
use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use BeyondCode\LaravelWebSockets\Tests\Mocks\Connection;
|
||||
use BeyondCode\LaravelWebSockets\WebSocketsServiceProvider;
|
||||
|
|
|
|||
Loading…
Reference in New Issue