Skip to content

Commit 5976e8f

Browse files
authored
[Chore] Refactored webhook notifications to their own listeners (alexjustesen#1267)
1 parent 25e8b6e commit 5976e8f

File tree

5 files changed

+175
-92
lines changed

5 files changed

+175
-92
lines changed

app/Listeners/SpeedtestCompletedListener.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,5 @@ public function handle(SpeedtestCompleted $event): void
8787
}
8888
}
8989
}
90-
91-
if ($this->notificationSettings->webhook_enabled) {
92-
if ($this->notificationSettings->webhook_on_speedtest_run && count($this->notificationSettings->webhook_urls)) {
93-
foreach ($this->notificationSettings->webhook_urls as $url) {
94-
WebhookCall::create()
95-
->url($url['url'])
96-
->payload([
97-
'result_id' => $event->result->id,
98-
'site_name' => $this->generalSettings->site_name,
99-
'ping' => $event->result->ping,
100-
'download' => $event->result->downloadBits,
101-
'upload' => $event->result->uploadBits,
102-
])
103-
->doNotSign()
104-
->dispatch();
105-
}
106-
}
107-
}
10890
}
10991
}

app/Listeners/Threshold/AbsoluteListener.php

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use App\Settings\ThresholdSettings;
1010
use App\Telegram\TelegramNotification;
1111
use Illuminate\Contracts\Queue\ShouldQueue;
12-
use Illuminate\Support\Facades\Http;
1312
use Illuminate\Support\Facades\Log;
1413
use Illuminate\Support\Facades\Mail;
1514
use Spatie\WebhookServer\WebhookCall;
@@ -59,11 +58,6 @@ public function handle(SpeedtestCompleted $event): void
5958
if ($this->notificationSettings->discord_enabled == true && $this->notificationSettings->discord_on_threshold_failure == true) {
6059
$this->discordChannel($event);
6160
}
62-
63-
// Webhook notification channel
64-
if ($this->notificationSettings->webhook_enabled == true && $this->notificationSettings->webhook_on_threshold_failure == true) {
65-
$this->webhookChannel($event);
66-
}
6761
}
6862

