This commit is contained in:
Alex Renoki 2020-08-23 20:41:17 +03:00
parent f62ac8fd56
commit 499a153a0a
3 changed files with 69 additions and 5 deletions

View File

@ -5,7 +5,7 @@ namespace BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers;
use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver;
use Illuminate\Http\Request;
class DashboardApiController
class ShowStatistics
{
/**
* Get statistics for an app ID.
@ -15,7 +15,7 @@ class DashboardApiController
* @param mixed $appId
* @return \Illuminate\Http\Response
*/
public function getStatistics(Request $request, StatisticsDriver $driver, $appId)
public function __invoke(Request $request, StatisticsDriver $driver, $appId)
{
return $driver::get($appId, $request);
}

View File

@ -4,7 +4,7 @@ namespace BeyondCode\LaravelWebSockets;
use BeyondCode\LaravelWebSockets\Apps\AppManager;
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\AuthenticateDashboard;
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\DashboardApiController;
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowStatistics;
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\SendMessage;
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard;
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize as AuthorizeDashboard;
@ -124,9 +124,9 @@ class WebSocketsServiceProvider extends ServiceProvider
'middleware' => config('websockets.dashboard.middleware', [AuthorizeDashboard::class]),
], function () {
Route::get('/', ShowDashboard::class)->name('dashboard');
Route::get('/api/{appId}/statistics', [DashboardApiController::class, 'getStatistics'])->name('statistics');
Route::get('/api/{appId}/statistics', ShowStatistics::class)->name('statistics');
Route::post('/auth', AuthenticateDashboard::class)->name('auth');
Route::post('/event', SendMessage::class)->name('send');
Route::post('/event', SendMessage::class)->name('event');
});
return $this;

View File

@ -0,0 +1,64 @@
<?php
namespace BeyondCode\LaravelWebSockets\Tests\Dashboard;
use BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger;
use BeyondCode\LaravelWebSockets\Tests\TestCase;
use BeyondCode\LaravelWebSockets\Tests\Models\User;
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
class StatisticsTest extends TestCase
{
/** @test */
public function can_get_statistics()
{
$connection = $this->getConnectedWebSocketConnection(['channel-1']);
$logger = new MemoryStatisticsLogger(
$this->channelManager,
$this->statisticsDriver
);
$logger->webSocketMessage($connection->app->id);
$logger->apiMessage($connection->app->id);
$logger->connection($connection->app->id);
$logger->disconnection($connection->app->id);
$logger->save();
$this->actingAs(factory(User::class)->create())
->json('GET', route('laravel-websockets.statistics', ['appId' => '1234']))
->assertResponseOk()
->seeJsonStructure([
'peak_connections' => ['x', 'y'],
'websocket_message_count' => ['x', 'y'],
'api_message_count' => ['x', 'y'],
]);
}
/** @test */
public function cant_get_statistics_for_invalid_app_id()
{
$connection = $this->getConnectedWebSocketConnection(['channel-1']);
$logger = new MemoryStatisticsLogger(
$this->channelManager,
$this->statisticsDriver
);
$logger->webSocketMessage($connection->app->id);
$logger->apiMessage($connection->app->id);
$logger->connection($connection->app->id);
$logger->disconnection($connection->app->id);
$logger->save();
$this->actingAs(factory(User::class)->create())
->json('GET', route('laravel-websockets.statistics', ['appId' => 'not_found']))
->seeJson([
'peak_connections' => ['x' => [], 'y' => []],
'websocket_message_count' => ['x' => [], 'y' => []],
'api_message_count' => ['x' => [], 'y' => []],
]);
}
}