This commit is contained in:
freek 2018-12-03 14:35:00 +01:00
parent 95e519e499
commit 3d94b97158
9 changed files with 35 additions and 32 deletions

View File

@ -14,7 +14,7 @@ class CreateWebSocketsStatisticsEntriesTable extends Migration
Schema::create('websockets_statistics_entries', function (Blueprint $table) { Schema::create('websockets_statistics_entries', function (Blueprint $table) {
$table->increments('id'); $table->increments('id');
$table->string('app_id'); $table->string('app_id');
$table->integer('peak_connections'); $table->integer('peak_connection_count');
$table->integer('websocket_message_count'); $table->integer('websocket_message_count');
$table->integer('api_message_count'); $table->integer('api_message_count');
$table->nullableTimestamps(); $table->nullableTimestamps();

View File

@ -7,10 +7,13 @@ use BeyondCode\LaravelWebSockets\Facades\WebSocketsRouter;
use BeyondCode\LaravelWebSockets\Server\Logger\ConnectionLogger; use BeyondCode\LaravelWebSockets\Server\Logger\ConnectionLogger;
use BeyondCode\LaravelWebSockets\Server\Logger\HttpLogger; use BeyondCode\LaravelWebSockets\Server\Logger\HttpLogger;
use BeyondCode\LaravelWebSockets\Server\Logger\WebsocketsLogger; use BeyondCode\LaravelWebSockets\Server\Logger\WebsocketsLogger;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use BeyondCode\LaravelWebSockets\Server\WebSocketServerFactory; use BeyondCode\LaravelWebSockets\Server\WebSocketServerFactory;
use React\EventLoop\Factory as LoopFactory; use React\EventLoop\Factory as LoopFactory;
use WyriHaximus\React\GuzzlePsr7\HttpClientAdapter;
class StartWebSocketServer extends Command class StartWebSocketServer extends Command
{ {
@ -74,10 +77,10 @@ class StartWebSocketServer extends Command
protected function configureStatisticsLogger() protected function configureStatisticsLogger()
{ {
$handler = new \WyriHaximus\React\GuzzlePsr7\HttpClientAdapter($this->loop); $handler = new HttpClientAdapter($this->loop);
$client = new \GuzzleHttp\Client([ $client = new Client([
'handler' => \GuzzleHttp\HandlerStack::create($handler), 'handler' => HandlerStack::create($handler),
]); ]);
app()->singleton('websockets.statisticslogger', function() use ($client) { app()->singleton('websockets.statisticslogger', function() use ($client) {

View File

@ -28,7 +28,7 @@ class TriggerEventController extends Controller
$request->json()->get('data') $request->json()->get('data')
); );
StatisticsLogger::apiMessage($request->appId); StatisticsLogger::logApiMessage($request->appId);
} }
return $request->json()->all(); return $request->json()->all();

View File

@ -12,7 +12,7 @@ class WebsocketStatisticsEntriesController
{ {
$validatedAttributes = $request->validate([ $validatedAttributes = $request->validate([
'app_id' => ['required', new AppId()], 'app_id' => ['required', new AppId()],
'peak_connections' => 'required|integer', 'peak_connection_count' => 'required|integer',
'websocket_message_count' => 'required|integer', 'websocket_message_count' => 'required|integer',
'api_message_count' => 'required|integer', 'api_message_count' => 'required|integer',
]); ]);

View File

@ -10,10 +10,10 @@ class Statistic
protected $appId; protected $appId;
/** @var int */ /** @var int */
protected $connections = 0; protected $currentConnectionCount = 0;
/** @var int */ /** @var int */
protected $peakConnections = 0; protected $peakConnectionCount = 0;
/** @var int */ /** @var int */
protected $webSocketMessageCount = 0; protected $webSocketMessageCount = 0;
@ -31,34 +31,34 @@ class Statistic
return App::findById($this->appId)->statisticsEnabled; 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++; $this->webSocketMessageCount++;
} }
public function apiMessage() public function logApiMessage()
{ {
$this->apiMessageCount++; $this->apiMessageCount++;
} }
public function reset(int $currentConnectionCount) public function reset(int $currentConnectionCount)
{ {
$this->connections = $currentConnectionCount; $this->currentConnectionCount = $currentConnectionCount;
$this->peakConnections = $currentConnectionCount; $this->peakConnectionCount = $currentConnectionCount;
$this->webSocketMessageCount = 0; $this->webSocketMessageCount = 0;
$this->apiMessageCount = 0; $this->apiMessageCount = 0;
} }
@ -67,7 +67,7 @@ class Statistic
{ {
return [ return [
'app_id' => $this->appId, 'app_id' => $this->appId,
'peak_connections' => $this->peakConnections, 'peak_connection_count' => $this->peakConnectionCount,
'websocket_message_count' => $this->webSocketMessageCount, 'websocket_message_count' => $this->webSocketMessageCount,
'api_message_count' => $this->apiMessageCount, 'api_message_count' => $this->apiMessageCount,
]; ];

View File

@ -5,7 +5,6 @@ namespace BeyondCode\LaravelWebSockets\Statistics\Logging;
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 Illuminate\Support\Collection;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;
class StatisticsLogger class StatisticsLogger
@ -23,32 +22,32 @@ class StatisticsLogger
$this->client = $client; $this->client = $client;
} }
public function webSocketMessage(ConnectionInterface $connection) public function logWebSocketMessage(ConnectionInterface $connection)
{ {
$this->initializeStatistics($connection->app->id); $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->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->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->initializeStatistics($connection->app->id);
$this->statistics[$connection->app->id]->disconnection(); $this->statistics[$connection->app->id]->logDisconnection();
} }
protected function initializeStatistics($id) protected function initializeStatistics($id)

View File

@ -39,7 +39,7 @@ class WebSocketHandler implements MessageComponentInterface
$message->respond(); $message->respond();
StatisticsLogger::webSocketMessage($connection); StatisticsLogger::logWebSocketMessage($connection);
} }
public function onClose(ConnectionInterface $connection) public function onClose(ConnectionInterface $connection)
@ -48,7 +48,7 @@ class WebSocketHandler implements MessageComponentInterface
DashboardLogger::disconnection($connection); DashboardLogger::disconnection($connection);
StatisticsLogger::disconnection($connection); StatisticsLogger::logDisconnection($connection);
} }
public function onError(ConnectionInterface $connection, Exception $exception) public function onError(ConnectionInterface $connection, Exception $exception)
@ -94,7 +94,7 @@ class WebSocketHandler implements MessageComponentInterface
DashboardLogger::connection($connection); DashboardLogger::connection($connection);
StatisticsLogger::connection($connection); StatisticsLogger::logConnection($connection);
return $this; return $this;
} }

View File

@ -27,7 +27,7 @@ class WebSocketsStatisticsControllerTest extends TestCase
{ {
return [ return [
'app_id' => config('websockets.apps.0.id'), 'app_id' => config('websockets.apps.0.id'),
'peak_connections' => 1, 'peak_connection_count' => 1,
'websocket_message_count' => 2, 'websocket_message_count' => 2,
'api_message_count' => 3, 'api_message_count' => 3,
]; ];

View File

@ -5,6 +5,7 @@ namespace BeyondCode\LaravelWebSockets\Tests;
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message; use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler; use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Request;
use BeyondCode\LaravelWebSockets\Tests\Mocks\Connection; use BeyondCode\LaravelWebSockets\Tests\Mocks\Connection;
use BeyondCode\LaravelWebSockets\WebSocketsServiceProvider; use BeyondCode\LaravelWebSockets\WebSocketsServiceProvider;