Renamed HttpStatisticsLogger with MemoryStatisticsLogger (because it stores it in-memory)

This commit is contained in:
Alex Renoki 2020-08-19 09:00:53 +03:00
parent 850ebe57dc
commit 3123f25cbc
6 changed files with 21 additions and 9 deletions

View File

@ -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, // 'logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger\NullStatisticsLogger::class,
/* /*

View File

@ -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 'logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger\NullStatisticsLogger::class, // use the `NullStatisticsLogger` instead
``` ```

View File

@ -33,6 +33,7 @@ class StartWebSocketServer extends Command
protected $signature = 'websockets:serve protected $signature = 'websockets:serve
{--host=0.0.0.0} {--host=0.0.0.0}
{--port=6001} {--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.} {--debug : Forces the loggers to be enabled and thereby overriding the APP_DEBUG setting.}
{--test : Prepare the server, but do not start it.} {--test : Prepare the server, but do not start it.}
'; ';
@ -110,7 +111,7 @@ class StartWebSocketServer extends Command
$browser = new Browser($this->loop, $connector); $browser = new Browser($this->loop, $connector);
$this->laravel->singleton(StatisticsLoggerInterface::class, function () use ($browser) { $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( return new $class(
$this->laravel->make(ChannelManager::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(); StatisticsLogger::save();
}); });

View File

@ -6,8 +6,8 @@ use BeyondCode\LaravelWebSockets\Statistics\Logger\StatisticsLogger as Statistic
use Illuminate\Support\Facades\Facade; use Illuminate\Support\Facades\Facade;
/** /**
* @see \BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger * @see \BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger
* @mixin \BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger * @mixin \BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger
*/ */
class StatisticsLogger extends Facade class StatisticsLogger extends Facade
{ {

View File

@ -10,7 +10,7 @@ use Clue\React\Buzz\Browser;
use function GuzzleHttp\Psr7\stream_for; use function GuzzleHttp\Psr7\stream_for;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;
class HttpStatisticsLogger implements StatisticsLogger class MemoryStatisticsLogger implements StatisticsLogger
{ {
/** /**
* The list of stored statistics. * The list of stored statistics.

View File

@ -2,10 +2,13 @@
namespace BeyondCode\LaravelWebSockets\Tests\Statistics\Logger; 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() public function save()
{ {
foreach ($this->statistics as $appId => $statistic) { 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 public function getForAppId($appId): array
{ {
$statistic = $this->findOrMakeStatisticForAppId($appId); $statistic = $this->findOrMakeStatisticForAppId($appId);