From abab2f9e376118face9ac87f8b68792191771c61 Mon Sep 17 00:00:00 2001 From: Alex Justesen Date: Sun, 3 Mar 2024 09:27:42 -0500 Subject: [PATCH] [Bug] Don't try to send empty threshold notifications (#1291) --- .../Discord/SendSpeedtestThresholdNotification.php | 14 ++++++++------ .../Mail/SendSpeedtestThresholdNotification.php | 14 ++++++++------ .../SendSpeedtestThresholdNotification.php | 14 ++++++++------ .../Webhook/SendSpeedtestThresholdNotification.php | 14 ++++++++------ .../views/emails/speedtest-completed.blade.php | 2 +- .../views/emails/speedtest-threshold.blade.php | 2 +- 6 files changed, 34 insertions(+), 26 deletions(-) diff --git a/app/Listeners/Discord/SendSpeedtestThresholdNotification.php b/app/Listeners/Discord/SendSpeedtestThresholdNotification.php index b535899a3..6f2a3776a 100644 --- a/app/Listeners/Discord/SendSpeedtestThresholdNotification.php +++ b/app/Listeners/Discord/SendSpeedtestThresholdNotification.php @@ -53,6 +53,8 @@ public function handle(SpeedtestCompleted $event): void array_push($failed, $this->absolutePingThreshold(event: $event, thresholdSettings: $thresholdSettings)); } + $failed = array_filter($failed); + if (! count($failed)) { Log::warning('Failed Discord thresholds not found, won\'t send notification.'); @@ -81,10 +83,10 @@ public function handle(SpeedtestCompleted $event): void /** * Build Discord notification if absolute download threshold is breached. */ - protected function absoluteDownloadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array + protected function absoluteDownloadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): bool|array { if (! absoluteDownloadThresholdFailed($thresholdSettings->absolute_download, $event->result->download)) { - return []; + return false; } return [ @@ -97,10 +99,10 @@ protected function absoluteDownloadThreshold(SpeedtestCompleted $event, Threshol /** * Build Discord notification if absolute upload threshold is breached. */ - protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array + protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): bool|array { if (! absoluteUploadThresholdFailed($thresholdSettings->absolute_upload, $event->result->upload)) { - return []; + return false; } return [ @@ -113,10 +115,10 @@ protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdS /** * Build Discord notification if absolute ping threshold is breached. */ - protected function absolutePingThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array + protected function absolutePingThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): bool|array { if (! absolutePingThresholdFailed($thresholdSettings->absolute_ping, $event->result->ping)) { - return []; + return false; } return [ diff --git a/app/Listeners/Mail/SendSpeedtestThresholdNotification.php b/app/Listeners/Mail/SendSpeedtestThresholdNotification.php index 9345d7501..2cd246655 100644 --- a/app/Listeners/Mail/SendSpeedtestThresholdNotification.php +++ b/app/Listeners/Mail/SendSpeedtestThresholdNotification.php @@ -53,6 +53,8 @@ public function handle(SpeedtestCompleted $event): void array_push($failed, $this->absolutePingThreshold(event: $event, thresholdSettings: $thresholdSettings)); } + $failed = array_filter($failed); + if (! count($failed)) { Log::warning('Failed mail thresholds not found, won\'t send notification.'); @@ -68,10 +70,10 @@ public function handle(SpeedtestCompleted $event): void /** * Build mail notification if absolute download threshold is breached. */ - protected function absoluteDownloadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array + protected function absoluteDownloadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): bool|array { if (! absoluteDownloadThresholdFailed($thresholdSettings->absolute_download, $event->result->download)) { - return []; + return false; } return [ @@ -84,10 +86,10 @@ protected function absoluteDownloadThreshold(SpeedtestCompleted $event, Threshol /** * Build mail notification if absolute upload threshold is breached. */ - protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array + protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): bool|array { if (! absoluteUploadThresholdFailed($thresholdSettings->absolute_upload, $event->result->upload)) { - return []; + return false; } return [ @@ -100,10 +102,10 @@ protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdS /** * Build mail notification if absolute ping threshold is breached. */ - protected function absolutePingThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array + protected function absolutePingThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): bool|array { if (! absolutePingThresholdFailed($thresholdSettings->absolute_ping, $event->result->ping)) { - return []; + return false; } return [ diff --git a/app/Listeners/Telegram/SendSpeedtestThresholdNotification.php b/app/Listeners/Telegram/SendSpeedtestThresholdNotification.php index f3b4276b5..ddd25ae2c 100644 --- a/app/Listeners/Telegram/SendSpeedtestThresholdNotification.php +++ b/app/Listeners/Telegram/SendSpeedtestThresholdNotification.php @@ -54,6 +54,8 @@ public function handle(SpeedtestCompleted $event): void array_push($failed, $this->absolutePingThreshold(event: $event, thresholdSettings: $thresholdSettings)); } + $failed = array_filter($failed); + if (! count($failed)) { Log::warning('Failed Telegram thresholds not found, won\'t send notification.'); @@ -77,10 +79,10 @@ public function handle(SpeedtestCompleted $event): void /** * Build Telegram notification if absolute download threshold is breached. */ - protected function absoluteDownloadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array + protected function absoluteDownloadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): bool|array { if (! absoluteDownloadThresholdFailed($thresholdSettings->absolute_download, $event->result->download)) { - return []; + return false; } return [ @@ -93,10 +95,10 @@ protected function absoluteDownloadThreshold(SpeedtestCompleted $event, Threshol /** * Build Telegram notification if absolute upload threshold is breached. */ - protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array + protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): bool|array { if (! absoluteUploadThresholdFailed($thresholdSettings->absolute_upload, $event->result->upload)) { - return []; + return false; } return [ @@ -109,10 +111,10 @@ protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdS /** * Build Telegram notification if absolute ping threshold is breached. */ - protected function absolutePingThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array + protected function absolutePingThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): bool|array { if (! absolutePingThresholdFailed($thresholdSettings->absolute_ping, $event->result->ping)) { - return []; + return false; } return [ diff --git a/app/Listeners/Webhook/SendSpeedtestThresholdNotification.php b/app/Listeners/Webhook/SendSpeedtestThresholdNotification.php index 3cf66e460..6af4d5bda 100644 --- a/app/Listeners/Webhook/SendSpeedtestThresholdNotification.php +++ b/app/Listeners/Webhook/SendSpeedtestThresholdNotification.php @@ -55,6 +55,8 @@ public function handle(SpeedtestCompleted $event): void array_push($failed, $this->absolutePingThreshold(event: $event, thresholdSettings: $thresholdSettings)); } + $failed = array_filter($failed); + if (! count($failed)) { Log::warning('Failed webhook thresholds not found, won\'t send notification.'); @@ -77,10 +79,10 @@ public function handle(SpeedtestCompleted $event): void /** * Build webhook notification if absolute download threshold is breached. */ - protected function absoluteDownloadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array + protected function absoluteDownloadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): bool|array { if (! absoluteDownloadThresholdFailed($thresholdSettings->absolute_download, $event->result->download)) { - return []; + return false; } return [ @@ -93,10 +95,10 @@ protected function absoluteDownloadThreshold(SpeedtestCompleted $event, Threshol /** * Build webhook notification if absolute upload threshold is breached. */ - protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array + protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): bool|array { if (! absoluteUploadThresholdFailed($thresholdSettings->absolute_upload, $event->result->upload)) { - return []; + return false; } return [ @@ -109,10 +111,10 @@ protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdS /** * Build webhook notification if absolute ping threshold is breached. */ - protected function absolutePingThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array + protected function absolutePingThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): bool|array { if (! absolutePingThresholdFailed($thresholdSettings->absolute_ping, $event->result->ping)) { - return []; + return false; } return [ diff --git a/resources/views/emails/speedtest-completed.blade.php b/resources/views/emails/speedtest-completed.blade.php index 645e0cb67..eb0042d9f 100644 --- a/resources/views/emails/speedtest-completed.blade.php +++ b/resources/views/emails/speedtest-completed.blade.php @@ -5,7 +5,7 @@ | **Metric** | **Value** | -|-------------|------------------:| +|:------------|------------------:| | Server name | {{ $serverName }} | | Server ID | {{ $serverId }} | | Ping | {{ $ping }} | diff --git a/resources/views/emails/speedtest-threshold.blade.php b/resources/views/emails/speedtest-threshold.blade.php index 032ec41d1..7ae6eb0e5 100644 --- a/resources/views/emails/speedtest-threshold.blade.php +++ b/resources/views/emails/speedtest-threshold.blade.php @@ -5,7 +5,7 @@ | **Metric** | **Threshold** | **Value** | -|------------|--------------:|----------:| +|:-----------|:--------------|----------:| @foreach ($metrics as $item) | {{ $item['name'] }} | {{ $item['threshold'] }} | {{ $item['value'] }} | @endforeach