diff --git a/app/Listeners/Database/SendSpeedtestCompletedNotification.php b/app/Listeners/Database/SendSpeedtestCompletedNotification.php new file mode 100644 index 000000000..a1bc97975 --- /dev/null +++ b/app/Listeners/Database/SendSpeedtestCompletedNotification.php @@ -0,0 +1,34 @@ +database_enabled) { + return; + } + + if (! $notificationSettings->database_on_speedtest_run) { + return; + } + + foreach (User::all() as $user) { + Notification::make() + ->title('Speedtest completed') + ->success() + ->sendToDatabase($user); + } + } +} diff --git a/app/Listeners/Database/SendSpeedtestThresholdNotification.php b/app/Listeners/Database/SendSpeedtestThresholdNotification.php new file mode 100644 index 000000000..4038b42b0 --- /dev/null +++ b/app/Listeners/Database/SendSpeedtestThresholdNotification.php @@ -0,0 +1,97 @@ +database_enabled) { + return; + } + + if (! $notificationSettings->database_on_threshold_failure) { + return; + } + + if ($thresholdSettings->absolute_download > 0) { + $this->absoluteDownloadThreshold(event: $event, thresholdSettings: $thresholdSettings); + } + + if ($thresholdSettings->absolute_upload > 0) { + $this->absoluteUploadThreshold(event: $event, thresholdSettings: $thresholdSettings); + } + + if ($thresholdSettings->absolute_ping > 0) { + $this->absolutePingThreshold(event: $event, thresholdSettings: $thresholdSettings); + } + } + + /** + * Send database notification if absolute download threshold is breached. + */ + protected function absoluteDownloadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): void + { + if (! absoluteDownloadThresholdFailed($thresholdSettings->absolute_download, $event->result->download)) { + return; + } + + foreach (User::all() as $user) { + Notification::make() + ->title('Download threshold breached!') + ->body('Speedtest #'.$event->result->id.' breached the download threshold of '.$thresholdSettings->absolute_download.' Mbps at '.Number::toBitRate($event->result->download_bits).'.') + ->warning() + ->sendToDatabase($user); + } + } + + /** + * Send database notification if absolute upload threshold is breached. + */ + protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): void + { + if (! absoluteUploadThresholdFailed($thresholdSettings->absolute_upload, $event->result->upload)) { + return; + } + + foreach (User::all() as $user) { + Notification::make() + ->title('Upload threshold breached!') + ->body('Speedtest #'.$event->result->id.' breached the upload threshold of '.$thresholdSettings->absolute_upload.' Mbps at '.Number::toBitRate($event->result->upload_bits).'.') + ->warning() + ->sendToDatabase($user); + } + } + + /** + * Send database notification if absolute upload threshold is breached. + */ + protected function absolutePingThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): void + { + if (! absolutePingThresholdFailed($thresholdSettings->absolute_ping, $event->result->ping)) { + return; + } + + foreach (User::all() as $user) { + Notification::make() + ->title('Ping threshold breached!') + ->body('Speedtest #'.$event->result->id.' breached the ping threshold of '.$thresholdSettings->absolute_ping.'ms at '.$event->result->ping.'ms.') + ->warning() + ->sendToDatabase($user); + } + } +} diff --git a/app/Listeners/SpeedtestCompletedListener.php b/app/Listeners/SpeedtestCompletedListener.php index 0892469e7..3a9d0af50 100644 --- a/app/Listeners/SpeedtestCompletedListener.php +++ b/app/Listeners/SpeedtestCompletedListener.php @@ -4,11 +4,9 @@ use App\Events\SpeedtestCompleted; use App\Mail\SpeedtestCompletedMail; -use App\Models\User; use App\Settings\GeneralSettings; use App\Settings\NotificationSettings; use App\Telegram\TelegramNotification; -use Filament\Notifications\Notification; use Illuminate\Support\Facades\Mail; use Spatie\WebhookServer\WebhookCall; @@ -35,15 +33,6 @@ public function __construct() */ public function handle(SpeedtestCompleted $event): void { - if ($this->notificationSettings->database_enabled && $this->notificationSettings->database_on_speedtest_run) { - foreach (User::all() as $user) { - Notification::make() - ->title('Speedtest completed') - ->success() - ->sendToDatabase($user); - } - } - if ($this->notificationSettings->mail_enabled) { if ($this->notificationSettings->mail_on_speedtest_run && count($this->notificationSettings->mail_recipients)) { foreach ($this->notificationSettings->mail_recipients as $recipient) { diff --git a/app/Listeners/Threshold/AbsoluteListener.php b/app/Listeners/Threshold/AbsoluteListener.php index ac3743d97..004517f2c 100644 --- a/app/Listeners/Threshold/AbsoluteListener.php +++ b/app/Listeners/Threshold/AbsoluteListener.php @@ -4,12 +4,10 @@ use App\Events\SpeedtestCompleted; use App\Mail\Threshold\AbsoluteMail; -use App\Models\User; use App\Settings\GeneralSettings; use App\Settings\NotificationSettings; use App\Settings\ThresholdSettings; use App\Telegram\TelegramNotification; -use Filament\Notifications\Notification; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; @@ -43,17 +41,10 @@ public function __construct() */ public function handle(SpeedtestCompleted $event): void { - if ($this->thresholdSettings->absolute_enabled !== true) { - Log::info('Absolute threshold notifications disabled.'); - + if (! $this->thresholdSettings->absolute_enabled) { return; } - // Database notification channel - if ($this->notificationSettings->database_enabled == true && $this->notificationSettings->database_on_threshold_failure == true) { - $this->databaseChannel($event); - } - // Mail notification channel if ($this->notificationSettings->mail_enabled == true && $this->notificationSettings->mail_on_threshold_failure == true) { $this->mailChannel($event); @@ -75,51 +66,6 @@ public function handle(SpeedtestCompleted $event): void } } - /** - * Handle database notifications. - */ - protected function databaseChannel(SpeedtestCompleted $event): void - { - // Download threshold - if ($this->thresholdSettings->absolute_download > 0) { - if (absoluteDownloadThresholdFailed($this->thresholdSettings->absolute_download, $event->result->download)) { - foreach (User::all() as $user) { - Notification::make() - ->title('Threshold breached') - ->body('Speedtest #'.$event->result->id.' breached the download threshold of '.$this->thresholdSettings->absolute_download.'Mbps at '.toBits(convertSize($event->result->download), 2).'Mbps.') - ->warning() - ->sendToDatabase($user); - } - } - } - - // Upload threshold - if ($this->thresholdSettings->absolute_upload > 0) { - if (absoluteUploadThresholdFailed($this->thresholdSettings->absolute_upload, $event->result->upload)) { - foreach (User::all() as $user) { - Notification::make() - ->title('Threshold breached') - ->body('Speedtest #'.$event->result->id.' breached the upload threshold of '.$this->thresholdSettings->absolute_upload.'Mbps at '.toBits(convertSize($event->result->upload), 2).'Mbps.') - ->warning() - ->sendToDatabase($user); - } - } - } - - // Ping threshold - if ($this->thresholdSettings->absolute_ping > 0) { - if (absolutePingThresholdFailed($this->thresholdSettings->absolute_ping, $event->result->ping)) { - foreach (User::all() as $user) { - Notification::make() - ->title('Threshold breached') - ->body('Speedtest #'.$event->result->id.' breached the ping threshold of '.$this->thresholdSettings->absolute_ping.'ms at '.$event->result->ping.'ms.') - ->warning() - ->sendToDatabase($user); - } - } - } - } - /** * Handle database notifications. */ diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 2e699bf60..6b949afc1 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -7,6 +7,8 @@ use App\Events\SpeedtestStarted; use App\Listeners\ClearApplicationCache; use App\Listeners\Data\InfluxDb2Listener; +use App\Listeners\Database\SendSpeedtestCompletedNotification as DatabaseSendSpeedtestCompletedNotification; +use App\Listeners\Database\SendSpeedtestThresholdNotification as DatabaseSendSpeedtestThresholdNotification; use App\Listeners\SpeedtestCompletedListener; use App\Listeners\Threshold\AbsoluteListener; use Illuminate\Auth\Events\Registered; @@ -38,6 +40,9 @@ class EventServiceProvider extends ServiceProvider InfluxDb2Listener::class, // Notification listeners + DatabaseSendSpeedtestCompletedNotification::class, + DatabaseSendSpeedtestThresholdNotification::class, + AbsoluteListener::class, ],