Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions app/Listeners/Database/SendSpeedtestCompletedNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Listeners\Database;

use App\Events\SpeedtestCompleted;
use App\Models\User;
use App\Settings\NotificationSettings;
use Filament\Notifications\Notification;

class SendSpeedtestCompletedNotification
{
/**
* Handle the event.
*/
public function handle(SpeedtestCompleted $event): void
{
$notificationSettings = new NotificationSettings();

if (! $notificationSettings->database_enabled) {
return;
}

if (! $notificationSettings->database_on_speedtest_run) {
return;
}

foreach (User::all() as $user) {
Notification::make()
->title('Speedtest completed')
->success()
->sendToDatabase($user);
}
}
}
97 changes: 97 additions & 0 deletions app/Listeners/Database/SendSpeedtestThresholdNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace App\Listeners\Database;

use App\Events\SpeedtestCompleted;
use App\Helpers\Number;
use App\Models\User;
use App\Settings\NotificationSettings;
use App\Settings\ThresholdSettings;
use Filament\Notifications\Notification;

class SendSpeedtestThresholdNotification
{
/**
* Handle the event.
*/
public function handle(SpeedtestCompleted $event): void
{
$notificationSettings = new NotificationSettings();

$thresholdSettings = new ThresholdSettings();

if (! $notificationSettings->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);
}
}
}
11 changes: 0 additions & 11 deletions app/Listeners/SpeedtestCompletedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down
56 changes: 1 addition & 55 deletions app/Listeners/Threshold/AbsoluteListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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.
*/
Expand Down
5 changes: 5 additions & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,6 +40,9 @@ class EventServiceProvider extends ServiceProvider
InfluxDb2Listener::class,

// Notification listeners
DatabaseSendSpeedtestCompletedNotification::class,
DatabaseSendSpeedtestThresholdNotification::class,

AbsoluteListener::class,
],

Expand Down