From 3123f25cbc4c03126e3b71add3e642740ea1c883 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Wed, 19 Aug 2020 09:00:53 +0300 Subject: [PATCH] Renamed HttpStatisticsLogger with MemoryStatisticsLogger (because it stores it in-memory) --- config/websockets.php | 2 +- docs/debugging/dashboard.md | 2 +- src/Console/StartWebSocketServer.php | 7 +++++-- src/Facades/StatisticsLogger.php | 4 ++-- ...tisticsLogger.php => MemoryStatisticsLogger.php} | 2 +- tests/Statistics/Logger/FakeStatisticsLogger.php | 13 +++++++++++-- 6 files changed, 21 insertions(+), 9 deletions(-) rename src/Statistics/Logger/{HttpStatisticsLogger.php => MemoryStatisticsLogger.php} (98%) diff --git a/config/websockets.php b/config/websockets.php index fd1d32b..bddb3d5 100644 --- a/config/websockets.php +++ b/config/websockets.php @@ -212,7 +212,7 @@ return [ | */ - 'logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger::class, + 'logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger::class, // 'logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger\NullStatisticsLogger::class, /* diff --git a/docs/debugging/dashboard.md b/docs/debugging/dashboard.md index b37194d..57f50e6 100644 --- a/docs/debugging/dashboard.md +++ b/docs/debugging/dashboard.md @@ -88,7 +88,7 @@ However, to disable it entirely and void any incoming statistic, you can uncomme | */ -// 'logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger::class, +// 'logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger::class, 'logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger\NullStatisticsLogger::class, // use the `NullStatisticsLogger` instead ``` diff --git a/src/Console/StartWebSocketServer.php b/src/Console/StartWebSocketServer.php index 51e3d02..70932ba 100644 --- a/src/Console/StartWebSocketServer.php +++ b/src/Console/StartWebSocketServer.php @@ -33,6 +33,7 @@ class StartWebSocketServer extends Command protected $signature = 'websockets:serve {--host=0.0.0.0} {--port=6001} + {--statistics-interval= : Overwrite the statistics interval set in the config.} {--debug : Forces the loggers to be enabled and thereby overriding the APP_DEBUG setting.} {--test : Prepare the server, but do not start it.} '; @@ -110,7 +111,7 @@ class StartWebSocketServer extends Command $browser = new Browser($this->loop, $connector); $this->laravel->singleton(StatisticsLoggerInterface::class, function () use ($browser) { - $class = config('websockets.statistics.logger', \BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger::class); + $class = config('websockets.statistics.logger', \BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger::class); return new $class( $this->laravel->make(ChannelManager::class), @@ -118,7 +119,9 @@ class StartWebSocketServer extends Command ); }); - $this->loop->addPeriodicTimer(config('websockets.statistics.interval_in_seconds'), function () { + $this->loop->addPeriodicTimer($this->option('statistics-interval') ?: config('websockets.statistics.interval_in_seconds'), function () { + $this->line('Saving statistics...'); + StatisticsLogger::save(); }); diff --git a/src/Facades/StatisticsLogger.php b/src/Facades/StatisticsLogger.php index 59e58d9..e7989b2 100644 --- a/src/Facades/StatisticsLogger.php +++ b/src/Facades/StatisticsLogger.php @@ -6,8 +6,8 @@ use BeyondCode\LaravelWebSockets\Statistics\Logger\StatisticsLogger as Statistic use Illuminate\Support\Facades\Facade; /** - * @see \BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger - * @mixin \BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger + * @see \BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger + * @mixin \BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger */ class StatisticsLogger extends Facade { diff --git a/src/Statistics/Logger/HttpStatisticsLogger.php b/src/Statistics/Logger/MemoryStatisticsLogger.php similarity index 98% rename from src/Statistics/Logger/HttpStatisticsLogger.php rename to src/Statistics/Logger/MemoryStatisticsLogger.php index a97c480..224ddfe 100644 --- a/src/Statistics/Logger/HttpStatisticsLogger.php +++ b/src/Statistics/Logger/MemoryStatisticsLogger.php @@ -10,7 +10,7 @@ use Clue\React\Buzz\Browser; use function GuzzleHttp\Psr7\stream_for; use Ratchet\ConnectionInterface; -class HttpStatisticsLogger implements StatisticsLogger +class MemoryStatisticsLogger implements StatisticsLogger { /** * The list of stored statistics. diff --git a/tests/Statistics/Logger/FakeStatisticsLogger.php b/tests/Statistics/Logger/FakeStatisticsLogger.php index 1f05bff..629e627 100644 --- a/tests/Statistics/Logger/FakeStatisticsLogger.php +++ b/tests/Statistics/Logger/FakeStatisticsLogger.php @@ -2,10 +2,13 @@ namespace BeyondCode\LaravelWebSockets\Tests\Statistics\Logger; -use BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger; +use BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger; -class FakeStatisticsLogger extends HttpStatisticsLogger +class FakeStatisticsLogger extends MemoryStatisticsLogger { + /** + * {@inheritdoc} + */ public function save() { foreach ($this->statistics as $appId => $statistic) { @@ -14,6 +17,12 @@ class FakeStatisticsLogger extends HttpStatisticsLogger } } + /** + * Get app by id. + * + * @param mixed $appId + * @return array + */ public function getForAppId($appId): array { $statistic = $this->findOrMakeStatisticForAppId($appId);