Permalink
Browse files

wip

  • Loading branch information...
freekmurze committed Dec 3, 2018
1 parent 95e519e commit 3d94b971588975bf73e7c7fd1bcc62b58cb5d5bb
@@ -14,7 +14,7 @@ class CreateWebSocketsStatisticsEntriesTable extends Migration
Schema::create('websockets_statistics_entries', function (Blueprint $table) {
$table->increments('id');
$table->string('app_id');
$table->integer('peak_connections');
$table->integer('peak_connection_count');
$table->integer('websocket_message_count');
$table->integer('api_message_count');
$table->nullableTimestamps();
@@ -7,10 +7,13 @@
use BeyondCode\LaravelWebSockets\Server\Logger\ConnectionLogger;
use BeyondCode\LaravelWebSockets\Server\Logger\HttpLogger;
use BeyondCode\LaravelWebSockets\Server\Logger\WebsocketsLogger;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Illuminate\Console\Command;
use BeyondCode\LaravelWebSockets\Server\WebSocketServerFactory;
use React\EventLoop\Factory as LoopFactory;
use WyriHaximus\React\GuzzlePsr7\HttpClientAdapter;
class StartWebSocketServer extends Command
{
@@ -74,10 +77,10 @@ protected function configureConnectionLogger()
protected function configureStatisticsLogger()
{
$handler = new \WyriHaximus\React\GuzzlePsr7\HttpClientAdapter($this->loop);
$handler = new HttpClientAdapter($this->loop);
$client = new \GuzzleHttp\Client([
'handler' => \GuzzleHttp\HandlerStack::create($handler),
$client = new Client([
'handler' => HandlerStack::create($handler),
]);
app()->singleton('websockets.statisticslogger', function() use ($client) {
@@ -28,7 +28,7 @@ public function __invoke(Request $request)
$request->json()->get('data')
);
StatisticsLogger::apiMessage($request->appId);
StatisticsLogger::logApiMessage($request->appId);
}
return $request->json()->all();
@@ -12,7 +12,7 @@ public function store(Request $request)
{
$validatedAttributes = $request->validate([
'app_id' => ['required', new AppId()],
'peak_connections' => 'required|integer',
'peak_connection_count' => 'required|integer',
'websocket_message_count' => 'required|integer',
'api_message_count' => 'required|integer',
]);
@@ -10,10 +10,10 @@ class Statistic
protected $appId;
/** @var int */
protected $connections = 0;
protected $currentConnectionCount = 0;
/** @var int */
protected $peakConnections = 0;
protected $peakConnectionCount = 0;
/** @var int */
protected $webSocketMessageCount = 0;
@@ -31,34 +31,34 @@ public function isEnabled(): bool
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++;
}
public function apiMessage()
public function logApiMessage()
{
$this->apiMessageCount++;
}
public function reset(int $currentConnectionCount)
{
$this->connections = $currentConnectionCount;
$this->peakConnections = $currentConnectionCount;
$this->currentConnectionCount = $currentConnectionCount;
$this->peakConnectionCount = $currentConnectionCount;
$this->webSocketMessageCount = 0;
$this->apiMessageCount = 0;
}
@@ -67,7 +67,7 @@ public function toArray()
{
return [
'app_id' => $this->appId,
'peak_connections' => $this->peakConnections,
'peak_connection_count' => $this->peakConnectionCount,
'websocket_message_count' => $this->webSocketMessageCount,
'api_message_count' => $this->apiMessageCount,
];
@@ -5,7 +5,6 @@
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use GuzzleHttp\Client;
use Illuminate\Support\Collection;
use Ratchet\ConnectionInterface;
class StatisticsLogger
@@ -23,32 +22,32 @@ public function __construct(ChannelManager $channelManager, Client $client)
$this->client = $client;
}
public function webSocketMessage(ConnectionInterface $connection)
public function logWebSocketMessage(ConnectionInterface $connection)
{
$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->statistics[$appId]->apiMessage();
$this->statistics[$appId]->logApiMessage();
}
public function connection(ConnectionInterface $connection)
public function logConnection(ConnectionInterface $connection)
{
$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->statistics[$connection->app->id]->disconnection();
$this->statistics[$connection->app->id]->logDisconnection();
}
protected function initializeStatistics($id)
@@ -39,7 +39,7 @@ public function onMessage(ConnectionInterface $connection, MessageInterface $mes
$message->respond();
StatisticsLogger::webSocketMessage($connection);
StatisticsLogger::logWebSocketMessage($connection);
}
public function onClose(ConnectionInterface $connection)
@@ -48,7 +48,7 @@ public function onClose(ConnectionInterface $connection)
DashboardLogger::disconnection($connection);
StatisticsLogger::disconnection($connection);
StatisticsLogger::logDisconnection($connection);
}
public function onError(ConnectionInterface $connection, Exception $exception)
@@ -94,7 +94,7 @@ protected function establishConnection(ConnectionInterface $connection)
DashboardLogger::connection($connection);
StatisticsLogger::connection($connection);
StatisticsLogger::logConnection($connection);
return $this;
}
@@ -27,7 +27,7 @@ protected function payload(): array
{
return [
'app_id' => config('websockets.apps.0.id'),
'peak_connections' => 1,
'peak_connection_count' => 1,
'websocket_message_count' => 2,
'api_message_count' => 3,
];
@@ -5,6 +5,7 @@
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use BeyondCode\LaravelWebSockets\Tests\Mocks\Connection;
use BeyondCode\LaravelWebSockets\WebSocketsServiceProvider;

0 comments on commit 3d94b97

Please sign in to comment.