Skip to content

Commit 25e8b6e

Browse files
authored
[Chore] Refactored database notifications to their own listeners (alexjustesen#1266)
1 parent fcf9fa2 commit 25e8b6e

File tree

5 files changed

+137
-66
lines changed

5 files changed

+137
-66
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace App\Listeners\Database;
4+
5+
use App\Events\SpeedtestCompleted;
6+
use App\Models\User;
7+
use App\Settings\NotificationSettings;
8+
use Filament\Notifications\Notification;
9+
10+
class SendSpeedtestCompletedNotification
11+
{
12+
/**
13+
* Handle the event.
14+
*/
15+
public function handle(SpeedtestCompleted $event): void
16+
{
17+
$notificationSettings = new NotificationSettings();
18+
19+
if (! $notificationSettings->database_enabled) {
20+
return;
21+
}
22+
23+
if (! $notificationSettings->database_on_speedtest_run) {
24+
return;
25+
}
26+
27+
foreach (User::all() as $user) {
28+
Notification::make()
29+
->title('Speedtest completed')
30+
->success()
31+
->sendToDatabase($user);
32+
}
33+
}
34+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace App\Listeners\Database;
4+
5+
use App\Events\SpeedtestCompleted;
6+
use App\Helpers\Number;
7+
use App\Models\User;
8+
use App\Settings\NotificationSettings;
9+
use App\Settings\ThresholdSettings;
10+
use Filament\Notifications\Notification;
11+
12+
class SendSpeedtestThresholdNotification
13+
{
14+
/**
15+
* Handle the event.
16+
*/
17+
public function handle(SpeedtestCompleted $event): void
18+
{
19+
$notificationSettings = new NotificationSettings();
20+
21+
$thresholdSettings = new ThresholdSettings();
22+
23+
if (! $notificationSettings->database_enabled) {
24+
return;
25+
}
26+
27+
if (! $notificationSettings->database_on_threshold_failure) {
28+
return;
29+
}
30+
31+
if ($thresholdSettings->absolute_download > 0) {
32+
$this->absoluteDownloadThreshold(event: $event, thresholdSettings: $thresholdSettings);
33+
}
34+
35+
if ($thresholdSettings->absolute_upload > 0) {
36+
$this->absoluteUploadThreshold(event: $event, thresholdSettings: $thresholdSettings);
37+
}
38+
39+
if ($thresholdSettings->absolute_ping > 0) {
40+
$this->absolutePingThreshold(event: $event, thresholdSettings: $thresholdSettings);
41+
}
42+
}
43+
44+
/**
45+
* Send database notification if absolute download threshold is breached.
46+
*/
47+
protected function absoluteDownloadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): void
48+
{
49+
if (! absoluteDownloadThresholdFailed($thresholdSettings->absolute_download, $event->result->download)) {
50+
return;
51+
}
52+
53+
foreach (User::all() as $user) {
54+
Notification::make()
55+
->title('Download threshold breached!')
56+
->body('Speedtest #'.$event->result->id.' breached the download threshold of '.$thresholdSettings->absolute_download.' Mbps at '.Number::toBitRate($event->result->download_bits).'.')
57+
->warning()
58+
->sendToDatabase($user);
59+
}
60+
}
61+
62+
/**
63+
* Send database notification if absolute upload threshold is breached.
64+
*/
65+
protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): void
66+
{
67+
if (! absoluteUploadThresholdFailed($thresholdSettings->absolute_upload, $event->result->upload)) {
68+
return;
69+
}
70+
71+
foreach (User::all() as $user) {
72+
Notification::make()
73+
->title('Upload threshold breached!')
74+
->body('Speedtest #'.$event->result->id.' breached the upload threshold of '.$thresholdSettings->absolute_upload.' Mbps at '.Number::toBitRate($event->result->upload_bits).'.')
75+
->warning()
76+
->sendToDatabase($user);
77+
}
78+
}
79+
80+
/**
81+
* Send database notification if absolute upload threshold is breached.
82+
*/
83+
protected function absolutePingThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): void
84+
{
85+
if (! absolutePingThresholdFailed($thresholdSettings->absolute_ping, $event->result->ping)) {
86+
return;
87+
}
88+
89+
foreach (User::all() as $user) {
90+
Notification::make()
91+
->title('Ping threshold breached!')
92+
->body('Speedtest #'.$event->result->id.' breached the ping threshold of '.$thresholdSettings->absolute_ping.'ms at '.$event->result->ping.'ms.')
93+
->warning()
94+
->sendToDatabase($user);
95+
}
96+
}
97+
}

app/Listeners/SpeedtestCompletedListener.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44

55
use App\Events\SpeedtestCompleted;
66
use App\Mail\SpeedtestCompletedMail;
7-
use App\Models\User;
87
use App\Settings\GeneralSettings;
98
use App\Settings\NotificationSettings;
109
use App\Telegram\TelegramNotification;
11-
use Filament\Notifications\Notification;
1210
use Illuminate\Support\Facades\Mail;
1311
use Spatie\WebhookServer\WebhookCall;
1412

