Removed controller that sends out the statistics

This commit is contained in:
Alex Renoki 2020-08-19 22:39:38 +03:00
parent ed96e24f6a
commit 99b55411c1
9 changed files with 23 additions and 189 deletions

View File

@ -239,37 +239,6 @@ return [
'delete_statistics_older_than_days' => 60, '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',
],
], ],
]; ];

View File

@ -12,6 +12,7 @@ use BeyondCode\LaravelWebSockets\Server\Logger\HttpLogger;
use BeyondCode\LaravelWebSockets\Server\Logger\WebsocketsLogger; use BeyondCode\LaravelWebSockets\Server\Logger\WebsocketsLogger;
use BeyondCode\LaravelWebSockets\Server\WebSocketServerFactory; use BeyondCode\LaravelWebSockets\Server\WebSocketServerFactory;
use BeyondCode\LaravelWebSockets\Statistics\DnsResolver; use BeyondCode\LaravelWebSockets\Statistics\DnsResolver;
use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver;
use BeyondCode\LaravelWebSockets\Statistics\Logger\StatisticsLogger as StatisticsLoggerInterface; use BeyondCode\LaravelWebSockets\Statistics\Logger\StatisticsLogger as StatisticsLoggerInterface;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use Clue\React\Buzz\Browser; use Clue\React\Buzz\Browser;
@ -103,19 +104,12 @@ class StartWebSocketServer extends Command
*/ */
protected function configureStatisticsLogger() protected function configureStatisticsLogger()
{ {
$connector = new Connector($this->loop, [ $this->laravel->singleton(StatisticsLoggerInterface::class, function () {
'dns' => $this->getDnsResolver(),
'tls' => config('websockets.statistics.tls'),
]);
$browser = new Browser($this->loop, $connector);
$this->laravel->singleton(StatisticsLoggerInterface::class, function () use ($browser) {
$class = config('websockets.statistics.logger', \BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger::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),
$browser $this->laravel->make(StatisticsDriver::class)
); );
}); });
@ -273,27 +267,6 @@ class StartWebSocketServer extends Command
->createServer(); ->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. * Get the last time the server restarted.
* *

View File

@ -1,34 +0,0 @@
<?php
namespace BeyondCode\LaravelWebSockets\Statistics\Http\Controllers;
use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver;
use BeyondCode\LaravelWebSockets\Statistics\Events\StatisticsUpdated;
use BeyondCode\LaravelWebSockets\Statistics\Rules\AppId;
use Illuminate\Http\Request;
class WebSocketStatisticsEntriesController
{
/**
* Store the entry.
*
* @param \Illuminate\Http\Request $request
* @param \BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver $driver
* @return \Illuminate\Http\Response
*/
public function store(Request $request, StatisticsDriver $driver)
{
$validatedAttributes = $request->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';
}
}

View File

@ -1,22 +0,0 @@
<?php
namespace BeyondCode\LaravelWebSockets\Statistics\Http\Middleware;
use BeyondCode\LaravelWebSockets\Apps\App;
class Authorize
{
/**
* Authorize the request by app secret.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return \Illuminate\Http\Response
*/
public function handle($request, $next)
{
return is_null(App::findBySecret($request->secret))
? abort(403)
: $next($request);
}
}

View File

