Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit cdf229b

Browse files
committed
wip
1 parent 84b8895 commit cdf229b

File tree

8 files changed

+66
-10
lines changed

8 files changed

+66
-10
lines changed

resources/views/dashboard.blade.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@
138138
});
139139
140140
this.subscribeToAllChannels();
141+
142+
this.subscribeToStatistics();
141143
},
142144
143145
disconnect() {
@@ -178,6 +180,16 @@
178180
});
179181
},
180182
183+
subscribeToStatistics() {
184+
this.pusher.subscribe('{{ \BeyondCode\LaravelWebSockets\Dashboard\DashboardLogger::LOG_CHANNEL_PREFIX }}statistics')
185+
.bind('statistics-updated', (data) => {
186+
this.chart.push([{
187+
time: data.time,
188+
y: data.peak_connection_count
189+
}]);
190+
});
191+
},
192+
181193
getBadgeClass(log) {
182194
if (log.type === 'occupied' || log.type === 'connection') {
183195
return 'badge-primary';

src/Console/StartWebSocketServer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected function configureStatisticsLogger()
5858
return new HttpStatisticsLogger(app(ChannelManager::class), $browser);
5959
});
6060

61-
$this->loop->addPeriodicTimer(60, function() {
61+
$this->loop->addPeriodicTimer(5, function() {
6262
StatisticsLogger::save();
6363
});
6464

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace BeyondCode\LaravelWebsockets\Statistics\Events;
4+
5+
use BeyondCode\LaravelWebSockets\Dashboard\DashboardLogger;
6+
use Illuminate\Broadcasting\PrivateChannel;
7+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
8+
use Illuminate\Queue\SerializesModels;
9+
10+
class StatisticsUpdated implements ShouldBroadcast
11+
{
12+
use SerializesModels;
13+
14+
protected $statisticModel;
15+
16+
public function __construct($statisticModel)
17+
{
18+
$this->statisticModel = $statisticModel;
19+
}
20+
21+
public function broadcastWith()
22+
{
23+
return [
24+
'time' => $this->statisticModel->created_at->timestamp,
25+
'app_id' => $this->statisticModel->appId,
26+
'peak_connection_count' => $this->statisticModel->peakConnectionCount,
27+
'websocket_message_count' => $this->statisticModel->webSocketMessageCount,
28+
'api_message_count' => $this->statisticModel->apiMessageCount,
29+
];
30+
}
31+
32+
public function broadcastOn()
33+
{
34+
return new PrivateChannel(str_after(DashboardLogger::LOG_CHANNEL_PREFIX . 'statistics', 'private-'));
35+
}
36+
37+
public function broadcastAs()
38+
{
39+
return 'statistics-updated';
40+
}
41+
}

src/Statistics/Http/Controllers/WebsocketStatisticsEntriesController.php renamed to src/Statistics/Http/Controllers/WebSocketStatisticsEntriesController.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace BeyondCode\LaravelWebSockets\Statistics\Http\Controllers;
44

5+
use BeyondCode\LaravelWebSockets\Statistics\Events\StatisticsUpdated;
56
use BeyondCode\LaravelWebSockets\Statistics\Rules\AppId;
67
use Illuminate\Http\Request;
78

8-
class WebsocketStatisticsEntriesController
9+
class WebSocketStatisticsEntriesController
910
{
1011
public function store(Request $request)
1112
{
@@ -18,7 +19,9 @@ public function store(Request $request)
1819

1920
$webSocketsStatisticsEntryModelClass = config('websockets.statistics_model');
2021

21-
$webSocketsStatisticsEntryModelClass::create($validatedAttributes);
22+
$statisticModel = $webSocketsStatisticsEntryModelClass::create($validatedAttributes);
23+
24+
broadcast(new StatisticsUpdated($statisticModel));
2225

2326
return 'ok';
2427
}

src/Statistics/Logger/FakeStatisticsLogger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace BeyondCode\LaravelWebSockets\Statistics\Logger;
44

5-
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController;
5+
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController;
66
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
77
use GuzzleHttp\Client;
88
use Ratchet\ConnectionInterface;

src/Statistics/Logger/HttpStatisticsLogger.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace BeyondCode\LaravelWebSockets\Statistics\Logger;
44

5-
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController;
5+
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController;
66
use BeyondCode\LaravelWebSockets\Statistics\Statistic;
77
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
88
use Clue\React\Buzz\Browser;
@@ -72,7 +72,7 @@ public function save()
7272

7373
$this->browser
7474
->post(
75-
action([WebsocketStatisticsEntriesController::class, 'store']),
75+
action([WebSocketStatisticsEntriesController::class, 'store']),
7676
['Content-Type' => 'application/json'],
7777
stream_for(json_encode($statistic->toArray()))
7878
);

src/WebSocketsServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard;
99
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize;
1010
use BeyondCode\LaravelWebSockets\Server\Router;
11-
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController;
11+
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController;
1212
use BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger;
1313
use Illuminate\Support\Facades\Gate;
1414
use Illuminate\Support\Facades\Route;
@@ -76,7 +76,7 @@ protected function registerRouteMacro()
7676
protected function registerStatisticRoute()
7777
{
7878
Route::prefix('/laravel-websockets')->namespace('\\')->group(function() {
79-
Route::post('statistics', [WebsocketStatisticsEntriesController::class, 'store']);
79+
Route::post('statistics', [WebSocketStatisticsEntriesController::class, 'store']);
8080
});
8181
}
8282

tests/Statistics/Controllers/WebSocketsStatisticsControllerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace BeyondCode\LaravelWebSockets\Tests\Statistics\Controllers;
44

5-
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController;
5+
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController;
66
use BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry;
77
use BeyondCode\LaravelWebSockets\Tests\TestCase;
88

@@ -12,7 +12,7 @@ class WebSocketsStatisticsControllerTest extends TestCase
1212
public function it_can_store_statistics()
1313
{
1414
$this->post(
15-
action([WebsocketStatisticsEntriesController::class, 'store']),
15+
action([WebSocketStatisticsEntriesController::class, 'store']),
1616
$this->payload()
1717
);
1818

0 commit comments

Comments
 (0)