Skip to content

Commit 62f13fb

Browse files
authored
Mark speedtest as scheduled when triggered using the API (#2597)
Co-authored-by: Alex Justesen <[email protected]>
1 parent b068b3a commit 62f13fb

File tree

6 files changed

+132
-118
lines changed

6 files changed

+132
-118
lines changed

app/Http/Controllers/Api/V1/SpeedtestController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function __invoke(Request $request)
3737
}
3838

3939
$result = RunSpeedtestAction::run(
40+
scheduled: true,
4041
serverId: $request->input('server_id'),
4142
dispatchedBy: $request->user()->id,
4243
);

app/Listeners/ProcessCompletedSpeedtest.php

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,17 @@ public function handle(SpeedtestCompleted $event): void
3131
{
3232
$result = $event->result;
3333

34-
$result->loadMissing(['dispatchedBy']);
34+
if ($result->healthy === false) {
35+
return;
36+
}
37+
38+
// Don't send notifications for unscheduled speedtests.
39+
if ($result->unscheduled) {
40+
return;
41+
}
3542

3643
$this->notifyAppriseChannels($result);
3744
$this->notifyDatabaseChannels($result);
38-
$this->notifyDispatchingUser($result);
3945
$this->notifyMailChannels($result);
4046
$this->notifyWebhookChannels($result);
4147
}
@@ -45,11 +51,6 @@ public function handle(SpeedtestCompleted $event): void
4551
*/
4652
private function notifyAppriseChannels(Result $result): void
4753
{
48-
// Don't send Apprise notification if dispatched by a user or test is unhealthy.
49-
if (filled($result->dispatched_by) || $result->healthy === false) {
50-
return;
51-
}
52-
5354
// Check if Apprise notifications are enabled.
5455
if (! $this->notificationSettings->apprise_enabled || ! $this->notificationSettings->apprise_on_speedtest_run) {
5556
return;
@@ -97,11 +98,6 @@ private function notifyAppriseChannels(Result $result): void
9798
*/
9899
private function notifyDatabaseChannels(Result $result): void
99100
{
100-
// Don't send database notification if dispatched by a user or test is unhealthy.
101-
if (filled($result->dispatched_by) || $result->healthy === false) {
102-
return;
103-
}
104-
105101
// Check if database notifications are enabled.
106102
if (! $this->notificationSettings->database_enabled || ! $this->notificationSettings->database_on_speedtest_run) {
107103
return;
@@ -120,37 +116,11 @@ private function notifyDatabaseChannels(Result $result): void
120116
}
121117
}
122118

123-
/**
124-
* Notify the user who dispatched the speedtest.
125-
*/
126-
private function notifyDispatchingUser(Result $result): void
127-
{
128-
if (empty($result->dispatched_by) || ! $result->healthy) {
129-
return;
130-
}
131-
132-
$result->dispatchedBy->notify(
133-
FilamentNotification::make()
134-
->title(__('results.speedtest_completed'))
135-
->actions([
136-
Action::make('view')
137-
->label(__('general.view'))
138-
->url(route('filament.admin.resources.results.index')),
139-
])
140-
->success()
141-
->toDatabase(),
142-
);
143-
}
144-
145119
/**
146120
* Notify mail channels.
147121
*/
148122
private function notifyMailChannels(Result $result): void
149123
{
150-
if (filled($result->dispatched_by) || $result->healthy === false) {
151-
return;
152-
}
153-
154124
if (! $this->notificationSettings->mail_enabled || ! $this->notificationSettings->mail_on_speedtest_run) {
155125
return;
156126
}
@@ -172,11 +142,6 @@ private function notifyMailChannels(Result $result): void
172142
*/
173143
private function notifyWebhookChannels(Result $result): void
174144
{
175-
// Don't send webhook if dispatched by a user or test is unhealthy.
176-
if (filled($result->dispatched_by) || $result->healthy === false) {
177-
return;
178-
}
179-
180145
// Check if webhook notifications are enabled.
181146
if (! $this->notificationSettings->webhook_enabled || ! $this->notificationSettings->webhook_on_speedtest_run) {
182147
return;

app/Listeners/ProcessFailedSpeedtest.php

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
use App\Events\SpeedtestFailed;
66
use App\Models\Result;
7-
use Filament\Actions\Action;
8-
use Filament\Notifications\Notification;
97

108
class ProcessFailedSpeedtest
119
{
@@ -16,44 +14,19 @@ public function handle(SpeedtestFailed $event): void
1614
{
1715
$result = $event->result;
1816

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

2122
// $this->notifyAppriseChannels($result);
22-
$this->notifyDispatchingUser($result);
2323
}
2424

2525
/**
2626
* Notify Apprise channels.
2727
*/
2828
private function notifyAppriseChannels(Result $result): void
2929
{
30-
// Don't send Apprise notification if dispatched by a user or test is unhealthy.
31-
if (filled($result->dispatched_by) || ! $result->healthy) {
32-
return;
33-
}
34-
3530
//
3631
}
37-
38-
/**
39-
* Notify the user who dispatched the speedtest.
40-
*/
41-
private function notifyDispatchingUser(Result $result): void
42-
{
43-
if (empty($result->dispatched_by)) {
44-
return;
45-
}
46-
47-
$result->dispatchedBy->notify(
48-
Notification::make()
49-
->title(__('results.speedtest_failed'))
50-
->actions([
51-
Action::make('view')
52-
->label(__('general.view'))
53-
->url(route('filament.admin.resources.results.index')),
54-
])
55-
->warning()
56-
->toDatabase(),
57-
);
58-
}
5932
}

app/Listeners/ProcessUnhealthySpeedtest.php

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ public function handle(SpeedtestBenchmarkFailed $event): void
3333
{
3434
$result = $event->result;
3535

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

3841
$this->notifyAppriseChannels($result);
3942
$this->notifyDatabaseChannels($result);
40-
$this->notifyDispatchingUser($result);
4143
$this->notifyMailChannels($result);
4244
$this->notifyWebhookChannels($result);
4345
}
@@ -47,11 +49,6 @@ public function handle(SpeedtestBenchmarkFailed $event): void
4749
*/
4850
private function notifyAppriseChannels(Result $result): void
4951
{
50-
// Don't send Apprise notification if dispatched by a user.
51-
if (filled($result->dispatched_by)) {
52-
return;
53-
}
54-
5552
if (! $this->notificationSettings->apprise_enabled || ! $this->notificationSettings->apprise_on_threshold_failure) {
5653
return;
5754
}
@@ -132,11 +129,6 @@ private function formatMetricValue(string $metric, Result $result): string
132129
*/
133130
private function notifyDatabaseChannels(Result $result): void
134131
{
135-
// Don't send database notification if dispatched by a user.
136-
if (filled($result->dispatched_by)) {
137-
return;
138-
}
139-
140132
// Check if database notifications are enabled.
141133
if (! $this->notificationSettings->database_enabled || ! $this->notificationSettings->database_on_threshold_failure) {
142134
return;
@@ -155,38 +147,11 @@ private function notifyDatabaseChannels(Result $result): void
155147
}
156148
}
157149

158-
/**
159-
* Notify the user who dispatched the speedtest.
160-
*/
161-
private function notifyDispatchingUser(Result $result): void
162-
{
163-
if (empty($result->dispatched_by)) {
164-
return;
165-
}
166-
167-
$result->dispatchedBy->notify(
168-
FilamentNotification::make()
169-
->title(__('results.speedtest_benchmark_failed'))
170-
->actions([
171-
Action::make('view')
172-
->label(__('general.view'))
173-
->url(route('filament.admin.resources.results.index')),
174-
])
175-
->warning()
176-
->toDatabase(),
177-
);
178-
}
179-
180150
/**
181151
* Notify mail channels.
182152
*/
183153
private function notifyMailChannels(Result $result): void
184154
{
185-
// Don't send mail if dispatched by a user.
186-
if (filled($result->dispatched_by)) {
187-
return;
188-
}
189-
190155
// Check if mail notifications are enabled.
191156
if (! $this->notificationSettings->mail_enabled || ! $this->notificationSettings->mail_on_threshold_failure) {
192157
return;
@@ -210,11 +175,6 @@ private function notifyMailChannels(Result $result): void
210175
*/
211176
private function notifyWebhookChannels(Result $result): void
212177
{
213-
// Don't send webhook if dispatched by a user.
214-
if (filled($result->dispatched_by)) {
215-
return;
216-
}
217-
218178
// Check if webhook notifications are enabled.
219179
if (! $this->notificationSettings->webhook_enabled || ! $this->notificationSettings->webhook_on_threshold_failure) {
220180
return;
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace App\Listeners;
4+
5+
use App\Events\SpeedtestBenchmarkFailed;
6+
use App\Events\SpeedtestCompleted;
7+
use App\Events\SpeedtestFailed;
8+
use Filament\Actions\Action;
9+
use Filament\Notifications\Notification;
10+
use Illuminate\Events\Dispatcher;
11+
12+
class UserNotificationSubscriber
13+
{
14+
/**
15+
* Handle the event.
16+
*/
17+
public function handleCompleted(SpeedtestCompleted $event): void
18+
{
19+
$result = $event->result;
20+
21+
if (empty($result->dispatched_by)) {
22+
return;
23+
}
24+
25+
$result->loadMissing('dispatchedBy');
26+
27+
Notification::make()
28+
->title(__('results.speedtest_completed'))
29+
->actions([
30+
Action::make('view')
31+
->label(__('general.view'))
32+
->url(route('filament.admin.resources.results.index')),
33+
])
34+
->success()
35+
->sendToDatabase($result->dispatchedBy);
36+
}
37+
38+
/**
39+
* Handle the event.
40+
*/
41+
public function handleBenchmarkFailed(SpeedtestBenchmarkFailed $event): void
42+
{
43+
$result = $event->result;
44+
45+
if (empty($result->dispatched_by)) {
46+
return;
47+
}
48+
49+
// Don't send notifications for unscheduled speedtests.
50+
if ($result->unscheduled) {
51+
return;
52+
}
53+
54+
$result->loadMissing('dispatchedBy');
55+
56+
Notification::make()
57+
->title(__('results.speedtest_benchmark_failed'))
58+
->actions([
59+
Action::make('view')
60+
->label(__('general.view'))
61+
->url(route('filament.admin.resources.results.index')),
62+
])
63+
->warning()
64+
->sendToDatabase($result->dispatchedBy);
65+
}
66+
67+
/**
68+
* Handle the event.
69+
*/
70+
public function handleFailed(SpeedtestFailed $event): void
71+
{
72+
$result = $event->result;
73+
74+
if (empty($result->dispatched_by)) {
75+
return;
76+
}
77+
78+
$result->loadMissing('dispatchedBy');
79+
80+
Notification::make()
81+
->title(__('results.speedtest_failed'))
82+
->actions([
83+
Action::make('view')
84+
->label(__('general.view'))
85+
->url(route('filament.admin.resources.results.index')),
86+
])
87+
->warning()
88+
->sendToDatabase($result->dispatchedBy);
89+
}
90+
91+
/**
92+
* Register the listeners for the subscriber.
93+
*
94+
* @return array<string, string>
95+
*/
96+
public function subscribe(Dispatcher $events): array
97+
{
98+
return [
99+
SpeedtestCompleted::class => 'handleCompleted',
100+
SpeedtestBenchmarkFailed::class => 'handleBenchmarkFailed',
101+
SpeedtestFailed::class => 'handleFailed',
102+
];
103+
}
104+
}

app/Models/Result.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Enums\ResultStatus;
77
use App\Models\Traits\ResultDataAttributes;
88
use Illuminate\Database\Eloquent\Builder;
9+
use Illuminate\Database\Eloquent\Casts\Attribute;
910
use Illuminate\Database\Eloquent\Factories\HasFactory;
1011
use Illuminate\Database\Eloquent\Model;
1112
use Illuminate\Database\Eloquent\Prunable;
@@ -54,4 +55,14 @@ public function dispatchedBy(): BelongsTo
5455
{
5556
return $this->belongsTo(User::class, 'dispatched_by');
5657
}
58+
59+
/**
60+
* Determine if the result was unscheduled.
61+
*/
62+
protected function unscheduled(): Attribute
63+
{
64+
return Attribute::make(
65+
get: fn (): bool => ! $this->scheduled,
66+
);
67+
}
5768
}

0 commit comments

Comments
 (0)