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

namespace App\Listeners\Mail;

use App\Events\SpeedtestCompleted;
use App\Mail\SpeedtestCompletedMail;
use App\Settings\NotificationSettings;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;

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

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

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

if (! count($notificationSettings->mail_recipients)) {
Log::warning('Mail recipients not found, check mail notification channel settings.');

return;
}

foreach ($notificationSettings->mail_recipients as $recipient) {
Mail::to($recipient)
->send(new SpeedtestCompletedMail($event->result));
}
}
}
109 changes: 109 additions & 0 deletions app/Listeners/Mail/SendSpeedtestThresholdNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace App\Listeners\Mail;

use App\Events\SpeedtestCompleted;
use App\Helpers\Number;
use App\Mail\SpeedtestThresholdMail;
use App\Settings\NotificationSettings;
use App\Settings\ThresholdSettings;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;

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

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

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

if (! count($notificationSettings->mail_recipients) > 0) {
Log::warning('Mail recipients not found, check mail 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;
}

foreach ($notificationSettings->mail_recipients as $recipient) {
Mail::to($recipient)
->send(new SpeedtestThresholdMail($event->result, $failed));
}
}

/**
* Build mail 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 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.' Mbps',
'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.' ms',
'value' => round($event->result->ping, 2).' ms',
];
}
}
11 changes: 0 additions & 11 deletions app/Listeners/SpeedtestCompletedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
namespace App\Listeners;

use App\Events\SpeedtestCompleted;
use App\Mail\SpeedtestCompletedMail;
use App\Settings\GeneralSettings;
use App\Settings\NotificationSettings;
use App\Telegram\TelegramNotification;
use Illuminate\Support\Facades\Mail;
use Spatie\WebhookServer\WebhookCall;

class SpeedtestCompletedListener
Expand All @@ -33,15 +31,6 @@ public function __construct()
*/
public function handle(SpeedtestCompleted $event): void
{
if ($this->notificationSettings->mail_enabled) {
if ($this->notificationSettings->mail_on_speedtest_run && count($this->notificationSettings->mail_recipients)) {
foreach ($this->notificationSettings->mail_recipients as $recipient) {
Mail::to($recipient)
->send(new SpeedtestCompletedMail($event->result));
}
}
}

if ($this->notificationSettings->telegram_enabled) {
if ($this->notificationSettings->telegram_on_speedtest_run && count($this->notificationSettings->telegram_recipients)) {
foreach ($this->notificationSettings->telegram_recipients as $recipient) {
Expand Down
59 changes: 0 additions & 59 deletions app/Listeners/Threshold/AbsoluteListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
namespace App\Listeners\Threshold;

use App\Events\SpeedtestCompleted;
use App\Mail\Threshold\AbsoluteMail;
use App\Settings\GeneralSettings;
use App\Settings\NotificationSettings;
use App\Settings\ThresholdSettings;
use App\Telegram\TelegramNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Spatie\WebhookServer\WebhookCall;

class AbsoluteListener implements ShouldQueue
Expand Down Expand Up @@ -44,11 +42,6 @@ public function handle(SpeedtestCompleted $event): void
return;
}

// Mail notification channel
if ($this->notificationSettings->mail_enabled == true && $this->notificationSettings->mail_on_threshold_failure == true) {
$this->mailChannel($event);
}

// Telegram notification channel
if ($this->notificationSettings->telegram_enabled == true && $this->notificationSettings->telegram_on_threshold_failure == true) {
$this->telegramChannel($event);
Expand All @@ -60,58 +53,6 @@ public function handle(SpeedtestCompleted $event): void
}
}

/**
* Handle database notifications.
*/
protected function mailChannel(SpeedtestCompleted $event): void
{
$failedThresholds = [];

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

// 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.' Mbps',
'value' => toBits(convertSize($event->result->download), 2).' Mbps',
]);
}
}

// 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.' Mbps',
'value' => toBits(convertSize($event->result->upload), 2).'Mbps',
]);
}
}

// 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.' ms',
'value' => round($event->result->ping, 2).' ms',
]);
}
}

if (count($failedThresholds)) {
foreach ($this->notificationSettings->mail_recipients as $recipient) {
Mail::to($recipient)
->send(new AbsoluteMail($event->result, $failedThresholds));
}
}
}

/**
* Handle telegram notifications.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected function absoluteDownloadThreshold(SpeedtestCompleted $event, Threshol

return [
'name' => 'Download',
'threshold' => $thresholdSettings->absolute_download,
'threshold' => $thresholdSettings->absolute_download.' Mbps',
'value' => Number::toBitRate(bits: $event->result->download_bits, precision: 2),
];
}
Expand All @@ -95,7 +95,7 @@ protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdS

return [
'name' => 'Upload',
'threshold' => $thresholdSettings->absolute_upload,
'threshold' => $thresholdSettings->absolute_upload.' Mbps',
'value' => Number::toBitRate(bits: $event->result->upload_bits, precision: 2),
];
}
Expand All @@ -111,8 +111,8 @@ protected function absolutePingThreshold(SpeedtestCompleted $event, ThresholdSet

return [
'name' => 'Ping',
'threshold' => $thresholdSettings->absolute_ping,
'value' => round($event->result->ping, 2),
'threshold' => $thresholdSettings->absolute_ping.' ms',
'value' => round($event->result->ping, 2).' ms',
];
}
}
26 changes: 12 additions & 14 deletions app/Mail/SpeedtestCompletedMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@

namespace App\Mail;

use App\Helpers\Number;
use App\Models\Result;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Str;

class SpeedtestCompletedMail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;

public $result;

/**
* Create a new message instance.
*
* @return void
*/
public function __construct(Result $result)
{
$this->result = $result;
public function __construct(
public Result $result,
) {
}

/**
Expand All @@ -32,7 +32,7 @@ public function __construct(Result $result)
public function envelope(): Envelope
{
return new Envelope(
subject: 'Speedtest Result #'.$this->result->id.' - Completed',
subject: 'Speedtest Completed - #'.$this->result->id,
);
}

Expand All @@ -45,16 +45,14 @@ public function content(): Content
markdown: 'emails.speedtest-completed',
with: [
'id' => $this->result->id,
'service' => Str::title($this->result->service),
'serverName' => $this->result->server_name,
'serverId' => $this->result->server_id,
'ping' => round($this->result->ping, 2).' ms',
'download' => Number::toBitRate(bits: $this->result->download_bits, precision: 2),
'upload' => Number::toBitRate(bits: $this->result->upload_bits, precision: 2),
'url' => url('/admin/results'),
],
);
}

/**
* Get the attachments for the message.
*/
public function attachments(): array
{
return [];
}
}
Loading