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
55 changes: 55 additions & 0 deletions app/Listeners/Discord/SendSpeedtestCompletedNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Listeners\Discord;

use App\Events\SpeedtestCompleted;
use App\Helpers\Number;
use App\Settings\NotificationSettings;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Spatie\WebhookServer\WebhookCall;

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

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

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

if (! count($notificationSettings->discord_webhooks)) {
Log::warning('Discord urls not found, check Discord notification channel settings.');

return;
}

$payload = [
'content' => view('discord.speedtest-completed', [
'id' => $event->result->id,
'service' => Str::title($event->result->service),
'serverName' => $event->result->server_name,
'serverId' => $event->result->server_id,
'ping' => round($event->result->ping).' ms',
'download' => Number::toBitRate(bits: $event->result->download_bits, precision: 2),
'upload' => Number::toBitRate(bits: $event->result->upload_bits, precision: 2),
])->render(),
];

foreach ($notificationSettings->discord_webhooks as $url) {
WebhookCall::create()
->url($url['url'])
->payload($payload)
->doNotSign()
->dispatch();
}
}
}
122 changes: 122 additions & 0 deletions app/Listeners/Discord/SendSpeedtestThresholdNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace App\Listeners\Discord;

use App\Events\SpeedtestCompleted;
use App\Helpers\Number;
use App\Settings\NotificationSettings;
use App\Settings\ThresholdSettings;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Spatie\WebhookServer\WebhookCall;

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

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

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

if (! count($notificationSettings->discord_webhooks)) {
Log::warning('Discord urls not found, check Discord notification channel settings.');

return;
}

$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;
}

$payload = [
'content' => view('discord.speedtest-threshold', [
'id' => $event->result->id,
'service' => Str::title($event->result->service),
'serverName' => $event->result->server_name,
'serverId' => $event->result->server_id,
'metrics' => $failed,
])->render(),
];

foreach ($notificationSettings->discord_webhooks as $url) {
WebhookCall::create()
->url($url['url'])
->payload($payload)
->doNotSign()
->dispatch();
}
}

/**
* Build Discord 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.' Mbps',
'value' => Number::toBitRate(bits: $event->result->download_bits, precision: 2),
];
}

/**
* Build Discord 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.' Mbps',
'value' => Number::toBitRate(bits: $event->result->upload_bits, precision: 2),
];
}

/**
* Build Discord 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.' ms',
'value' => round($event->result->ping, 2).' ms',
];
}
}
56 changes: 0 additions & 56 deletions app/Listeners/SpeedtestCompletedListener.php

This file was deleted.

102 changes: 0 additions & 102 deletions app/Listeners/Threshold/AbsoluteListener.php

This file was deleted.

12 changes: 6 additions & 6 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
use App\Listeners\Data\InfluxDb2Listener;
use App\Listeners\Database\SendSpeedtestCompletedNotification as DatabaseSendSpeedtestCompletedNotification;
use App\Listeners\Database\SendSpeedtestThresholdNotification as DatabaseSendSpeedtestThresholdNotification;
use App\Listeners\Discord\SendSpeedtestCompletedNotification as DiscordSendSpeedtestCompletedNotification;
use App\Listeners\Discord\SendSpeedtestThresholdNotification as DiscordSendSpeedtestThresholdNotification;
use App\Listeners\Mail\SendSpeedtestCompletedNotification as MailSendSpeedtestCompletedNotification;
use App\Listeners\Mail\SendSpeedtestThresholdNotification as MailSendSpeedtestThresholdNotification;
use App\Listeners\SpeedtestCompletedListener;
use App\Listeners\Telegram\SendSpeedtestCompletedNotification as TelegramSendSpeedtestCompletedNotification;
use App\Listeners\Telegram\SendSpeedtestThresholdNotification as TelegramSendSpeedtestThresholdNotification;
use App\Listeners\Threshold\AbsoluteListener;
use App\Listeners\Webhook\SendSpeedtestCompletedNotification as WebhookSendSpeedtestCompletedNotification;
use App\Listeners\Webhook\SendSpeedtestThresholdNotification as WebhookSendSpeedtestThresholdNotification;
use Illuminate\Auth\Events\Registered;
Expand All @@ -40,15 +40,17 @@ class EventServiceProvider extends ServiceProvider
],

SpeedtestCompleted::class => [
SpeedtestCompletedListener::class,

// Data listeners
InfluxDb2Listener::class,

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

// Discord notification listeners
DiscordSendSpeedtestCompletedNotification::class,
DiscordSendSpeedtestThresholdNotification::class,

// Mail notification listeners
MailSendSpeedtestCompletedNotification::class,
MailSendSpeedtestThresholdNotification::class,
Expand All @@ -60,8 +62,6 @@ class EventServiceProvider extends ServiceProvider
// Webhook notification listeners
WebhookSendSpeedtestCompletedNotification::class,
WebhookSendSpeedtestThresholdNotification::class,

AbsoluteListener::class,
],

SpeedtestFailed::class => [
Expand Down
9 changes: 9 additions & 0 deletions resources/views/discord/speedtest-completed.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
**Speedtest Completed - #{{ $id }}**

A new speedtest was completed using **{{ $service }}**.

- **Server name:** {{ $serverName }}
- **Server ID:** {{ $serverId }}
- **Ping:** {{ $ping }}
- **Download:** {{ $download }}
- **Upload:** {{ $upload }}
Loading