-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Chore] Refactored webhook notifications to their own listeners (#1267)
- Loading branch information
1 parent
25e8b6e
commit 5976e8f
Showing
5 changed files
with
175 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
app/Listeners/Webhook/SendSpeedtestCompletedNotification.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace App\Listeners\Webhook; | ||
|
||
use App\Events\SpeedtestCompleted; | ||
use App\Settings\GeneralSettings; | ||
use App\Settings\NotificationSettings; | ||
use Illuminate\Support\Facades\Log; | ||
use Spatie\WebhookServer\WebhookCall; | ||
|
||
class SendSpeedtestCompletedNotification | ||
{ | ||
/** | ||
* Handle the event. | ||
*/ | ||
public function handle(SpeedtestCompleted $event): void | ||
{ | ||
$generalSettings = new GeneralSettings(); | ||
|
||
$notificationSettings = new NotificationSettings(); | ||
|
||
if (! $notificationSettings->webhook_enabled) { | ||
return; | ||
} | ||
|
||
if (! $notificationSettings->webhook_on_speedtest_run) { | ||
return; | ||
} | ||
|
||
if (! count($notificationSettings->webhook_urls)) { | ||
Log::warning('Webhook urls not found, check webhook notification channel settings.'); | ||
|
||
return; | ||
} | ||
|
||
foreach ($notificationSettings->webhook_urls as $url) { | ||
WebhookCall::create() | ||
->url($url['url']) | ||
->payload([ | ||
'result_id' => $event->result->id, | ||
'site_name' => $generalSettings->site_name, | ||
'ping' => $event->result->ping, | ||
'download' => $event->result->downloadBits, | ||
'upload' => $event->result->uploadBits, | ||
]) | ||
->doNotSign() | ||
->dispatch(); | ||
} | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
app/Listeners/Webhook/SendSpeedtestThresholdNotification.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
namespace App\Listeners\Webhook; | ||
|
||
use App\Events\SpeedtestCompleted; | ||
use App\Helpers\Number; | ||
use App\Settings\GeneralSettings; | ||
use App\Settings\NotificationSettings; | ||
use App\Settings\ThresholdSettings; | ||
use Illuminate\Support\Facades\Log; | ||
use Spatie\WebhookServer\WebhookCall; | ||
|
||
class SendSpeedtestThresholdNotification | ||
{ | ||
/** | ||
* Handle the event. | ||
*/ | ||
public function handle(SpeedtestCompleted $event): void | ||
{ | ||
$notificationSettings = new NotificationSettings(); | ||
|
||
if (! $notificationSettings->webhook_enabled) { | ||
return; | ||
} | ||
|
||
if (! $notificationSettings->webhook_on_threshold_failure) { | ||
return; | ||
} | ||
|
||
if (! count($notificationSettings->webhook_urls)) { | ||
Log::warning('Webhook urls not found, check webhook notification channel settings.'); | ||
|
||
return; | ||
} | ||
|
||
$generalSettings = new GeneralSettings(); | ||
|
||
$thresholdSettings = new ThresholdSettings(); | ||
|
||
$failed = []; | ||
|
||
if ($thresholdSettings->absolute_download > 0) { | ||
array_push($failed, $this->absoluteDownloadThreshold(event: $event, thresholdSettings: $thresholdSettings)); | ||
} | ||
|
||
if ($thresholdSettings->absolute_upload > 0) { | ||
array_push($failed, $this->absoluteUploadThreshold(event: $event, thresholdSettings: $thresholdSettings)); | ||
} | ||
|
||
if ($thresholdSettings->absolute_ping > 0) { | ||
array_push($failed, $this->absolutePingThreshold(event: $event, thresholdSettings: $thresholdSettings)); | ||
} | ||
|
||
if (! count($failed)) { | ||
return; | ||
} | ||
|
||
foreach ($notificationSettings->webhook_urls as $url) { | ||
WebhookCall::create() | ||
->url($url['url']) | ||
->payload([ | ||
'result_id' => $event->result->id, | ||
'site_name' => $generalSettings->site_name, | ||
'metrics' => $failed, | ||
]) | ||
->doNotSign() | ||
->dispatch(); | ||
} | ||
} | ||
|
||
/** | ||
* Build webhook notification if absolute download threshold is breached. | ||
*/ | ||
protected function absoluteDownloadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array | ||
{ | ||
if (! absoluteDownloadThresholdFailed($thresholdSettings->absolute_download, $event->result->download)) { | ||
return []; | ||
} | ||
|
||
return [ | ||
'name' => 'Download', | ||
'threshold' => $thresholdSettings->absolute_download, | ||
'value' => Number::toBitRate(bits: $event->result->download_bits, precision: 2), | ||
]; | ||
} | ||
|
||
/** | ||
* Build webhook notification if absolute upload threshold is breached. | ||
*/ | ||
protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array | ||
{ | ||
if (! absoluteUploadThresholdFailed($thresholdSettings->absolute_upload, $event->result->upload)) { | ||
return []; | ||
} | ||
|
||
return [ | ||
'name' => 'Upload', | ||
'threshold' => $thresholdSettings->absolute_upload, | ||
'value' => Number::toBitRate(bits: $event->result->upload_bits, precision: 2), | ||
]; | ||
} | ||
|
||
/** | ||
* Build webhook notification if absolute ping threshold is breached. | ||
*/ | ||
protected function absolutePingThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array | ||
{ | ||
if (! absolutePingThresholdFailed($thresholdSettings->absolute_ping, $event->result->ping)) { | ||
return []; | ||
} | ||
|
||
return [ | ||
'name' => 'Ping', | ||
'threshold' => $thresholdSettings->absolute_ping, | ||
'value' => round($event->result->ping, 2), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters