diff --git a/app/Listeners/SpeedtestCompletedListener.php b/app/Listeners/SpeedtestCompletedListener.php index 3a9d0af50..a12982a54 100644 --- a/app/Listeners/SpeedtestCompletedListener.php +++ b/app/Listeners/SpeedtestCompletedListener.php @@ -87,23 +87,5 @@ public function handle(SpeedtestCompleted $event): void } } } - - if ($this->notificationSettings->webhook_enabled) { - if ($this->notificationSettings->webhook_on_speedtest_run && count($this->notificationSettings->webhook_urls)) { - foreach ($this->notificationSettings->webhook_urls as $url) { - WebhookCall::create() - ->url($url['url']) - ->payload([ - 'result_id' => $event->result->id, - 'site_name' => $this->generalSettings->site_name, - 'ping' => $event->result->ping, - 'download' => $event->result->downloadBits, - 'upload' => $event->result->uploadBits, - ]) - ->doNotSign() - ->dispatch(); - } - } - } } } diff --git a/app/Listeners/Threshold/AbsoluteListener.php b/app/Listeners/Threshold/AbsoluteListener.php index 004517f2c..635f7be33 100644 --- a/app/Listeners/Threshold/AbsoluteListener.php +++ b/app/Listeners/Threshold/AbsoluteListener.php @@ -9,7 +9,6 @@ use App\Settings\ThresholdSettings; use App\Telegram\TelegramNotification; use Illuminate\Contracts\Queue\ShouldQueue; -use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Mail; use Spatie\WebhookServer\WebhookCall; @@ -59,11 +58,6 @@ public function handle(SpeedtestCompleted $event): void if ($this->notificationSettings->discord_enabled == true && $this->notificationSettings->discord_on_threshold_failure == true) { $this->discordChannel($event); } - - // Webhook notification channel - if ($this->notificationSettings->webhook_enabled == true && $this->notificationSettings->webhook_on_threshold_failure == true) { - $this->webhookChannel($event); - } } /** @@ -230,71 +224,4 @@ protected function discordChannel(SpeedtestCompleted $event): void } } } - - /** - * Handle webhook notifications. - * - * TODO: refactor - */ - protected function webhookChannel(SpeedtestCompleted $event): void - { - $failedThresholds = []; - - if (! count($this->notificationSettings->webhook_urls) > 0) { - Log::info('Skipping sending webhook notification, no urls.'); - } - - // Download threshold - if ($this->thresholdSettings->absolute_download > 0) { - if (absoluteDownloadThresholdFailed($this->thresholdSettings->absolute_download, $event->result->download)) { - array_push($failedThresholds, [ - 'name' => 'Download', - 'threshold' => $this->thresholdSettings->absolute_download, - 'value' => toBits(convertSize($event->result->download), 2), - ]); - } - } - - // Upload threshold - if ($this->thresholdSettings->absolute_upload > 0) { - if (absoluteUploadThresholdFailed($this->thresholdSettings->absolute_upload, $event->result->upload)) { - array_push($failedThresholds, [ - 'name' => 'Upload', - 'threshold' => $this->thresholdSettings->absolute_upload, - 'value' => toBits(convertSize($event->result->upload), 2), - ]); - } - } - - // Ping threshold - if ($this->thresholdSettings->absolute_ping > 0) { - if (absolutePingThresholdFailed($this->thresholdSettings->absolute_ping, $event->result->ping)) { - array_push($failedThresholds, [ - 'name' => 'Ping', - 'threshold' => $this->thresholdSettings->absolute_ping, - 'value' => round($event->result->ping, 2), - ]); - } - } - - if (count($failedThresholds)) { - foreach ($this->notificationSettings->webhook_urls as $url) { - Http::post($url['url'], [ - 'result_id' => $event->result->id, - 'site_name' => $this->generalSettings->site_name, - 'metrics' => $failedThresholds, - ]); - - WebhookCall::create() - ->url($url['url']) - ->payload([ - 'result_id' => $event->result->id, - 'site_name' => $this->generalSettings->site_name, - 'metrics' => $failedThresholds, - ]) - ->doNotSign() - ->dispatch(); - } - } - } } diff --git a/app/Listeners/Webhook/SendSpeedtestCompletedNotification.php b/app/Listeners/Webhook/SendSpeedtestCompletedNotification.php new file mode 100644 index 000000000..9000464d5 --- /dev/null +++ b/app/Listeners/Webhook/SendSpeedtestCompletedNotification.php @@ -0,0 +1,50 @@ +webhook_enabled) { + return; + } + + if (! $notificationSettings->webhook_on_speedtest_run) { + return; + } + + if (! count($notificationSettings->webhook_urls)) { + Log::warning('Webhook urls not found, check webhook notification channel settings.'); + + return; + } + + foreach ($notificationSettings->webhook_urls as $url) { + WebhookCall::create() + ->url($url['url']) + ->payload([ + 'result_id' => $event->result->id, + 'site_name' => $generalSettings->site_name, + 'ping' => $event->result->ping, + 'download' => $event->result->downloadBits, + 'upload' => $event->result->uploadBits, + ]) + ->doNotSign() + ->dispatch(); + } + } +} diff --git a/app/Listeners/Webhook/SendSpeedtestThresholdNotification.php b/app/Listeners/Webhook/SendSpeedtestThresholdNotification.php new file mode 100644 index 000000000..f622d3b31 --- /dev/null +++ b/app/Listeners/Webhook/SendSpeedtestThresholdNotification.php @@ -0,0 +1,118 @@ +webhook_enabled) { + return; + } + + if (! $notificationSettings->webhook_on_threshold_failure) { + return; + } + + if (! count($notificationSettings->webhook_urls)) { + Log::warning('Webhook urls not found, check webhook notification channel settings.'); + + return; + } + + $generalSettings = new GeneralSettings(); + + $thresholdSettings = new ThresholdSettings(); + + $failed = []; + + if ($thresholdSettings->absolute_download > 0) { + array_push($failed, $this->absoluteDownloadThreshold(event: $event, thresholdSettings: $thresholdSettings)); + } + + if ($thresholdSettings->absolute_upload > 0) { + array_push($failed, $this->absoluteUploadThreshold(event: $event, thresholdSettings: $thresholdSettings)); + } + + if ($thresholdSettings->absolute_ping > 0) { + array_push($failed, $this->absolutePingThreshold(event: $event, thresholdSettings: $thresholdSettings)); + } + + if (! count($failed)) { + return; + } + + foreach ($notificationSettings->webhook_urls as $url) { + WebhookCall::create() + ->url($url['url']) + ->payload([ + 'result_id' => $event->result->id, + 'site_name' => $generalSettings->site_name, + 'metrics' => $failed, + ]) + ->doNotSign() + ->dispatch(); + } + } + + /** + * Build webhook notification if absolute download threshold is breached. + */ + protected function absoluteDownloadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array + { + if (! absoluteDownloadThresholdFailed($thresholdSettings->absolute_download, $event->result->download)) { + return []; + } + + return [ + 'name' => 'Download', + 'threshold' => $thresholdSettings->absolute_download, + 'value' => Number::toBitRate(bits: $event->result->download_bits, precision: 2), + ]; + } + + /** + * Build webhook notification if absolute upload threshold is breached. + */ + protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array + { + if (! absoluteUploadThresholdFailed($thresholdSettings->absolute_upload, $event->result->upload)) { + return []; + } + + return [ + 'name' => 'Upload', + 'threshold' => $thresholdSettings->absolute_upload, + 'value' => Number::toBitRate(bits: $event->result->upload_bits, precision: 2), + ]; + } + + /** + * Build webhook notification if absolute ping threshold is breached. + */ + protected function absolutePingThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array + { + if (! absolutePingThresholdFailed($thresholdSettings->absolute_ping, $event->result->ping)) { + return []; + } + + return [ + 'name' => 'Ping', + 'threshold' => $thresholdSettings->absolute_ping, + 'value' => round($event->result->ping, 2), + ]; + } +} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 6b949afc1..54ed38cb4 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -11,6 +11,8 @@ use App\Listeners\Database\SendSpeedtestThresholdNotification as DatabaseSendSpeedtestThresholdNotification; use App\Listeners\SpeedtestCompletedListener; use App\Listeners\Threshold\AbsoluteListener; +use App\Listeners\Webhook\SendSpeedtestCompletedNotification as WebhookSendSpeedtestCompletedNotification; +use App\Listeners\Webhook\SendSpeedtestThresholdNotification as WebhookSendSpeedtestThresholdNotification; use Illuminate\Auth\Events\Registered; use Illuminate\Auth\Listeners\SendEmailVerificationNotification; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; @@ -39,10 +41,14 @@ class EventServiceProvider extends ServiceProvider // Data listeners InfluxDb2Listener::class, - // Notification listeners + // Database notification listeners DatabaseSendSpeedtestCompletedNotification::class, DatabaseSendSpeedtestThresholdNotification::class, + // Webhook notification listeners + WebhookSendSpeedtestCompletedNotification::class, + WebhookSendSpeedtestThresholdNotification::class, + AbsoluteListener::class, ],