diff --git a/database/migrations/create_websockets_statistics_entries_table.php.stub b/database/migrations/create_websockets_statistics_entries_table.php.stub index 0e42ba4..b56e525 100644 --- a/database/migrations/create_websockets_statistics_entries_table.php.stub +++ b/database/migrations/create_websockets_statistics_entries_table.php.stub @@ -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(); diff --git a/src/Console/StartWebSocketServer.php b/src/Console/StartWebSocketServer.php index 0f283f2..215bbfd 100644 --- a/src/Console/StartWebSocketServer.php +++ b/src/Console/StartWebSocketServer.php @@ -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) { diff --git a/src/HttpApi/Controllers/TriggerEventController.php b/src/HttpApi/Controllers/TriggerEventController.php index cd81729..53ef72d 100644 --- a/src/HttpApi/Controllers/TriggerEventController.php +++ b/src/HttpApi/Controllers/TriggerEventController.php @@ -28,7 +28,7 @@ class TriggerEventController extends Controller $request->json()->get('data') ); - StatisticsLogger::apiMessage($request->appId); + StatisticsLogger::logApiMessage($request->appId); } return $request->json()->all(); diff --git a/src/Statistics/Http/Controllers/WebsocketStatisticsEntriesController.php b/src/Statistics/Http/Controllers/WebsocketStatisticsEntriesController.php index 38c4d01..61a113e 100644 --- a/src/Statistics/Http/Controllers/WebsocketStatisticsEntriesController.php +++ b/src/Statistics/Http/Controllers/WebsocketStatisticsEntriesController.php @@ -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', ]); diff --git a/src/Statistics/Logging/Statistic.php b/src/Statistics/Logging/Statistic.php index e2e221c..81b52f6 100644 --- a/src/Statistics/Logging/Statistic.php +++ b/src/Statistics/Logging/Statistic.php @@ -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, ]; diff --git a/src/Statistics/Logging/StatisticsLogger.php b/src/Statistics/Logging/StatisticsLogger.php index 1ef5497..82a186a 100644 --- a/src/Statistics/Logging/StatisticsLogger.php +++ b/src/Statistics/Logging/StatisticsLogger.php @@ -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) diff --git a/src/WebSockets/WebSocketHandler.php b/src/WebSockets/WebSocketHandler.php index e2590fc..60c302c 100644 --- a/src/WebSockets/WebSocketHandler.php +++ b/src/WebSockets/WebSocketHandler.php @@ -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; } diff --git a/tests/Statistics/Controllers/WebSocketsStatisticsControllerTest.php b/tests/Statistics/Controllers/WebSocketsStatisticsControllerTest.php index 28bbb53..960dea6 100644 --- a/tests/Statistics/Controllers/WebSocketsStatisticsControllerTest.php +++ b/tests/Statistics/Controllers/WebSocketsStatisticsControllerTest.php @@ -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, ]; diff --git a/tests/TestCase.php b/tests/TestCase.php index be99228..c6c1983 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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;