|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Listeners\Discord; |
| 4 | + |
| 5 | +use App\Events\SpeedtestCompleted; |
| 6 | +use App\Helpers\Number; |
| 7 | +use App\Settings\NotificationSettings; |
| 8 | +use App\Settings\ThresholdSettings; |
| 9 | +use Illuminate\Support\Facades\Log; |
| 10 | +use Illuminate\Support\Str; |
| 11 | +use Spatie\WebhookServer\WebhookCall; |
| 12 | + |
| 13 | +class SendSpeedtestThresholdNotification |
| 14 | +{ |
| 15 | + /** |
| 16 | + * Handle the event. |
| 17 | + */ |
| 18 | + public function handle(SpeedtestCompleted $event): void |
| 19 | + { |
| 20 | + $notificationSettings = new NotificationSettings(); |
| 21 | + |
| 22 | + if (! $notificationSettings->discord_enabled) { |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + if (! $notificationSettings->discord_on_threshold_failure) { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + if (! count($notificationSettings->discord_webhooks)) { |
| 31 | + Log::warning('Discord urls not found, check Discord notification channel settings.'); |
| 32 | + |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + $thresholdSettings = new ThresholdSettings(); |
| 37 | + |
| 38 | + $failed = []; |
| 39 | + |
| 40 | + if ($thresholdSettings->absolute_download > 0) { |
| 41 | + array_push($failed, $this->absoluteDownloadThreshold(event: $event, thresholdSettings: $thresholdSettings)); |
| 42 | + } |
| 43 | + |
| 44 | + if ($thresholdSettings->absolute_upload > 0) { |
| 45 | + array_push($failed, $this->absoluteUploadThreshold(event: $event, thresholdSettings: $thresholdSettings)); |
| 46 | + } |
| 47 | + |
| 48 | + if ($thresholdSettings->absolute_ping > 0) { |
| 49 | + array_push($failed, $this->absolutePingThreshold(event: $event, thresholdSettings: $thresholdSettings)); |
| 50 | + } |
| 51 | + |
| 52 | + if (! count($failed)) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + $payload = [ |
| 57 | + 'content' => view('discord.speedtest-threshold', [ |
| 58 | + 'id' => $event->result->id, |
| 59 | + 'service' => Str::title($event->result->service), |
| 60 | + 'serverName' => $event->result->server_name, |
| 61 | + 'serverId' => $event->result->server_id, |
| 62 | + 'metrics' => $failed, |
| 63 | + ])->render(), |
| 64 | + ]; |
| 65 | + |
| 66 | + foreach ($notificationSettings->discord_webhooks as $url) { |
| 67 | + WebhookCall::create() |
| 68 | + ->url($url['url']) |
| 69 | + ->payload($payload) |
| 70 | + ->doNotSign() |
| 71 | + ->dispatch(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Build Discord notification if absolute download threshold is breached. |
| 77 | + */ |
| 78 | + protected function absoluteDownloadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array |
| 79 | + { |
| 80 | + if (! absoluteDownloadThresholdFailed($thresholdSettings->absolute_download, $event->result->download)) { |
| 81 | + return []; |
| 82 | + } |
| 83 | + |
| 84 | + return [ |
| 85 | + 'name' => 'Download', |
| 86 | + 'threshold' => $thresholdSettings->absolute_download.' Mbps', |
| 87 | + 'value' => Number::toBitRate(bits: $event->result->download_bits, precision: 2), |
| 88 | + ]; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Build Discord notification if absolute upload threshold is breached. |
| 93 | + */ |
| 94 | + protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array |
| 95 | + { |
| 96 | + if (! absoluteUploadThresholdFailed($thresholdSettings->absolute_upload, $event->result->upload)) { |
| 97 | + return []; |
| 98 | + } |
| 99 | + |
| 100 | + return [ |
| 101 | + 'name' => 'Upload', |
| 102 | + 'threshold' => $thresholdSettings->absolute_upload.' Mbps', |
| 103 | + 'value' => Number::toBitRate(bits: $event->result->upload_bits, precision: 2), |
| 104 | + ]; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Build Discord notification if absolute ping threshold is breached. |
| 109 | + */ |
| 110 | + protected function absolutePingThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array |
| 111 | + { |
| 112 | + if (! absolutePingThresholdFailed($thresholdSettings->absolute_ping, $event->result->ping)) { |
| 113 | + return []; |
| 114 | + } |
| 115 | + |
| 116 | + return [ |
| 117 | + 'name' => 'Ping', |
| 118 | + 'threshold' => $thresholdSettings->absolute_ping.' ms', |
| 119 | + 'value' => round($event->result->ping, 2).' ms', |
| 120 | + ]; |
| 121 | + } |
| 122 | +} |
0 commit comments