From 99b55411c1666a9518fbcaf1e0f80c87be33fb86 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Wed, 19 Aug 2020 22:39:38 +0300 Subject: [PATCH] Removed controller that sends out the statistics --- config/websockets.php | 31 -------------- src/Console/StartWebSocketServer.php | 33 ++------------- .../WebSocketStatisticsEntriesController.php | 34 --------------- src/Statistics/Http/Middleware/Authorize.php | 22 ---------- .../Logger/MemoryStatisticsLogger.php | 27 +++++------- .../Logger/NullStatisticsLogger.php | 13 +++--- src/WebSocketsServiceProvider.php | 5 --- .../WebSocketsStatisticsControllerTest.php | 42 ------------------- tests/TestCase.php | 5 +-- 9 files changed, 23 insertions(+), 189 deletions(-) delete mode 100644 src/Statistics/Http/Controllers/WebSocketStatisticsEntriesController.php delete mode 100644 src/Statistics/Http/Middleware/Authorize.php delete mode 100644 tests/Statistics/Controllers/WebSocketsStatisticsControllerTest.php diff --git a/config/websockets.php b/config/websockets.php index bddb3d5..a2e29b2 100644 --- a/config/websockets.php +++ b/config/websockets.php @@ -239,37 +239,6 @@ return [ 'delete_statistics_older_than_days' => 60, - /* - |-------------------------------------------------------------------------- - | DNS Lookup - |-------------------------------------------------------------------------- - | - | Use an DNS resolver to make the requests to the statistics logger - | default is to resolve everything to 127.0.0.1. - | - */ - - 'perform_dns_lookup' => false, - - /* - |-------------------------------------------------------------------------- - | DNS Lookup TLS Settings - |-------------------------------------------------------------------------- - | - | You can configure the DNS Lookup Connector the TLS settings. - | Check the available options here: - | https://github.com/reactphp/socket/blob/master/src/Connector.php#L29 - | - */ - - 'tls' => [ - - 'verify_peer' => env('APP_ENV') === 'production', - - 'verify_peer_name' => env('APP_ENV') === 'production', - - ], - ], ]; diff --git a/src/Console/StartWebSocketServer.php b/src/Console/StartWebSocketServer.php index 70932ba..f5185b7 100644 --- a/src/Console/StartWebSocketServer.php +++ b/src/Console/StartWebSocketServer.php @@ -12,6 +12,7 @@ use BeyondCode\LaravelWebSockets\Server\Logger\HttpLogger; use BeyondCode\LaravelWebSockets\Server\Logger\WebsocketsLogger; use BeyondCode\LaravelWebSockets\Server\WebSocketServerFactory; use BeyondCode\LaravelWebSockets\Statistics\DnsResolver; +use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver; use BeyondCode\LaravelWebSockets\Statistics\Logger\StatisticsLogger as StatisticsLoggerInterface; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; use Clue\React\Buzz\Browser; @@ -103,19 +104,12 @@ class StartWebSocketServer extends Command */ protected function configureStatisticsLogger() { - $connector = new Connector($this->loop, [ - 'dns' => $this->getDnsResolver(), - 'tls' => config('websockets.statistics.tls'), - ]); - - $browser = new Browser($this->loop, $connector); - - $this->laravel->singleton(StatisticsLoggerInterface::class, function () use ($browser) { + $this->laravel->singleton(StatisticsLoggerInterface::class, function () { $class = config('websockets.statistics.logger', \BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger::class); return new $class( $this->laravel->make(ChannelManager::class), - $browser + $this->laravel->make(StatisticsDriver::class) ); }); @@ -273,27 +267,6 @@ class StartWebSocketServer extends Command ->createServer(); } - /** - * Create a DNS resolver for the stats manager. - * - * @return \React\Dns\Resolver\ResolverInterface - */ - protected function getDnsResolver(): ResolverInterface - { - if (! config('websockets.statistics.perform_dns_lookup')) { - return new DnsResolver; - } - - $dnsConfig = DnsConfig::loadSystemConfigBlocking(); - - return (new DnsFactory)->createCached( - $dnsConfig->nameservers - ? reset($dnsConfig->nameservers) - : '1.1.1.1', - $this->loop - ); - } - /** * Get the last time the server restarted. * diff --git a/src/Statistics/Http/Controllers/WebSocketStatisticsEntriesController.php b/src/Statistics/Http/Controllers/WebSocketStatisticsEntriesController.php deleted file mode 100644 index bf9453b..0000000 --- a/src/Statistics/Http/Controllers/WebSocketStatisticsEntriesController.php +++ /dev/null @@ -1,34 +0,0 @@ -validate([ - 'app_id' => ['required', new AppId()], - 'peak_connection_count' => 'required|integer', - 'websocket_message_count' => 'required|integer', - 'api_message_count' => 'required|integer', - ]); - - broadcast(new StatisticsUpdated( - $driver::create($validatedAttributes) - )); - - return 'ok'; - } -} diff --git a/src/Statistics/Http/Middleware/Authorize.php b/src/Statistics/Http/Middleware/Authorize.php deleted file mode 100644 index cadd0d6..0000000 --- a/src/Statistics/Http/Middleware/Authorize.php +++ /dev/null @@ -1,22 +0,0 @@ -secret)) - ? abort(403) - : $next($request); - } -} diff --git a/src/Statistics/Logger/MemoryStatisticsLogger.php b/src/Statistics/Logger/MemoryStatisticsLogger.php index 0c4c0a1..942af42 100644 --- a/src/Statistics/Logger/MemoryStatisticsLogger.php +++ b/src/Statistics/Logger/MemoryStatisticsLogger.php @@ -3,6 +3,8 @@ namespace BeyondCode\LaravelWebSockets\Statistics\Logger; use BeyondCode\LaravelWebSockets\Apps\App; +use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver; +use BeyondCode\LaravelWebSockets\Statistics\Events\StatisticsUpdated; use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController; use BeyondCode\LaravelWebSockets\Statistics\Statistic; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; @@ -27,23 +29,23 @@ class MemoryStatisticsLogger implements StatisticsLogger protected $channelManager; /** - * The Browser instance. + * The statistics driver instance. * - * @var \Clue\React\Buzz\Browser + * @var \BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver */ - protected $browser; + protected $driver; /** * Initialize the logger. * * @param \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager $channelManager - * @param \Clue\React\Buzz\Browser $browser + * @param \BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver $driver * @return void */ - public function __construct(ChannelManager $channelManager, Browser $browser) + public function __construct(ChannelManager $channelManager, StatisticsDriver $driver) { $this->channelManager = $channelManager; - $this->browser = $browser; + $this->driver = $driver; } /** @@ -106,16 +108,9 @@ class MemoryStatisticsLogger implements StatisticsLogger continue; } - $postData = array_merge($statistic->toArray(), [ - 'secret' => App::findById($appId)->secret, - ]); - - $this->browser - ->post( - action([WebSocketStatisticsEntriesController::class, 'store']), - ['Content-Type' => 'application/json'], - stream_for(json_encode($postData)) - ); + broadcast(new StatisticsUpdated( + $this->driver::create($statistic->toArray()) + )); $currentConnectionCount = $this->channelManager->getConnectionCount($appId); diff --git a/src/Statistics/Logger/NullStatisticsLogger.php b/src/Statistics/Logger/NullStatisticsLogger.php index ee8728e..1a1af5b 100644 --- a/src/Statistics/Logger/NullStatisticsLogger.php +++ b/src/Statistics/Logger/NullStatisticsLogger.php @@ -5,6 +5,7 @@ namespace BeyondCode\LaravelWebSockets\Statistics\Logger; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; use Clue\React\Buzz\Browser; use Ratchet\ConnectionInterface; +use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver; class NullStatisticsLogger implements StatisticsLogger { @@ -16,23 +17,23 @@ class NullStatisticsLogger implements StatisticsLogger protected $channelManager; /** - * The Browser instance. + * The statistics driver instance. * - * @var \Clue\React\Buzz\Browser + * @var \BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver */ - protected $browser; + protected $driver; /** * Initialize the logger. * * @param \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager $channelManager - * @param \Clue\React\Buzz\Browser $browser + * @param \BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver $driver * @return void */ - public function __construct(ChannelManager $channelManager, Browser $browser) + public function __construct(ChannelManager $channelManager, StatisticsDriver $driver) { $this->channelManager = $channelManager; - $this->browser = $browser; + $this->driver = $driver; } /** diff --git a/src/WebSocketsServiceProvider.php b/src/WebSocketsServiceProvider.php index 297820f..e70b191 100644 --- a/src/WebSocketsServiceProvider.php +++ b/src/WebSocketsServiceProvider.php @@ -12,7 +12,6 @@ use BeyondCode\LaravelWebSockets\PubSub\Broadcasters\RedisPusherBroadcaster; use BeyondCode\LaravelWebSockets\Server\Router; use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver; use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController; -use BeyondCode\LaravelWebSockets\Statistics\Http\Middleware\Authorize as AuthorizeStatistics; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager; use Illuminate\Broadcasting\BroadcastManager; @@ -127,10 +126,6 @@ class WebSocketsServiceProvider extends ServiceProvider Route::post('auth', AuthenticateDashboard::class); Route::post('event', SendMessage::class); }); - - Route::middleware(AuthorizeStatistics::class)->group(function () { - Route::post('statistics', [WebSocketStatisticsEntriesController::class, 'store']); - }); }); return $this; diff --git a/tests/Statistics/Controllers/WebSocketsStatisticsControllerTest.php b/tests/Statistics/Controllers/WebSocketsStatisticsControllerTest.php deleted file mode 100644 index 360518f..0000000 --- a/tests/Statistics/Controllers/WebSocketsStatisticsControllerTest.php +++ /dev/null @@ -1,42 +0,0 @@ -post( - action([WebSocketStatisticsEntriesController::class, 'store']), - array_merge($this->payload(), [ - 'secret' => config('websockets.apps.0.secret'), - ]) - ); - - $entries = WebSocketsStatisticsEntry::get(); - - $this->assertCount(1, $entries); - - $actual = $entries->first()->attributesToArray(); - - foreach ($this->payload() as $key => $value) { - $this->assertArrayHasKey($key, $actual); - $this->assertSame($value, $actual[$key]); - } - } - - protected function payload(): array - { - return [ - 'app_id' => config('websockets.apps.0.id'), - 'peak_connection_count' => '1', - 'websocket_message_count' => '2', - 'api_message_count' => '3', - ]; - } -} diff --git a/tests/TestCase.php b/tests/TestCase.php index 664bf28..02c9c18 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -6,6 +6,7 @@ use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger; use BeyondCode\LaravelWebSockets\PubSub\Drivers\LocalClient; use BeyondCode\LaravelWebSockets\PubSub\Drivers\RedisClient; use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface; +use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver; use BeyondCode\LaravelWebSockets\Tests\Mocks\Connection; use BeyondCode\LaravelWebSockets\Tests\Mocks\Message; use BeyondCode\LaravelWebSockets\Tests\Statistics\Logger\FakeStatisticsLogger; @@ -45,7 +46,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase StatisticsLogger::swap(new FakeStatisticsLogger( $this->channelManager, - Mockery::mock(Browser::class) + app(StatisticsDriver::class) )); $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); @@ -94,8 +95,6 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase ], ]); - $app['config']->set('websockets.statistics.perform_dns_lookup', true); - $app['config']->set('database.redis.default', [ 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PASSWORD', null),