From eeaef2750b32e57dcf1591b2f76eb3d00689420f Mon Sep 17 00:00:00 2001 From: Alex Justesen Date: Thu, 29 Feb 2024 23:10:41 -0500 Subject: [PATCH] refactored Discord notification to their own listeners --- .../SendSpeedtestCompletedNotification.php | 55 ++++++++ .../SendSpeedtestThresholdNotification.php | 122 ++++++++++++++++++ app/Listeners/SpeedtestCompletedListener.php | 56 -------- app/Listeners/Threshold/AbsoluteListener.php | 102 --------------- app/Providers/EventServiceProvider.php | 12 +- .../discord/speedtest-completed.blade.php | 9 ++ .../discord/speedtest-threshold.blade.php | 7 + .../telegram/speedtest-threshold.blade.php | 2 +- 8 files changed, 200 insertions(+), 165 deletions(-) create mode 100644 app/Listeners/Discord/SendSpeedtestCompletedNotification.php create mode 100644 app/Listeners/Discord/SendSpeedtestThresholdNotification.php delete mode 100644 app/Listeners/SpeedtestCompletedListener.php delete mode 100644 app/Listeners/Threshold/AbsoluteListener.php create mode 100644 resources/views/discord/speedtest-completed.blade.php create mode 100644 resources/views/discord/speedtest-threshold.blade.php diff --git a/app/Listeners/Discord/SendSpeedtestCompletedNotification.php b/app/Listeners/Discord/SendSpeedtestCompletedNotification.php new file mode 100644 index 000000000..56c1b6b27 --- /dev/null +++ b/app/Listeners/Discord/SendSpeedtestCompletedNotification.php @@ -0,0 +1,55 @@ +discord_enabled) { + return; + } + + if (! $notificationSettings->discord_on_speedtest_run) { + return; + } + + if (! count($notificationSettings->discord_webhooks)) { + Log::warning('Discord urls not found, check Discord notification channel settings.'); + + return; + } + + $payload = [ + 'content' => view('discord.speedtest-completed', [ + 'id' => $event->result->id, + 'service' => Str::title($event->result->service), + 'serverName' => $event->result->server_name, + 'serverId' => $event->result->server_id, + 'ping' => round($event->result->ping).' ms', + 'download' => Number::toBitRate(bits: $event->result->download_bits, precision: 2), + 'upload' => Number::toBitRate(bits: $event->result->upload_bits, precision: 2), + ])->render(), + ]; + + foreach ($notificationSettings->discord_webhooks as $url) { + WebhookCall::create() + ->url($url['url']) + ->payload($payload) + ->doNotSign() + ->dispatch(); + } + } +} diff --git a/app/Listeners/Discord/SendSpeedtestThresholdNotification.php b/app/Listeners/Discord/SendSpeedtestThresholdNotification.php new file mode 100644 index 000000000..82bf5708b --- /dev/null +++ b/app/Listeners/Discord/SendSpeedtestThresholdNotification.php @@ -0,0 +1,122 @@ +discord_enabled) { + return; + } + + if (! $notificationSettings->discord_on_threshold_failure) { + return; + } + + if (! count($notificationSettings->discord_webhooks)) { + Log::warning('Discord urls not found, check Discord notification channel settings.'); + + return; + } + + $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; + } + + $payload = [ + 'content' => view('discord.speedtest-threshold', [ + 'id' => $event->result->id, + 'service' => Str::title($event->result->service), + 'serverName' => $event->result->server_name, + 'serverId' => $event->result->server_id, + 'metrics' => $failed, + ])->render(), + ]; + + foreach ($notificationSettings->discord_webhooks as $url) { + WebhookCall::create() + ->url($url['url']) + ->payload($payload) + ->doNotSign() + ->dispatch(); + } + } + + /** + * Build Discord 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.' Mbps', + 'value' => Number::toBitRate(bits: $event->result->download_bits, precision: 2), + ]; + } + + /** + * Build Discord 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.' Mbps', + 'value' => Number::toBitRate(bits: $event->result->upload_bits, precision: 2), + ]; + } + + /** + * Build Discord 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.' ms', + 'value' => round($event->result->ping, 2).' ms', + ]; + } +} diff --git a/app/Listeners/SpeedtestCompletedListener.php b/app/Listeners/SpeedtestCompletedListener.php deleted file mode 100644 index 84a889145..000000000 --- a/app/Listeners/SpeedtestCompletedListener.php +++ /dev/null @@ -1,56 +0,0 @@ -generalSettings = new (GeneralSettings::class); - - $this->notificationSettings = new (NotificationSettings::class); - } - - /** - * Handle the event. - */ - public function handle(SpeedtestCompleted $event): void - { - if ($this->notificationSettings->discord_enabled) { - if ($this->notificationSettings->discord_on_speedtest_run && count($this->notificationSettings->discord_webhooks)) { - foreach ($this->notificationSettings->discord_webhooks as $webhook) { - // Construct the payload - $payload = [ - 'content' => 'There are new speedtest results for your network.'. - "\nResult ID: ".$event->result->id. - "\nSite Name: ".$this->generalSettings->site_name. - "\nPing: ".$event->result->ping.' ms'. - "\nDownload: ".($event->result->downloadBits / 1000000).' (Mbps)'. - "\nUpload: ".($event->result->uploadBits / 1000000).' (Mbps)', - ]; - // Send the payload using WebhookCall - WebhookCall::create() - ->url($webhook['url']) - ->payload($payload) - ->doNotSign() - ->dispatch(); - - } - } - } - } -} diff --git a/app/Listeners/Threshold/AbsoluteListener.php b/app/Listeners/Threshold/AbsoluteListener.php deleted file mode 100644 index efaac8ee0..000000000 --- a/app/Listeners/Threshold/AbsoluteListener.php +++ /dev/null @@ -1,102 +0,0 @@ -generalSettings = new (GeneralSettings::class); - - $this->notificationSettings = new (NotificationSettings::class); - - $this->thresholdSettings = new (ThresholdSettings::class); - } - - /** - * Handle the event. - */ - public function handle(SpeedtestCompleted $event): void - { - if (! $this->thresholdSettings->absolute_enabled) { - return; - } - - // Discord notification channel - if ($this->notificationSettings->discord_enabled == true && $this->notificationSettings->discord_on_threshold_failure == true) { - $this->discordChannel($event); - } - } - - /** - * Handle Discord notifications. - */ - protected function discordChannel(SpeedtestCompleted $event): void - { - if ($this->notificationSettings->discord_enabled) { - $failedThresholds = []; // Initialize an array to keep track of failed thresholds - - // Check Download threshold - if ($this->thresholdSettings->absolute_download > 0 && absoluteDownloadThresholdFailed($this->thresholdSettings->absolute_download, $event->result->download)) { - $failedThresholds['Download'] = toBits(convertSize($event->result->download), 2).' Mbps'; - } - - // Check Upload threshold - if ($this->thresholdSettings->absolute_upload > 0 && absoluteUploadThresholdFailed($this->thresholdSettings->absolute_upload, $event->result->upload)) { - $failedThresholds['Upload'] = toBits(convertSize($event->result->upload), 2).' Mbps'; - } - - // Check Ping threshold - if ($this->thresholdSettings->absolute_ping > 0 && absolutePingThresholdFailed($this->thresholdSettings->absolute_ping, $event->result->ping)) { - $failedThresholds['Ping'] = $event->result->ping.' ms'; - } - - // Proceed with sending notifications only if there are any failed thresholds - if (count($failedThresholds) > 0) { - if ($this->notificationSettings->discord_on_threshold_failure && count($this->notificationSettings->discord_webhooks)) { - foreach ($this->notificationSettings->discord_webhooks as $webhook) { - // Construct the payload with the failed thresholds information - $contentLines = [ - 'Result ID: '.$event->result->id, - 'Site Name: '.$this->generalSettings->site_name, - ]; - - foreach ($failedThresholds as $metric => $result) { - $contentLines[] = "{$metric} threshold failed with result: {$result}."; - } - - $payload = [ - 'content' => implode("\n", $contentLines), - ]; - - // Send the payload using WebhookCall - WebhookCall::create() - ->url($webhook['url']) - ->payload($payload) - ->doNotSign() - ->dispatch(); - - } - } - } - } - } -} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index d2815f1a4..1c9e80c0d 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -9,12 +9,12 @@ use App\Listeners\Data\InfluxDb2Listener; use App\Listeners\Database\SendSpeedtestCompletedNotification as DatabaseSendSpeedtestCompletedNotification; use App\Listeners\Database\SendSpeedtestThresholdNotification as DatabaseSendSpeedtestThresholdNotification; +use App\Listeners\Discord\SendSpeedtestCompletedNotification as DiscordSendSpeedtestCompletedNotification; +use App\Listeners\Discord\SendSpeedtestThresholdNotification as DiscordSendSpeedtestThresholdNotification; use App\Listeners\Mail\SendSpeedtestCompletedNotification as MailSendSpeedtestCompletedNotification; use App\Listeners\Mail\SendSpeedtestThresholdNotification as MailSendSpeedtestThresholdNotification; -use App\Listeners\SpeedtestCompletedListener; use App\Listeners\Telegram\SendSpeedtestCompletedNotification as TelegramSendSpeedtestCompletedNotification; use App\Listeners\Telegram\SendSpeedtestThresholdNotification as TelegramSendSpeedtestThresholdNotification; -use App\Listeners\Threshold\AbsoluteListener; use App\Listeners\Webhook\SendSpeedtestCompletedNotification as WebhookSendSpeedtestCompletedNotification; use App\Listeners\Webhook\SendSpeedtestThresholdNotification as WebhookSendSpeedtestThresholdNotification; use Illuminate\Auth\Events\Registered; @@ -40,8 +40,6 @@ class EventServiceProvider extends ServiceProvider ], SpeedtestCompleted::class => [ - SpeedtestCompletedListener::class, - // Data listeners InfluxDb2Listener::class, @@ -49,6 +47,10 @@ class EventServiceProvider extends ServiceProvider DatabaseSendSpeedtestCompletedNotification::class, DatabaseSendSpeedtestThresholdNotification::class, + // Discord notification listeners + DiscordSendSpeedtestCompletedNotification::class, + DiscordSendSpeedtestThresholdNotification::class, + // Mail notification listeners MailSendSpeedtestCompletedNotification::class, MailSendSpeedtestThresholdNotification::class, @@ -60,8 +62,6 @@ class EventServiceProvider extends ServiceProvider // Webhook notification listeners WebhookSendSpeedtestCompletedNotification::class, WebhookSendSpeedtestThresholdNotification::class, - - AbsoluteListener::class, ], SpeedtestFailed::class => [ diff --git a/resources/views/discord/speedtest-completed.blade.php b/resources/views/discord/speedtest-completed.blade.php new file mode 100644 index 000000000..618c3eafe --- /dev/null +++ b/resources/views/discord/speedtest-completed.blade.php @@ -0,0 +1,9 @@ +**Speedtest Completed - #{{ $id }}** + +A new speedtest was completed using **{{ $service }}**. + +- **Server name:** {{ $serverName }} +- **Server ID:** {{ $serverId }} +- **Ping:** {{ $ping }} +- **Download:** {{ $download }} +- **Upload:** {{ $upload }} diff --git a/resources/views/discord/speedtest-threshold.blade.php b/resources/views/discord/speedtest-threshold.blade.php new file mode 100644 index 000000000..2fc1ac014 --- /dev/null +++ b/resources/views/discord/speedtest-threshold.blade.php @@ -0,0 +1,7 @@ +**Speedtest Threshold Breached - #{{ $id }}** + +A new speedtest was completed using **{{ $service }}** but a threshold was breached. + +@foreach ($metrics as $item) +- **{{ $item['name'] }}** {{ $item['threshold'] }}: {{ $item['value'] }} +@endforeach diff --git a/resources/views/telegram/speedtest-threshold.blade.php b/resources/views/telegram/speedtest-threshold.blade.php index 961a90d79..3db2f1826 100644 --- a/resources/views/telegram/speedtest-threshold.blade.php +++ b/resources/views/telegram/speedtest-threshold.blade.php @@ -1,6 +1,6 @@ *Speedtest Threshold Breached - #{{ $id }}* -A new speedtest was completed using **{{ $service }}** but a threshold was breached. +A new speedtest was completed using *{{ $service }}* but a threshold was breached. @foreach ($metrics as $item) - *{{ $item['name'] }}* {{ $item['threshold'] }}: {{ $item['value'] }}