@@ -35,15 +33,6 @@ public function __construct()
3533
*/
3634
public function handle(SpeedtestCompleted $event): void
3735
{
38-
if ($this->notificationSettings->database_enabled && $this->notificationSettings->database_on_speedtest_run) {
39-
foreach (User::all() as $user) {
40-
Notification::make()
41-
->title('Speedtest completed')
42-
->success()
43-
->sendToDatabase($user);
44-
}
45-
}
46-
4736
if ($this->notificationSettings->mail_enabled) {
4837
if ($this->notificationSettings->mail_on_speedtest_run && count($this->notificationSettings->mail_recipients)) {
4938
foreach ($this->notificationSettings->mail_recipients as $recipient) {

app/Listeners/Threshold/AbsoluteListener.php

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44

55
use App\Events\SpeedtestCompleted;
66
use App\Mail\Threshold\AbsoluteMail;
7-
use App\Models\User;
87
use App\Settings\GeneralSettings;
98
use App\Settings\NotificationSettings;
109
use App\Settings\ThresholdSettings;
1110
use App\Telegram\TelegramNotification;
12-
use Filament\Notifications\Notification;
1311
use Illuminate\Contracts\Queue\ShouldQueue;
1412
use Illuminate\Support\Facades\Http;
1513
use Illuminate\Support\Facades\Log;
@@ -43,17 +41,10 @@ public function __construct()
4341
*/
4442
public function handle(SpeedtestCompleted $event): void
4543
{
46-
if ($this->thresholdSettings->absolute_enabled !== true) {
47-
Log::info('Absolute threshold notifications disabled.');
48-
44+
if (! $this->thresholdSettings->absolute_enabled) {
4945
return;
5046
}
5147

52-
// Database notification channel
53-
if ($this->notificationSettings->database_enabled == true && $this->notificationSettings->database_on_threshold_failure == true) {
54-
$this->databaseChannel($event);
55-
}
56-
5748
// Mail notification channel
5849
if ($this->notificationSettings->mail_enabled == true && $this->notificationSettings->mail_on_threshold_failure == true) {
5950
$this->mailChannel($event);
@@ -75,51 +66,6 @@ public function handle(SpeedtestCompleted $event): void
7566
}
7667
}
7768

78-
/**
79-
* Handle database notifications.
80-
*/
81-
protected function databaseChannel(SpeedtestCompleted $event): void
82-
{
83-
// Download threshold
84-
if ($this->thresholdSettings->absolute_download > 0) {
85-
if (absoluteDownloadThresholdFailed($this->thresholdSettings->absolute_download, $event->result->download)) {
86-
foreach (User::all() as $user) {
87-
Notification::make()
88-
->title('Threshold breached')
89-
->body('Speedtest #'.$event->result->id.' breached the download threshold of '.$this->thresholdSettings->absolute_download.'Mbps at '.toBits(convertSize($event->result->download), 2).'Mbps.')
90-
->warning()
91-
->sendToDatabase($user);
92-
}
93-
}
94-
}
95-
96-
// Upload threshold
97-
if ($this->thresholdSettings->absolute_upload > 0) {
98-
if (absoluteUploadThresholdFailed($this->thresholdSettings->absolute_upload, $event->result->upload)) {
99-
foreach (User::all() as $user) {
100-
Notification::make()
101-
->title('Threshold breached')
102-
->body('Speedtest #'.$event->result->id.' breached the upload threshold of '.$this->thresholdSettings->absolute_upload.'Mbps at '.toBits(convertSize($event->result->upload), 2).'Mbps.')
103-
->warning()
104-
->sendToDatabase($user);
105-
}
106-
}
107-
}
108-
109-
// Ping threshold
110-
if ($this->thresholdSettings->absolute_ping > 0) {
111-
if (absolutePingThresholdFailed($this->thresholdSettings->absolute_ping, $event->result->ping)) {
112-
foreach (User::all() as $user) {
113-
Notification::make()
114-
->title('Threshold breached')
115-
->body('Speedtest #'.$event->result->id.' breached the ping threshold of '.$this->thresholdSettings->absolute_ping.'ms at '.$event->result->ping.'ms.')
116-
->warning()
117-
->sendToDatabase($user);
118-
}
119-
}
120-
}
121-
}
122-
12369
/**
12470
* Handle database notifications.
12571
*/

app/Providers/EventServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use App\Events\SpeedtestStarted;
88
use App\Listeners\ClearApplicationCache;
99
use App\Listeners\Data\InfluxDb2Listener;
10+
use App\Listeners\Database\SendSpeedtestCompletedNotification as DatabaseSendSpeedtestCompletedNotification;
11+
use App\Listeners\Database\SendSpeedtestThresholdNotification as DatabaseSendSpeedtestThresholdNotification;
1012
use App\Listeners\SpeedtestCompletedListener;
1113
use App\Listeners\Threshold\AbsoluteListener;
1214
use Illuminate\Auth\Events\Registered;
@@ -38,6 +40,9 @@ class EventServiceProvider extends ServiceProvider
3840
InfluxDb2Listener::class,
3941

4042
// Notification listeners
43+
DatabaseSendSpeedtestCompletedNotification::class,
44+
DatabaseSendSpeedtestThresholdNotification::class,
45+
4146
AbsoluteListener::class,
4247
],
4348

0 commit comments

Comments
 (0)