From 70ffe41c868f2e270e171f9dff80a2ccccd10290 Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Thu, 21 Feb 2019 12:11:37 -0500 Subject: [PATCH 1/5] Add support for overriding stats logger's base URL --- config/websockets.php | 8 ++++++++ src/Statistics/Logger/HttpStatisticsLogger.php | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/config/websockets.php b/config/websockets.php index 74075b5c52..8543b99ec1 100644 --- a/config/websockets.php +++ b/config/websockets.php @@ -80,6 +80,14 @@ */ 'delete_statistics_older_than_days' => 60, + /* + * By default, the websockets server attempts to connect to whatever + * your APP_URL is set to. If running in a more complex environment, + * you may wish to override the base URL for internal requests to + * allow statistics to be collected. + */ + 'base_url_override' => null, + /* * Use an DNS resolver to make the requests to the statistics logger * default is to resolve everything to 127.0.0.1. diff --git a/src/Statistics/Logger/HttpStatisticsLogger.php b/src/Statistics/Logger/HttpStatisticsLogger.php index a23f7a9003..ef61b94269 100644 --- a/src/Statistics/Logger/HttpStatisticsLogger.php +++ b/src/Statistics/Logger/HttpStatisticsLogger.php @@ -65,6 +65,19 @@ protected function findOrMakeStatisticForAppId($appId): Statistic return $this->statistics[$appId]; } + protected function getUrl(): string + { + $action = [WebSocketStatisticsEntriesController::class, 'store']; + + $url_override = config('websockets.statistics.base_url_override', null); + + if ($url_override !== null) { + return $url_override.action($action, [], false); + } + + return action($action); + } + public function save() { foreach ($this->statistics as $appId => $statistic) { @@ -79,7 +92,7 @@ public function save() $this ->browser ->post( - action([WebSocketStatisticsEntriesController::class, 'store']), + $this->getUrl(), ['Content-Type' => 'application/json'], stream_for(json_encode($postData)) ); From a752fb9ecc8fabc173bc89ee7f7d8779c4226fab Mon Sep 17 00:00:00 2001 From: rennokki Date: Thu, 13 Aug 2020 08:30:24 +0300 Subject: [PATCH 2/5] formatting --- src/Statistics/Logger/HttpStatisticsLogger.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Statistics/Logger/HttpStatisticsLogger.php b/src/Statistics/Logger/HttpStatisticsLogger.php index ef61b94269..ea14bc26ee 100644 --- a/src/Statistics/Logger/HttpStatisticsLogger.php +++ b/src/Statistics/Logger/HttpStatisticsLogger.php @@ -69,10 +69,10 @@ protected function getUrl(): string { $action = [WebSocketStatisticsEntriesController::class, 'store']; - $url_override = config('websockets.statistics.base_url_override', null); + $overridenUrl = config('websockets.statistics.base_url_override'); - if ($url_override !== null) { - return $url_override.action($action, [], false); + if ($overridenUrl) { + return $overridenUrl.action($action, [], false); } return action($action); From 99c6c068970bf2ae8dad8ac45dbf2097f68f3203 Mon Sep 17 00:00:00 2001 From: rennokki Date: Thu, 13 Aug 2020 08:32:20 +0300 Subject: [PATCH 3/5] formatting --- .../Logger/HttpStatisticsLogger.php | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Statistics/Logger/HttpStatisticsLogger.php b/src/Statistics/Logger/HttpStatisticsLogger.php index ea14bc26ee..0197c38ba3 100644 --- a/src/Statistics/Logger/HttpStatisticsLogger.php +++ b/src/Statistics/Logger/HttpStatisticsLogger.php @@ -65,19 +65,6 @@ protected function findOrMakeStatisticForAppId($appId): Statistic return $this->statistics[$appId]; } - protected function getUrl(): string - { - $action = [WebSocketStatisticsEntriesController::class, 'store']; - - $overridenUrl = config('websockets.statistics.base_url_override'); - - if ($overridenUrl) { - return $overridenUrl.action($action, [], false); - } - - return action($action); - } - public function save() { foreach ($this->statistics as $appId => $statistic) { @@ -92,7 +79,7 @@ public function save() $this ->browser ->post( - $this->getUrl(), + $this->storeStatisticsUrl(), ['Content-Type' => 'application/json'], stream_for(json_encode($postData)) ); @@ -101,4 +88,17 @@ public function save() $statistic->reset($currentConnectionCount); } } + + protected function storeStatisticsUrl(): string + { + $action = [WebSocketStatisticsEntriesController::class, 'store']; + + $overridenUrl = config('websockets.statistics.base_url_override'); + + if ($overridenUrl) { + return $overridenUrl.action($action, [], false); + } + + return action($action); + } } From 371bf5418c1021ede09cb48d5a5a3ee5341ca3e8 Mon Sep 17 00:00:00 2001 From: rennokki Date: Thu, 13 Aug 2020 09:47:55 +0300 Subject: [PATCH 4/5] inline if --- src/Statistics/Logger/HttpStatisticsLogger.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Statistics/Logger/HttpStatisticsLogger.php b/src/Statistics/Logger/HttpStatisticsLogger.php index 0197c38ba3..926099e0ec 100644 --- a/src/Statistics/Logger/HttpStatisticsLogger.php +++ b/src/Statistics/Logger/HttpStatisticsLogger.php @@ -95,10 +95,8 @@ protected function storeStatisticsUrl(): string $overridenUrl = config('websockets.statistics.base_url_override'); - if ($overridenUrl) { - return $overridenUrl.action($action, [], false); - } - - return action($action); + return $overridentUrl + ? $overridenUrl.action($action, [], false) + : action($action); } } From 10208671bb8bbf458e62225ad542bfdf42192cc1 Mon Sep 17 00:00:00 2001 From: rennokki Date: Thu, 13 Aug 2020 09:49:09 +0300 Subject: [PATCH 5/5] csfixing --- src/Statistics/Logger/HttpStatisticsLogger.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Statistics/Logger/HttpStatisticsLogger.php b/src/Statistics/Logger/HttpStatisticsLogger.php index 926099e0ec..d7320253ef 100644 --- a/src/Statistics/Logger/HttpStatisticsLogger.php +++ b/src/Statistics/Logger/HttpStatisticsLogger.php @@ -2,13 +2,13 @@ namespace BeyondCode\LaravelWebSockets\Statistics\Logger; -use Clue\React\Buzz\Browser; -use Ratchet\ConnectionInterface; -use function GuzzleHttp\Psr7\stream_for; use BeyondCode\LaravelWebSockets\Apps\App; +use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController; use BeyondCode\LaravelWebSockets\Statistics\Statistic; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; -use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController; +use Clue\React\Buzz\Browser; +use function GuzzleHttp\Psr7\stream_for; +use Ratchet\ConnectionInterface; class HttpStatisticsLogger implements StatisticsLogger {