diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index 28521a6..9193f7a 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -138,6 +138,8 @@ }); this.subscribeToAllChannels(); + + this.subscribeToStatistics(); }, disconnect() { @@ -178,6 +180,16 @@ }); }, + subscribeToStatistics() { + this.pusher.subscribe('{{ \BeyondCode\LaravelWebSockets\Dashboard\DashboardLogger::LOG_CHANNEL_PREFIX }}statistics') + .bind('statistics-updated', (data) => { + this.chart.push([{ + time: data.time, + y: data.peak_connection_count + }]); + }); + }, + getBadgeClass(log) { if (log.type === 'occupied' || log.type === 'connection') { return 'badge-primary'; diff --git a/src/Console/StartWebSocketServer.php b/src/Console/StartWebSocketServer.php index cd625cc..6be87ba 100644 --- a/src/Console/StartWebSocketServer.php +++ b/src/Console/StartWebSocketServer.php @@ -58,7 +58,7 @@ class StartWebSocketServer extends Command return new HttpStatisticsLogger(app(ChannelManager::class), $browser); }); - $this->loop->addPeriodicTimer(60, function() { + $this->loop->addPeriodicTimer(5, function() { StatisticsLogger::save(); }); diff --git a/src/Statistics/Events/StatisticsUpdated.php b/src/Statistics/Events/StatisticsUpdated.php new file mode 100644 index 0000000..128a25b --- /dev/null +++ b/src/Statistics/Events/StatisticsUpdated.php @@ -0,0 +1,41 @@ +statisticModel = $statisticModel; + } + + public function broadcastWith() + { + return [ + 'time' => $this->statisticModel->created_at->timestamp, + 'app_id' => $this->statisticModel->appId, + 'peak_connection_count' => $this->statisticModel->peakConnectionCount, + 'websocket_message_count' => $this->statisticModel->webSocketMessageCount, + 'api_message_count' => $this->statisticModel->apiMessageCount, + ]; + } + + public function broadcastOn() + { + return new PrivateChannel(str_after(DashboardLogger::LOG_CHANNEL_PREFIX . 'statistics', 'private-')); + } + + public function broadcastAs() + { + return 'statistics-updated'; + } +} \ No newline at end of file diff --git a/src/Statistics/Http/Controllers/WebsocketStatisticsEntriesController.php b/src/Statistics/Http/Controllers/WebSocketStatisticsEntriesController.php similarity index 69% rename from src/Statistics/Http/Controllers/WebsocketStatisticsEntriesController.php rename to src/Statistics/Http/Controllers/WebSocketStatisticsEntriesController.php index 3f38d45..2279a0e 100644 --- a/src/Statistics/Http/Controllers/WebsocketStatisticsEntriesController.php +++ b/src/Statistics/Http/Controllers/WebSocketStatisticsEntriesController.php @@ -2,10 +2,11 @@ namespace BeyondCode\LaravelWebSockets\Statistics\Http\Controllers; +use BeyondCode\LaravelWebSockets\Statistics\Events\StatisticsUpdated; use BeyondCode\LaravelWebSockets\Statistics\Rules\AppId; use Illuminate\Http\Request; -class WebsocketStatisticsEntriesController +class WebSocketStatisticsEntriesController { public function store(Request $request) { @@ -18,7 +19,9 @@ class WebsocketStatisticsEntriesController $webSocketsStatisticsEntryModelClass = config('websockets.statistics_model'); - $webSocketsStatisticsEntryModelClass::create($validatedAttributes); + $statisticModel = $webSocketsStatisticsEntryModelClass::create($validatedAttributes); + + broadcast(new StatisticsUpdated($statisticModel)); return 'ok'; } diff --git a/src/Statistics/Logger/FakeStatisticsLogger.php b/src/Statistics/Logger/FakeStatisticsLogger.php index c1f8df3..d6bc759 100644 --- a/src/Statistics/Logger/FakeStatisticsLogger.php +++ b/src/Statistics/Logger/FakeStatisticsLogger.php @@ -2,7 +2,7 @@ namespace BeyondCode\LaravelWebSockets\Statistics\Logger; -use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController; +use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; use GuzzleHttp\Client; use Ratchet\ConnectionInterface; diff --git a/src/Statistics/Logger/HttpStatisticsLogger.php b/src/Statistics/Logger/HttpStatisticsLogger.php index d83316c..847f319 100644 --- a/src/Statistics/Logger/HttpStatisticsLogger.php +++ b/src/Statistics/Logger/HttpStatisticsLogger.php @@ -2,7 +2,7 @@ namespace BeyondCode\LaravelWebSockets\Statistics\Logger; -use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController; +use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController; use BeyondCode\LaravelWebSockets\Statistics\Statistic; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; use Clue\React\Buzz\Browser; @@ -72,7 +72,7 @@ class HttpStatisticsLogger implements StatisticsLogger $this->browser ->post( - action([WebsocketStatisticsEntriesController::class, 'store']), + action([WebSocketStatisticsEntriesController::class, 'store']), ['Content-Type' => 'application/json'], stream_for(json_encode($statistic->toArray())) ); diff --git a/src/WebSocketsServiceProvider.php b/src/WebSocketsServiceProvider.php index be699c7..0c6901b 100644 --- a/src/WebSocketsServiceProvider.php +++ b/src/WebSocketsServiceProvider.php @@ -8,7 +8,7 @@ use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\SendMessage; use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard; use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize; use BeyondCode\LaravelWebSockets\Server\Router; -use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController; +use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController; use BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger; use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Route; @@ -76,7 +76,7 @@ class WebSocketsServiceProvider extends ServiceProvider protected function registerStatisticRoute() { Route::prefix('/laravel-websockets')->namespace('\\')->group(function() { - Route::post('statistics', [WebsocketStatisticsEntriesController::class, 'store']); + Route::post('statistics', [WebSocketStatisticsEntriesController::class, 'store']); }); } diff --git a/tests/Statistics/Controllers/WebSocketsStatisticsControllerTest.php b/tests/Statistics/Controllers/WebSocketsStatisticsControllerTest.php index 960dea6..a3a566d 100644 --- a/tests/Statistics/Controllers/WebSocketsStatisticsControllerTest.php +++ b/tests/Statistics/Controllers/WebSocketsStatisticsControllerTest.php @@ -2,7 +2,7 @@ namespace BeyondCode\LaravelWebSockets\Tests\Statistics\Controllers; -use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController; +use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController; use BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry; use BeyondCode\LaravelWebSockets\Tests\TestCase; @@ -12,7 +12,7 @@ class WebSocketsStatisticsControllerTest extends TestCase public function it_can_store_statistics() { $this->post( - action([WebsocketStatisticsEntriesController::class, 'store']), + action([WebSocketStatisticsEntriesController::class, 'store']), $this->payload() );