@ -3,6 +3,8 @@
namespace BeyondCode\LaravelWebSockets\Statistics\Logger; namespace BeyondCode\LaravelWebSockets\Statistics\Logger;
use BeyondCode\LaravelWebSockets\Apps\App; 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\Http\Controllers\WebSocketStatisticsEntriesController;
use BeyondCode\LaravelWebSockets\Statistics\Statistic; use BeyondCode\LaravelWebSockets\Statistics\Statistic;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
@ -27,23 +29,23 @@ class MemoryStatisticsLogger implements StatisticsLogger
protected $channelManager; 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. * Initialize the logger.
* *
* @param \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager $channelManager * @param \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager $channelManager
* @param \Clue\React\Buzz\Browser $browser * @param \BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver $driver
* @return void * @return void
*/ */
public function __construct(ChannelManager $channelManager, Browser $browser) public function __construct(ChannelManager $channelManager, StatisticsDriver $driver)
{ {
$this->channelManager = $channelManager; $this->channelManager = $channelManager;
$this->browser = $browser; $this->driver = $driver;
} }
/** /**
@ -106,16 +108,9 @@ class MemoryStatisticsLogger implements StatisticsLogger
continue; continue;
} }
$postData = array_merge($statistic->toArray(), [ broadcast(new StatisticsUpdated(
'secret' => App::findById($appId)->secret, $this->driver::create($statistic->toArray())
]); ));
$this->browser
->post(
action([WebSocketStatisticsEntriesController::class, 'store']),
['Content-Type' => 'application/json'],
stream_for(json_encode($postData))
);
$currentConnectionCount = $this->channelManager->getConnectionCount($appId); $currentConnectionCount = $this->channelManager->getConnectionCount($appId);

View File

@ -5,6 +5,7 @@ namespace BeyondCode\LaravelWebSockets\Statistics\Logger;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use Clue\React\Buzz\Browser; use Clue\React\Buzz\Browser;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;
use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver;
class NullStatisticsLogger implements StatisticsLogger class NullStatisticsLogger implements StatisticsLogger
{ {
@ -16,23 +17,23 @@ class NullStatisticsLogger implements StatisticsLogger
protected $channelManager; 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. * Initialize the logger.
* *
* @param \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager $channelManager * @param \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager $channelManager
* @param \Clue\React\Buzz\Browser $browser * @param \BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver $driver
* @return void * @return void
*/ */
public function __construct(ChannelManager $channelManager, Browser $browser) public function __construct(ChannelManager $channelManager, StatisticsDriver $driver)
{ {
$this->channelManager = $channelManager; $this->channelManager = $channelManager;
$this->browser = $browser; $this->driver = $driver;
} }
/** /**

View File

@ -12,7 +12,6 @@ use BeyondCode\LaravelWebSockets\PubSub\Broadcasters\RedisPusherBroadcaster;
use BeyondCode\LaravelWebSockets\Server\Router; use BeyondCode\LaravelWebSockets\Server\Router;
use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver; use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver;
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController; 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\ChannelManager;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager;
use Illuminate\Broadcasting\BroadcastManager; use Illuminate\Broadcasting\BroadcastManager;
@ -127,10 +126,6 @@ class WebSocketsServiceProvider extends ServiceProvider
Route::post('auth', AuthenticateDashboard::class); Route::post('auth', AuthenticateDashboard::class);
Route::post('event', SendMessage::class); Route::post('event', SendMessage::class);
}); });
Route::middleware(AuthorizeStatistics::class)->group(function () {
Route::post('statistics', [WebSocketStatisticsEntriesController::class, 'store']);
});
}); });
return $this; return $this;

View File

@ -1,42 +0,0 @@
<?php
namespace BeyondCode\LaravelWebSockets\Tests\Statistics\Controllers;
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController;
use BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry;
use BeyondCode\LaravelWebSockets\Tests\TestCase;
class WebSocketsStatisticsControllerTest extends TestCase
{
/** @test */
public function it_can_store_statistics()
{
$this->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',
];
}
}

View File

@ -6,6 +6,7 @@ use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger;
use BeyondCode\LaravelWebSockets\PubSub\Drivers\LocalClient; use BeyondCode\LaravelWebSockets\PubSub\Drivers\LocalClient;
use BeyondCode\LaravelWebSockets\PubSub\Drivers\RedisClient; use BeyondCode\LaravelWebSockets\PubSub\Drivers\RedisClient;
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface; use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver;
use BeyondCode\LaravelWebSockets\Tests\Mocks\Connection; use BeyondCode\LaravelWebSockets\Tests\Mocks\Connection;
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message; use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
use BeyondCode\LaravelWebSockets\Tests\Statistics\Logger\FakeStatisticsLogger; use BeyondCode\LaravelWebSockets\Tests\Statistics\Logger\FakeStatisticsLogger;
@ -45,7 +46,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
StatisticsLogger::swap(new FakeStatisticsLogger( StatisticsLogger::swap(new FakeStatisticsLogger(
$this->channelManager, $this->channelManager,
Mockery::mock(Browser::class) app(StatisticsDriver::class)
)); ));
$this->loadMigrationsFrom(__DIR__.'/../database/migrations'); $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', [ $app['config']->set('database.redis.default', [
'host' => env('REDIS_HOST', '127.0.0.1'), 'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null), 'password' => env('REDIS_PASSWORD', null),