|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Listeners\Threshold; |
| 4 | + |
| 5 | +use App\Events\ResultCreated; |
| 6 | +use App\Mail\Threshold\AbsoluteMail; |
| 7 | +use App\Settings\NotificationSettings; |
| 8 | +use App\Settings\ThresholdSettings; |
| 9 | +use Filament\Notifications\Notification; |
| 10 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 11 | +use Illuminate\Support\Facades\Log; |
| 12 | +use Illuminate\Support\Facades\Mail; |
| 13 | + |
| 14 | +class AbsoluteListener implements ShouldQueue |
| 15 | +{ |
| 16 | + public $notificationSettings; |
| 17 | + |
| 18 | + public $thresholdSettings; |
| 19 | + |
| 20 | + /** |
| 21 | + * Create the event listener. |
| 22 | + * |
| 23 | + * @return void |
| 24 | + */ |
| 25 | + public function __construct() |
| 26 | + { |
| 27 | + $this->notificationSettings = new (NotificationSettings::class); |
| 28 | + |
| 29 | + $this->thresholdSettings = new (ThresholdSettings::class); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Handle the event. |
| 34 | + * |
| 35 | + * @param \App\Events\ResultCreated $event |
| 36 | + * @return void |
| 37 | + */ |
| 38 | + public function handle(ResultCreated $event) |
| 39 | + { |
| 40 | + if ($this->thresholdSettings->absolute_enabled !== true) { |
| 41 | + Log::info('Absolute threshold notifications disabled.'); |
| 42 | + |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + // Database notification channel |
| 47 | + if ($this->notificationSettings->database_enabled == true && $this->notificationSettings->database_on_threshold_failure == true) { |
| 48 | + $this->databaseChannel($event); |
| 49 | + } |
| 50 | + |
| 51 | + // Mail notification channel |
| 52 | + if ($this->notificationSettings->mail_enabled == true && $this->notificationSettings->mail_on_threshold_failure == true) { |
| 53 | + $this->mailChannel($event); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Handle database notifications. |
| 59 | + * |
| 60 | + * @param \App\Events\ResultCreated $event |
| 61 | + * @return void |
| 62 | + */ |
| 63 | + protected function databaseChannel(ResultCreated $event) |
| 64 | + { |
| 65 | + // Download threshold |
| 66 | + if ($this->thresholdSettings->absolute_download > 0) { |
| 67 | + if (absoluteDownloadThresholdFailed($this->thresholdSettings->absolute_download, $event->result->download)) { |
| 68 | + Notification::make() |
| 69 | + ->title('Threshold breached') |
| 70 | + ->body('Speedtest #'.$event->result->id.' breached the download threshold of '.$this->thresholdSettings->absolute_download.'Mbps at '.formatBits(formatBytesToBits($event->result->download), 2, false).'Mbps.') |
| 71 | + ->warning() |
| 72 | + ->sendToDatabase($event->user); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // Upload threshold |
| 77 | + if ($this->thresholdSettings->absolute_upload > 0) { |
| 78 | + if (absoluteUploadThresholdFailed($this->thresholdSettings->absolute_upload, $event->result->upload)) { |
| 79 | + Notification::make() |
| 80 | + ->title('Threshold breached') |
| 81 | + ->body('Speedtest #'.$event->result->id.' breached the upload threshold of '.$this->thresholdSettings->absolute_upload.'Mbps at '.formatBits(formatBytesToBits($event->result->upload), 2, false).'Mbps.') |
| 82 | + ->warning() |
| 83 | + ->sendToDatabase($event->user); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Ping threshold |
| 88 | + if ($this->thresholdSettings->absolute_ping > 0) { |
| 89 | + if (absolutePingThresholdFailed($this->thresholdSettings->absolute_ping, $event->result->ping)) { |
| 90 | + Notification::make() |
| 91 | + ->title('Threshold breached') |
| 92 | + ->body('Speedtest #'.$event->result->id.' breached the ping threshold of '.$this->thresholdSettings->absolute_ping.'ms at '.$event->result->ping.'ms.') |
| 93 | + ->warning() |
| 94 | + ->sendToDatabase($event->user); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Handle database notifications. |
| 101 | + * |
| 102 | + * @param \App\Events\ResultCreated $event |
| 103 | + * @return void |
| 104 | + */ |
| 105 | + protected function mailChannel(ResultCreated $event) |
| 106 | + { |
| 107 | + $failedThresholds = []; |
| 108 | + |
| 109 | + if (! count($this->notificationSettings->mail_recipients) > 0) { |
| 110 | + Log::info('Skipping sending mail notification, no recipients.'); |
| 111 | + } |
| 112 | + |
| 113 | + // Download threshold |
| 114 | + if ($this->thresholdSettings->absolute_download > 0) { |
| 115 | + if (absoluteDownloadThresholdFailed($this->thresholdSettings->absolute_download, $event->result->download)) { |
| 116 | + array_push($failedThresholds, [ |
| 117 | + 'name' => 'Download', |
| 118 | + 'threshold' => $this->thresholdSettings->absolute_download.' Mbps', |
| 119 | + 'value' => formatBits(formatBytesToBits($event->result->download)).'ps', |
| 120 | + ]); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // Upload threshold |
| 125 | + if ($this->thresholdSettings->absolute_upload > 0) { |
| 126 | + if (absoluteUploadThresholdFailed($this->thresholdSettings->absolute_upload, $event->result->upload)) { |
| 127 | + array_push($failedThresholds, [ |
| 128 | + 'name' => 'Upload', |
| 129 | + 'threshold' => $this->thresholdSettings->absolute_upload.' Mbps', |
| 130 | + 'value' => formatBits(formatBytesToBits($event->result->upload)).'ps', |
| 131 | + ]); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // Ping threshold |
| 136 | + if ($this->thresholdSettings->absolute_ping > 0) { |
| 137 | + if (absolutePingThresholdFailed($this->thresholdSettings->absolute_ping, $event->result->ping)) { |
| 138 | + array_push($failedThresholds, [ |
| 139 | + 'name' => 'Ping', |
| 140 | + 'threshold' => $this->thresholdSettings->absolute_ping.' Ms', |
| 141 | + 'value' => round($event->result->ping, 2).' Ms', |
| 142 | + ]); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + if (count($failedThresholds)) { |
| 147 | + foreach ($this->notificationSettings->mail_recipients as $recipient) { |
| 148 | + Mail::to($recipient) |
| 149 | + ->send(new AbsoluteMail($event->result, $failedThresholds)); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments