Skip to content

Commit

Permalink
[Chore] Refactored webhook notifications to their own listeners (#1267)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjustesen authored Feb 29, 2024
1 parent 25e8b6e commit 5976e8f
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 92 deletions.
18 changes: 0 additions & 18 deletions app/Listeners/SpeedtestCompletedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,5 @@ public function handle(SpeedtestCompleted $event): void
}
}
}

if ($this->notificationSettings->webhook_enabled) {
if ($this->notificationSettings->webhook_on_speedtest_run && count($this->notificationSettings->webhook_urls)) {
foreach ($this->notificationSettings->webhook_urls as $url) {
WebhookCall::create()
->url($url['url'])
->payload([
'result_id' => $event->result->id,
'site_name' => $this->generalSettings->site_name,
'ping' => $event->result->ping,
'download' => $event->result->downloadBits,
'upload' => $event->result->uploadBits,
])
->doNotSign()
->dispatch();
}
}
}
}
}
73 changes: 0 additions & 73 deletions app/Listeners/Threshold/AbsoluteListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use App\Settings\ThresholdSettings;
use App\Telegram\TelegramNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Spatie\WebhookServer\WebhookCall;
Expand Down Expand Up @@ -59,11 +58,6 @@ public function handle(SpeedtestCompleted $event): void
if ($this->notificationSettings->discord_enabled == true && $this->notificationSettings->discord_on_threshold_failure == true) {
$this->discordChannel($event);
}

// Webhook notification channel
if ($this->notificationSettings->webhook_enabled == true && $this->notificationSettings->webhook_on_threshold_failure == true) {
$this->webhookChannel($event);
}
}

/**
Expand Down Expand Up @@ -230,71 +224,4 @@ protected function discordChannel(SpeedtestCompleted $event): void
}
}
}

/**
* Handle webhook notifications.
*
* TODO: refactor
*/
protected function webhookChannel(SpeedtestCompleted $event): void
{
$failedThresholds = [];

if (! count($this->notificationSettings->webhook_urls) > 0) {
Log::info('Skipping sending webhook notification, no urls.');
}

// Download threshold
if ($this->thresholdSettings->absolute_download > 0) {
if (absoluteDownloadThresholdFailed($this->thresholdSettings->absolute_download, $event->result->download)) {
array_push($failedThresholds, [
'name' => 'Download',
'threshold' => $this->thresholdSettings->absolute_download,
'value' => toBits(convertSize($event->result->download), 2),
]);
}
}

// Upload threshold
if ($this->thresholdSettings->absolute_upload > 0) {
if (absoluteUploadThresholdFailed($this->thresholdSettings->absolute_upload, $event->result->upload)) {
array_push($failedThresholds, [
'name' => 'Upload',
'threshold' => $this->thresholdSettings->absolute_upload,
'value' => toBits(convertSize($event->result->upload), 2),
]);
}
}

// Ping threshold
if ($this->thresholdSettings->absolute_ping > 0) {
if (absolutePingThresholdFailed($this->thresholdSettings->absolute_ping, $event->result->ping)) {
array_push($failedThresholds, [
'name' => 'Ping',
'threshold' => $this->thresholdSettings->absolute_ping,
'value' => round($event->result->ping, 2),
]);
}
}

if (count($failedThresholds)) {
foreach ($this->notificationSettings->webhook_urls as $url) {
Http::post($url['url'], [
'result_id' => $event->result->id,
'site_name' => $this->generalSettings->site_name,
'metrics' => $failedThresholds,
]);

WebhookCall::create()
->url($url['url'])
->payload([
'result_id' => $event->result->id,
'site_name' => $this->generalSettings->site_name,
'metrics' => $failedThresholds,
])
->doNotSign()
->dispatch();
}
}
}
}
50 changes: 50 additions & 0 deletions app/Listeners/Webhook/SendSpeedtestCompletedNotification.php
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 app/Listeners/Webhook/SendSpeedtestThresholdNotification.php
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),
];
}
}
8 changes: 7 additions & 1 deletion app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use App\Listeners\Database\SendSpeedtestThresholdNotification as DatabaseSendSpeedtestThresholdNotification;
use App\Listeners\SpeedtestCompletedListener;
use App\Listeners\Threshold\AbsoluteListener;
use App\Listeners\Webhook\SendSpeedtestCompletedNotification as WebhookSendSpeedtestCompletedNotification;
use App\Listeners\Webhook\SendSpeedtestThresholdNotification as WebhookSendSpeedtestThresholdNotification;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
Expand Down Expand Up @@ -39,10 +41,14 @@ class EventServiceProvider extends ServiceProvider
// Data listeners
InfluxDb2Listener::class,

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

// Webhook notification listeners
WebhookSendSpeedtestCompletedNotification::class,
WebhookSendSpeedtestThresholdNotification::class,

AbsoluteListener::class,
],

Expand Down

0 comments on commit 5976e8f

Please sign in to comment.