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
1 change: 1 addition & 0 deletions app/Http/Controllers/Api/V1/SpeedtestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function __invoke(Request $request)
}

$result = RunSpeedtestAction::run(
scheduled: true,
serverId: $request->input('server_id'),
dispatchedBy: $request->user()->id,
);
Expand Down
51 changes: 8 additions & 43 deletions app/Listeners/ProcessCompletedSpeedtest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ public function handle(SpeedtestCompleted $event): void
{
$result = $event->result;

$result->loadMissing(['dispatchedBy']);
if ($result->healthy === false) {
return;
}

// Don't send notifications for unscheduled speedtests.
if ($result->unscheduled) {
return;
}

$this->notifyAppriseChannels($result);
$this->notifyDatabaseChannels($result);
$this->notifyDispatchingUser($result);
$this->notifyMailChannels($result);
$this->notifyWebhookChannels($result);
}
Expand All @@ -45,11 +51,6 @@ public function handle(SpeedtestCompleted $event): void
*/
private function notifyAppriseChannels(Result $result): void
{
// Don't send Apprise notification if dispatched by a user or test is unhealthy.
if (filled($result->dispatched_by) || $result->healthy === false) {
return;
}

// Check if Apprise notifications are enabled.
if (! $this->notificationSettings->apprise_enabled || ! $this->notificationSettings->apprise_on_speedtest_run) {
return;
Expand Down Expand Up @@ -97,11 +98,6 @@ private function notifyAppriseChannels(Result $result): void
*/
private function notifyDatabaseChannels(Result $result): void
{
// Don't send database notification if dispatched by a user or test is unhealthy.
if (filled($result->dispatched_by) || $result->healthy === false) {
return;
}

// Check if database notifications are enabled.
if (! $this->notificationSettings->database_enabled || ! $this->notificationSettings->database_on_speedtest_run) {
return;
Expand All @@ -120,37 +116,11 @@ private function notifyDatabaseChannels(Result $result): void
}
}

/**
* Notify the user who dispatched the speedtest.
*/
private function notifyDispatchingUser(Result $result): void
{
if (empty($result->dispatched_by) || ! $result->healthy) {
return;
}

$result->dispatchedBy->notify(
FilamentNotification::make()
->title(__('results.speedtest_completed'))
->actions([
Action::make('view')
->label(__('general.view'))
->url(route('filament.admin.resources.results.index')),
])
->success()
->toDatabase(),
);
}

/**
* Notify mail channels.
*/
private function notifyMailChannels(Result $result): void
{
if (filled($result->dispatched_by) || $result->healthy === false) {
return;
}

if (! $this->notificationSettings->mail_enabled || ! $this->notificationSettings->mail_on_speedtest_run) {
return;
}
Expand All @@ -172,11 +142,6 @@ private function notifyMailChannels(Result $result): void
*/
private function notifyWebhookChannels(Result $result): void
{
// Don't send webhook if dispatched by a user or test is unhealthy.
if (filled($result->dispatched_by) || $result->healthy === false) {
return;
}

// Check if webhook notifications are enabled.
if (! $this->notificationSettings->webhook_enabled || ! $this->notificationSettings->webhook_on_speedtest_run) {
return;
Expand Down
35 changes: 4 additions & 31 deletions app/Listeners/ProcessFailedSpeedtest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use App\Events\SpeedtestFailed;
use App\Models\Result;
use Filament\Actions\Action;
use Filament\Notifications\Notification;

class ProcessFailedSpeedtest
{
Expand All @@ -16,44 +14,19 @@ public function handle(SpeedtestFailed $event): void
{
$result = $event->result;

$result->loadMissing(['dispatchedBy']);
// Don't send notifications for unscheduled speedtests.
if ($result->unscheduled) {
return;
}

// $this->notifyAppriseChannels($result);
$this->notifyDispatchingUser($result);
}

/**
* Notify Apprise channels.
*/
private function notifyAppriseChannels(Result $result): void
{
// Don't send Apprise notification if dispatched by a user or test is unhealthy.
if (filled($result->dispatched_by) || ! $result->healthy) {
return;
}

//
}

/**
* Notify the user who dispatched the speedtest.
*/
private function notifyDispatchingUser(Result $result): void
{
if (empty($result->dispatched_by)) {
return;
}

$result->dispatchedBy->notify(
Notification::make()
->title(__('results.speedtest_failed'))
->actions([
Action::make('view')
->label(__('general.view'))
->url(route('filament.admin.resources.results.index')),
])
->warning()
->toDatabase(),
);
}
}
48 changes: 4 additions & 44 deletions app/Listeners/ProcessUnhealthySpeedtest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ public function handle(SpeedtestBenchmarkFailed $event): void
{
$result = $event->result;

$result->loadMissing(['dispatchedBy']);
// Don't send notifications for unscheduled speedtests.
if ($result->unscheduled) {
return;
}

$this->notifyAppriseChannels($result);
$this->notifyDatabaseChannels($result);
$this->notifyDispatchingUser($result);
$this->notifyMailChannels($result);
$this->notifyWebhookChannels($result);
}
Expand All @@ -47,11 +49,6 @@ public function handle(SpeedtestBenchmarkFailed $event): void
*/
private function notifyAppriseChannels(Result $result): void
{
// Don't send Apprise notification if dispatched by a user.
if (filled($result->dispatched_by)) {
return;
}

if (! $this->notificationSettings->apprise_enabled || ! $this->notificationSettings->apprise_on_threshold_failure) {
return;
}
Expand Down Expand Up @@ -132,11 +129,6 @@ private function formatMetricValue(string $metric, Result $result): string
*/
private function notifyDatabaseChannels(Result $result): void
{
// Don't send database notification if dispatched by a user.
if (filled($result->dispatched_by)) {
return;
}

// Check if database notifications are enabled.
if (! $this->notificationSettings->database_enabled || ! $this->notificationSettings->database_on_threshold_failure) {
return;
Expand All @@ -155,38 +147,11 @@ private function notifyDatabaseChannels(Result $result): void
}
}

/**
* Notify the user who dispatched the speedtest.
*/
private function notifyDispatchingUser(Result $result): void
{
if (empty($result->dispatched_by)) {
return;
}

$result->dispatchedBy->notify(
FilamentNotification::make()
->title(__('results.speedtest_benchmark_failed'))
->actions([
Action::make('view')
->label(__('general.view'))
->url(route('filament.admin.resources.results.index')),
])
->warning()
->toDatabase(),
);
}

/**
* Notify mail channels.
*/
private function notifyMailChannels(Result $result): void
{
// Don't send mail if dispatched by a user.
if (filled($result->dispatched_by)) {
return;
}

// Check if mail notifications are enabled.
if (! $this->notificationSettings->mail_enabled || ! $this->notificationSettings->mail_on_threshold_failure) {
return;
Expand All @@ -210,11 +175,6 @@ private function notifyMailChannels(Result $result): void
*/
private function notifyWebhookChannels(Result $result): void
{
// Don't send webhook if dispatched by a user.
if (filled($result->dispatched_by)) {
return;
}

// Check if webhook notifications are enabled.
if (! $this->notificationSettings->webhook_enabled || ! $this->notificationSettings->webhook_on_threshold_failure) {
return;
Expand Down
104 changes: 104 additions & 0 deletions app/Listeners/UserNotificationSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace App\Listeners;

use App\Events\SpeedtestBenchmarkFailed;
use App\Events\SpeedtestCompleted;
use App\Events\SpeedtestFailed;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Illuminate\Events\Dispatcher;

class UserNotificationSubscriber
{
/**
* Handle the event.
*/
public function handleCompleted(SpeedtestCompleted $event): void
{
$result = $event->result;

if (empty($result->dispatched_by)) {
return;
}

$result->loadMissing('dispatchedBy');

Notification::make()
->title(__('results.speedtest_completed'))
->actions([
Action::make('view')
->label(__('general.view'))
->url(route('filament.admin.resources.results.index')),
])
->success()
->sendToDatabase($result->dispatchedBy);
}

/**
* Handle the event.
*/
public function handleBenchmarkFailed(SpeedtestBenchmarkFailed $event): void
{
$result = $event->result;

if (empty($result->dispatched_by)) {
return;
}

// Don't send notifications for unscheduled speedtests.
if ($result->unscheduled) {
return;
}

$result->loadMissing('dispatchedBy');

Notification::make()
->title(__('results.speedtest_benchmark_failed'))
->actions([
Action::make('view')
->label(__('general.view'))
->url(route('filament.admin.resources.results.index')),
])
->warning()
->sendToDatabase($result->dispatchedBy);
}

/**
* Handle the event.
*/
public function handleFailed(SpeedtestFailed $event): void
{
$result = $event->result;

if (empty($result->dispatched_by)) {
return;
}

$result->loadMissing('dispatchedBy');

Notification::make()
->title(__('results.speedtest_failed'))
->actions([
Action::make('view')
->label(__('general.view'))
->url(route('filament.admin.resources.results.index')),
])
->warning()
->sendToDatabase($result->dispatchedBy);
}

/**
* Register the listeners for the subscriber.
*
* @return array<string, string>
*/
public function subscribe(Dispatcher $events): array
{
return [
SpeedtestCompleted::class => 'handleCompleted',
SpeedtestBenchmarkFailed::class => 'handleBenchmarkFailed',
SpeedtestFailed::class => 'handleFailed',
];
}
}
11 changes: 11 additions & 0 deletions app/Models/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Enums\ResultStatus;
use App\Models\Traits\ResultDataAttributes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Prunable;
Expand Down Expand Up @@ -54,4 +55,14 @@ public function dispatchedBy(): BelongsTo
{
return $this->belongsTo(User::class, 'dispatched_by');
}

/**
* Determine if the result was unscheduled.
*/
protected function unscheduled(): Attribute
{
return Attribute::make(
get: fn (): bool => ! $this->scheduled,
);
}
}