From d29b1f59b4b195ab502c35dc1d92eeffbc782e1f Mon Sep 17 00:00:00 2001 From: svenvg93 Date: Tue, 20 Aug 2024 21:20:36 +0200 Subject: [PATCH 1/3] first commit --- app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php b/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php index 3e3d591f9..c57daa6a4 100644 --- a/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php +++ b/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php @@ -64,7 +64,6 @@ public function handle(): void $message = collect(array_filter($messages, 'json_validate'))->last(); $this->result->update([ - 'server_id' => $this->serverId, 'data' => json_decode($message, true), 'status' => ResultStatus::Failed, ]); @@ -98,7 +97,6 @@ protected function checkForInternetConnection(): bool if (! URL::isValidUrl($url)) { $this->result->update([ - 'server_id' => $this->serverId, 'data' => [ 'type' => 'log', 'level' => 'error', @@ -122,7 +120,6 @@ protected function checkForInternetConnection(): bool if ($ping->ping() === false) { $this->result->update([ - 'server_id' => $this->serverId, 'data' => [ 'type' => 'log', 'level' => 'error', From 2f6954d69c5ae2d472e5b7a13a47813a57abdfcb Mon Sep 17 00:00:00 2001 From: svenvg93 Date: Tue, 20 Aug 2024 21:53:18 +0200 Subject: [PATCH 2/3] commit --- app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php | 39 +++++++++++++++---- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php b/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php index c57daa6a4..8a433f56d 100644 --- a/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php +++ b/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php @@ -61,13 +61,35 @@ public function handle(): void } catch (ProcessFailedException $exception) { $messages = explode(PHP_EOL, $exception->getMessage()); - $message = collect(array_filter($messages, 'json_validate'))->last(); - - $this->result->update([ - 'data' => json_decode($message, true), - 'status' => ResultStatus::Failed, - ]); - + // Extract only the "message" part from each JSON error message + $errorMessages = array_map(function ($message) { + $decoded = json_decode($message, true); + if (json_last_error() === JSON_ERROR_NONE && isset($decoded['message'])) { + return $decoded['message']; + } + + return ''; // If it's not valid JSON or doesn't contain "message", return an empty string + }, $messages); + + // Filter out empty messages and concatenate + $errorMessage = implode(' | ', array_filter($errorMessages)); + + // Add server ID to the error message if it exists + if ($this->serverId !== null) { + + $this->result->update([ + 'data' => [ + 'type' => 'log', + 'level' => 'error', + 'message' => $errorMessage, + 'server' => [ + 'id' => $this->serverId, + ], + ], + 'status' => ResultStatus::Failed, + ]); + + } SpeedtestFailed::dispatch($this->result); return; @@ -86,6 +108,9 @@ public function handle(): void SpeedtestCompleted::dispatch($this->result); } + /** + * Check for internet connection. + */ protected function checkForInternetConnection(): bool { $url = config('speedtest.ping_url'); From 2bfe90c2e56a1772455a68cbbb3f243a18e0cbfa Mon Sep 17 00:00:00 2001 From: svenvg93 Date: Thu, 26 Sep 2024 07:19:37 +0200 Subject: [PATCH 3/3] Make server id optional --- app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php b/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php index 8a433f56d..3d298a7bc 100644 --- a/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php +++ b/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php @@ -74,22 +74,24 @@ public function handle(): void // Filter out empty messages and concatenate $errorMessage = implode(' | ', array_filter($errorMessages)); - // Add server ID to the error message if it exists + // Prepare the error message data + $data = [ + 'type' => 'log', + 'level' => 'error', + 'message' => $errorMessage, + ]; + + // Add server ID if it exists if ($this->serverId !== null) { + $data['server'] = ['id' => $this->serverId]; + } - $this->result->update([ - 'data' => [ - 'type' => 'log', - 'level' => 'error', - 'message' => $errorMessage, - 'server' => [ - 'id' => $this->serverId, - ], - ], - 'status' => ResultStatus::Failed, - ]); + // Update the result with the error data + $this->result->update([ + 'data' => $data, + 'status' => ResultStatus::Failed, + ]); - } SpeedtestFailed::dispatch($this->result); return;