This commit is contained in:
Marcel Pociot 2018-12-03 23:19:46 +01:00
parent 84b8895ff8
commit cdf229bfa1
8 changed files with 66 additions and 10 deletions

View File

@ -138,6 +138,8 @@
}); });
this.subscribeToAllChannels(); this.subscribeToAllChannels();
this.subscribeToStatistics();
}, },
disconnect() { 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) { getBadgeClass(log) {
if (log.type === 'occupied' || log.type === 'connection') { if (log.type === 'occupied' || log.type === 'connection') {
return 'badge-primary'; return 'badge-primary';

View File

@ -58,7 +58,7 @@ class StartWebSocketServer extends Command
return new HttpStatisticsLogger(app(ChannelManager::class), $browser); return new HttpStatisticsLogger(app(ChannelManager::class), $browser);
}); });
$this->loop->addPeriodicTimer(60, function() { $this->loop->addPeriodicTimer(5, function() {
StatisticsLogger::save(); StatisticsLogger::save();
}); });

View File

@ -0,0 +1,41 @@
<?php
namespace BeyondCode\LaravelWebsockets\Statistics\Events;
use BeyondCode\LaravelWebSockets\Dashboard\DashboardLogger;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Queue\SerializesModels;
class StatisticsUpdated implements ShouldBroadcast
{
use SerializesModels;
protected $statisticModel;
public function __construct($statisticModel)
{
$this->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';
}
}

View File

@ -2,10 +2,11 @@
namespace BeyondCode\LaravelWebSockets\Statistics\Http\Controllers; namespace BeyondCode\LaravelWebSockets\Statistics\Http\Controllers;
use BeyondCode\LaravelWebSockets\Statistics\Events\StatisticsUpdated;
use BeyondCode\LaravelWebSockets\Statistics\Rules\AppId; use BeyondCode\LaravelWebSockets\Statistics\Rules\AppId;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class WebsocketStatisticsEntriesController class WebSocketStatisticsEntriesController
{ {
public function store(Request $request) public function store(Request $request)
{ {
@ -18,7 +19,9 @@ class WebsocketStatisticsEntriesController
$webSocketsStatisticsEntryModelClass = config('websockets.statistics_model'); $webSocketsStatisticsEntryModelClass = config('websockets.statistics_model');
$webSocketsStatisticsEntryModelClass::create($validatedAttributes); $statisticModel = $webSocketsStatisticsEntryModelClass::create($validatedAttributes);
broadcast(new StatisticsUpdated($statisticModel));
return 'ok'; return 'ok';
} }

View File

@ -2,7 +2,7 @@
namespace BeyondCode\LaravelWebSockets\Statistics\Logger; 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 BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;

View File

@ -2,7 +2,7 @@
namespace BeyondCode\LaravelWebSockets\Statistics\Logger; 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\Statistics\Statistic;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use Clue\React\Buzz\Browser; use Clue\React\Buzz\Browser;
@ -72,7 +72,7 @@ class HttpStatisticsLogger implements StatisticsLogger
$this->browser $this->browser
->post( ->post(
action([WebsocketStatisticsEntriesController::class, 'store']), action([WebSocketStatisticsEntriesController::class, 'store']),
['Content-Type' => 'application/json'], ['Content-Type' => 'application/json'],
stream_for(json_encode($statistic->toArray())) stream_for(json_encode($statistic->toArray()))
); );

View File

@ -8,7 +8,7 @@ use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\SendMessage;
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard; use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard;
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize; use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize;
use BeyondCode\LaravelWebSockets\Server\Router; 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 BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger;
use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
@ -76,7 +76,7 @@ class WebSocketsServiceProvider extends ServiceProvider
protected function registerStatisticRoute() protected function registerStatisticRoute()
{ {
Route::prefix('/laravel-websockets')->namespace('\\')->group(function() { Route::prefix('/laravel-websockets')->namespace('\\')->group(function() {
Route::post('statistics', [WebsocketStatisticsEntriesController::class, 'store']); Route::post('statistics', [WebSocketStatisticsEntriesController::class, 'store']);
}); });
} }

View File

@ -2,7 +2,7 @@
namespace BeyondCode\LaravelWebSockets\Tests\Statistics\Controllers; 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\Statistics\Models\WebSocketsStatisticsEntry;
use BeyondCode\LaravelWebSockets\Tests\TestCase; use BeyondCode\LaravelWebSockets\Tests\TestCase;
@ -12,7 +12,7 @@ class WebSocketsStatisticsControllerTest extends TestCase
public function it_can_store_statistics() public function it_can_store_statistics()
{ {
$this->post( $this->post(
action([WebsocketStatisticsEntriesController::class, 'store']), action([WebSocketStatisticsEntriesController::class, 'store']),
$this->payload() $this->payload()
); );