6963
/**
@@ -230,71 +224,4 @@ protected function discordChannel(SpeedtestCompleted $event): void
230224
}
231225
}
232226
}
233-
234-
/**
235-
* Handle webhook notifications.
236-
*
237-
* TODO: refactor
238-
*/
239-
protected function webhookChannel(SpeedtestCompleted $event): void
240-
{
241-
$failedThresholds = [];
242-
243-
if (! count($this->notificationSettings->webhook_urls) > 0) {
244-
Log::info('Skipping sending webhook notification, no urls.');
245-
}
246-
247-
// Download threshold
248-
if ($this->thresholdSettings->absolute_download > 0) {
249-
if (absoluteDownloadThresholdFailed($this->thresholdSettings->absolute_download, $event->result->download)) {
250-
array_push($failedThresholds, [
251-
'name' => 'Download',
252-
'threshold' => $this->thresholdSettings->absolute_download,
253-
'value' => toBits(convertSize($event->result->download), 2),
254-
]);
255-
}
256-
}
257-
258-
// Upload threshold
259-
if ($this->thresholdSettings->absolute_upload > 0) {
260-
if (absoluteUploadThresholdFailed($this->thresholdSettings->absolute_upload, $event->result->upload)) {
261-
array_push($failedThresholds, [
262-
'name' => 'Upload',
263-
'threshold' => $this->thresholdSettings->absolute_upload,
264-
'value' => toBits(convertSize($event->result->upload), 2),
265-
]);
266-
}
267-
}
268-
269-
// Ping threshold
270-
if ($this->thresholdSettings->absolute_ping > 0) {
271-
if (absolutePingThresholdFailed($this->thresholdSettings->absolute_ping, $event->result->ping)) {
272-
array_push($failedThresholds, [
273-
'name' => 'Ping',
274-
'threshold' => $this->thresholdSettings->absolute_ping,
275-
'value' => round($event->result->ping, 2),
276-
]);
277-
}
278-
}
279-
280-
if (count($failedThresholds)) {
281-
foreach ($this->notificationSettings->webhook_urls as $url) {
282-
Http::post($url['url'], [
283-
'result_id' => $event->result->id,
284-
'site_name' => $this->generalSettings->site_name,
285-
'metrics' => $failedThresholds,
286-
]);
287-
288-
WebhookCall::create()
289-
->url($url['url'])
290-
->payload([
291-
'result_id' => $event->result->id,
292-
'site_name' => $this->generalSettings->site_name,
293-
'metrics' => $failedThresholds,
294-
])
295-
->doNotSign()
296-
->dispatch();
297-
}
298-
}
299-
}
300227
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace App\Listeners\Webhook;
4+
5+
use App\Events\SpeedtestCompleted;
6+
use App\Settings\GeneralSettings;
7+
use App\Settings\NotificationSettings;
8+
use Illuminate\Support\Facades\Log;
9+
use Spatie\WebhookServer\WebhookCall;
10+
11+
class SendSpeedtestCompletedNotification
12+
{
13+
/**
14+
* Handle the event.
15+
*/
16+
public function handle(SpeedtestCompleted $event): void
17+
{
18+
$generalSettings = new GeneralSettings();
19+
20+
$notificationSettings = new NotificationSettings();
21+
22+
if (! $notificationSettings->webhook_enabled) {
23+
return;
24+
}
25+
26+
if (! $notificationSettings->webhook_on_speedtest_run) {
27+
return;
28+
}
29+
30+
if (! count($notificationSettings->webhook_urls)) {
31+
Log::warning('Webhook urls not found, check webhook notification channel settings.');
32+
33+
return;
34+
}
35+
36+
foreach ($notificationSettings->webhook_urls as $url) {
37+
WebhookCall::create()
38+
->url($url['url'])
39+
->payload([
40+
'result_id' => $event->result->id,
41+
'site_name' => $generalSettings->site_name,
42+
'ping' => $event->result->ping,
43+
'download' => $event->result->downloadBits,
44+
'upload' => $event->result->uploadBits,
45+
])
46+
->doNotSign()
47+
->dispatch();
48+
}
49+
}
50+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
namespace App\Listeners\Webhook;
4+
5+
use App\Events\SpeedtestCompleted;
6+
use App\Helpers\Number;
7+
use App\Settings\GeneralSettings;
8+
use App\Settings\NotificationSettings;
9+
use App\Settings\ThresholdSettings;
10+
use Illuminate\Support\Facades\Log;
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->webhook_enabled) {
23+
return;
24+
}
25+
26+
if (! $notificationSettings->webhook_on_threshold_failure) {
27+
return;
28+
}
29+
30+
if (! count($notificationSettings->webhook_urls)) {
31+
Log::warning('Webhook urls not found, check webhook notification channel settings.');
32+
33+
return;
34+
}
35+
36+
$generalSettings = new GeneralSettings();
37+
38+
$thresholdSettings = new ThresholdSettings();
39+
40+
$failed = [];
41+
42+
if ($thresholdSettings->absolute_download > 0) {
43+
array_push($failed, $this->absoluteDownloadThreshold(event: $event, thresholdSettings: $thresholdSettings));
44+
}
45+
46+
if ($thresholdSettings->absolute_upload > 0) {
47+
array_push($failed, $this->absoluteUploadThreshold(event: $event, thresholdSettings: $thresholdSettings));
48+
}
49+
50+
if ($thresholdSettings->absolute_ping > 0) {
51+
array_push($failed, $this->absolutePingThreshold(event: $event, thresholdSettings: $thresholdSettings));
52+
}
53+
54+
if (! count($failed)) {
55+
return;
56+
}
57+
58+
foreach ($notificationSettings->webhook_urls as $url) {
59+
WebhookCall::create()
60+
->url($url['url'])
61+
->payload([
62+
'result_id' => $event->result->id,
63+
'site_name' => $generalSettings->site_name,
64+
'metrics' => $failed,
65+
])
66+
->doNotSign()
67+
->dispatch();
68+
}
69+
}
70+
71+
/**
72+
* Build webhook notification if absolute download threshold is breached.
73+
*/
74+
protected function absoluteDownloadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array
75+
{
76+
if (! absoluteDownloadThresholdFailed($thresholdSettings->absolute_download, $event->result->download)) {
77+
return [];
78+
}
79+
80+
return [
81+
'name' => 'Download',
82+
'threshold' => $thresholdSettings->absolute_download,
83+
'value' => Number::toBitRate(bits: $event->result->download_bits, precision: 2),
84+
];
85+
}
86+
87+
/**
88+
* Build webhook notification if absolute upload threshold is breached.
89+
*/
90+
protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array
91+
{
92+
if (! absoluteUploadThresholdFailed($thresholdSettings->absolute_upload, $event->result->upload)) {
93+
return [];
94+
}
95+
96+
return [
97+
'name' => 'Upload',
98+
'threshold' => $thresholdSettings->absolute_upload,
99+
'value' => Number::toBitRate(bits: $event->result->upload_bits, precision: 2),
100+
];
101+
}
102+
103+
/**
104+
* Build webhook notification if absolute ping threshold is breached.
105+
*/
106+
protected function absolutePingThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array
107+
{
108+
if (! absolutePingThresholdFailed($thresholdSettings->absolute_ping, $event->result->ping)) {
109+
return [];
110+
}
111+
112+
return [
113+
'name' => 'Ping',
114+
'threshold' => $thresholdSettings->absolute_ping,
115+
'value' => round($event->result->ping, 2),
116+
];
117+
}
118+
}

app/Providers/EventServiceProvider.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use App\Listeners\Database\SendSpeedtestThresholdNotification as DatabaseSendSpeedtestThresholdNotification;
1212
use App\Listeners\SpeedtestCompletedListener;
1313
use App\Listeners\Threshold\AbsoluteListener;
14+
use App\Listeners\Webhook\SendSpeedtestCompletedNotification as WebhookSendSpeedtestCompletedNotification;
15+
use App\Listeners\Webhook\SendSpeedtestThresholdNotification as WebhookSendSpeedtestThresholdNotification;
1416
use Illuminate\Auth\Events\Registered;
1517
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
1618
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
@@ -39,10 +41,14 @@ class EventServiceProvider extends ServiceProvider
3941
// Data listeners
4042
InfluxDb2Listener::class,
4143

42-
// Notification listeners
44+
// Database notification listeners
4345
DatabaseSendSpeedtestCompletedNotification::class,
4446
DatabaseSendSpeedtestThresholdNotification::class,
4547

48+
// Webhook notification listeners
49+
WebhookSendSpeedtestCompletedNotification::class,
50+
WebhookSendSpeedtestThresholdNotification::class,
51+
4652
AbsoluteListener::class,
4753
],
4854

0 commit comments

Comments
 (0)