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
59 changes: 59 additions & 0 deletions app/Filament/Pages/Settings/NotificationPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
namespace App\Filament\Pages\Settings;

use App\Forms\Components\TestDatabaseNotification;
use App\Forms\Components\TestMailNotification;
use App\Mail\Test;
use App\Settings\NotificationSettings;
use Closure;
use Filament\Forms\Components\Card;
use Filament\Forms\Components\Fieldset;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Components\View;
use Filament\Notifications\Notification;
use Filament\Pages\SettingsPage;
use Illuminate\Support\Facades\Mail;

class NotificationPage extends SettingsPage
{
Expand Down Expand Up @@ -69,6 +74,45 @@ protected function getFormSchema(): array
'default' => 1,
'md' => 2,
]),

Section::make('Mail')
->schema([
Toggle::make('mail_enabled')
->label('Enable mail notifications')
->reactive()
->columnSpan(2),
Grid::make([
'default' => 1,
])
->hidden(fn (Closure $get) => $get('mail_enabled') !== true)
->schema([
Fieldset::make('Triggers')
->schema([
Toggle::make('mail_on_speedtest_run')
->label('Notify on every speetest run')
->columnSpan(2),
Toggle::make('mail_on_threshold_failure')
->label('Notify on threshold failures')
->columnSpan(2),
]),
]),
Repeater::make('mail_recipients')
->label('Recipients')
->schema([
TextInput::make('email_address')
->email()
->required(fn (Closure $get) => $get('mail_enabled') == true),
])
->hidden(fn (Closure $get) => $get('mail_enabled') !== true)
->columnSpan(['md' => 2]),
TestMailNotification::make('test channel')
->hidden(fn (Closure $get) => $get('mail_enabled') !== true),
])
->compact()
->columns([
'default' => 1,
'md' => 2,
]),
])
->columnSpan([
'md' => 2,
Expand Down Expand Up @@ -103,4 +147,19 @@ public function sendTestDatabaseNotification()
->success()
->send();
}

public function sendTestMailNotification()
{
$notificationSettings = new (NotificationSettings::class);

foreach ($notificationSettings->mail_recipients as $recipient) {
Mail::to($recipient)
->send(new Test());
}

Notification::make()
->title('Test mail notification sent.')
->success()
->send();
}
}
10 changes: 10 additions & 0 deletions app/Forms/Components/TestMailNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Forms\Components;

use Filament\Forms\Components\Field;

class TestMailNotification extends Field
{
protected string $view = 'forms.components.test-mail-notification';
}
23 changes: 16 additions & 7 deletions app/Listeners/SpeedtestCompletedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace App\Listeners;

use App\Events\ResultCreated;
use App\Mail\SpeedtestCompletedMail;
use App\Settings\NotificationSettings;
use Filament\Notifications\Notification;
use Illuminate\Support\Facades\Mail;

class SpeedtestCompletedListener
{
Expand All @@ -28,15 +30,22 @@ public function __construct()
*/
public function handle(ResultCreated $event)
{
if (! $this->notificationSettings->database_enabled) {
return;
if ($this->notificationSettings->database_enabled) {
if ($this->notificationSettings->database_on_speedtest_run) {
Notification::make()
->title('Speedtest completed')
->success()
->sendToDatabase($event->user);
}
}

if ($this->notificationSettings->database_on_speedtest_run) {
Notification::make()
->title('Speedtest completed')
->success()
->sendToDatabase($event->user);
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));
}
}
}
}
}
49 changes: 0 additions & 49 deletions app/Listeners/Threshold/AbsoluteDownloadListener.php

This file was deleted.

153 changes: 153 additions & 0 deletions app/Listeners/Threshold/AbsoluteListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

namespace App\Listeners\Threshold;

use App\Events\ResultCreated;
use App\Mail\Threshold\AbsoluteMail;
use App\Settings\NotificationSettings;
use App\Settings\ThresholdSettings;
use Filament\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;

class AbsoluteListener implements ShouldQueue
{
public $notificationSettings;

public $thresholdSettings;

/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
$this->notificationSettings = new (NotificationSettings::class);

$this->thresholdSettings = new (ThresholdSettings::class);
}

/**
* Handle the event.
*
* @param \App\Events\ResultCreated $event
* @return void
*/
public function handle(ResultCreated $event)
{
if ($this->thresholdSettings->absolute_enabled !== true) {
Log::info('Absolute threshold notifications disabled.');

return;
}

// Database notification channel
if ($this->notificationSettings->database_enabled == true && $this->notificationSettings->database_on_threshold_failure == true) {
$this->databaseChannel($event);
}

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

/**
* Handle database notifications.
*
* @param \App\Events\ResultCreated $event
* @return void
*/
protected function databaseChannel(ResultCreated $event)
{
// Download threshold
if ($this->thresholdSettings->absolute_download > 0) {
if (absoluteDownloadThresholdFailed($this->thresholdSettings->absolute_download, $event->result->download)) {
Notification::make()
->title('Threshold breached')
->body('Speedtest #'.$event->result->id.' breached the download threshold of '.$this->thresholdSettings->absolute_download.'Mbps at '.formatBits(formatBytesToBits($event->result->download), 2, false).'Mbps.')
->warning()
->sendToDatabase($event->user);
}
}

// Upload threshold
if ($this->thresholdSettings->absolute_upload > 0) {
if (absoluteUploadThresholdFailed($this->thresholdSettings->absolute_upload, $event->result->upload)) {
Notification::make()
->title('Threshold breached')
->body('Speedtest #'.$event->result->id.' breached the upload threshold of '.$this->thresholdSettings->absolute_upload.'Mbps at '.formatBits(formatBytesToBits($event->result->upload), 2, false).'Mbps.')
->warning()
->sendToDatabase($event->user);
}
}

// Ping threshold
if ($this->thresholdSettings->absolute_ping > 0) {
if (absolutePingThresholdFailed($this->thresholdSettings->absolute_ping, $event->result->ping)) {
Notification::make()
->title('Threshold breached')
->body('Speedtest #'.$event->result->id.' breached the ping threshold of '.$this->thresholdSettings->absolute_ping.'ms at '.$event->result->ping.'ms.')
->warning()
->sendToDatabase($event->user);
}
}
}

/**
* Handle database notifications.
*
* @param \App\Events\ResultCreated $event
* @return void
*/
protected function mailChannel(ResultCreated $event)
{
$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' => formatBits(formatBytesToBits($event->result->download)).'ps',
]);
}
}

// 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' => formatBits(formatBytesToBits($event->result->upload)).'ps',
]);
}
}

// 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));
}
}
}
}
49 changes: 0 additions & 49 deletions app/Listeners/Threshold/AbsolutePingListener.php

This file was deleted.

Loading