Skip to content

Commit fcf9fa2

Browse files
authored
[Chore] Refactored executing Ookla speedtests and added new events (alexjustesen#1264)
1 parent 92262ed commit fcf9fa2

File tree

16 files changed

+280
-280
lines changed

16 files changed

+280
-280
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Actions\Speedtests;
4+
5+
use App\Enums\ResultStatus;
6+
use App\Events\SpeedtestStarted;
7+
use App\Jobs\Speedtests\ExecuteOoklaSpeedtest;
8+
use App\Models\Result;
9+
use Lorisleiva\Actions\Concerns\AsAction;
10+
11+
class RunOoklaSpeedtest
12+
{
13+
use AsAction;
14+
15+
public function handle(?int $serverId = null, bool $scheduled = false): void
16+
{
17+
$result = Result::create([
18+
'service' => 'ookla',
19+
'status' => ResultStatus::Started,
20+
'scheduled' => $scheduled,
21+
]);
22+
23+
SpeedtestStarted::dispatch($result);
24+
25+
ExecuteOoklaSpeedtest::dispatch(result: $result, serverId: $serverId);
26+
}
27+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App\Actions\Speedtests;
4+
5+
use App\Settings\GeneralSettings;
6+
use Cron\CronExpression;
7+
use Illuminate\Support\Arr;
8+
use Lorisleiva\Actions\Concerns\AsAction;
9+
10+
class RunScheduledSpeedtests
11+
{
12+
use AsAction;
13+
14+
public function handle()
15+
{
16+
$settings = new GeneralSettings();
17+
18+
/**
19+
* Ookla service
20+
*/
21+
$cronExpression = new CronExpression($settings->speedtest_schedule);
22+
23+
if ($cronExpression->isDue(now()->timezone($settings->timezone ?? 'UTC'))) {
24+
$serverId = null;
25+
26+
if (is_array($settings->speedtest_server) && count($settings->speedtest_server)) {
27+
$serverId = Arr::random($settings->speedtest_server);
28+
}
29+
30+
RunOoklaSpeedtest::run(serverId: $serverId, scheduled: true);
31+
}
32+
}
33+
}

app/Console/Commands/RunOoklaSpeedtest.php

Lines changed: 0 additions & 78 deletions
This file was deleted.

app/Console/Kernel.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
namespace App\Console;
44

5-
use App\Console\Commands\RunOoklaSpeedtest;
5+
use App\Actions\Speedtests\RunScheduledSpeedtests;
66
use App\Console\Commands\SystemMaintenance;
77
use App\Console\Commands\VersionChecker;
88
use App\Models\Result;
99
use App\Settings\GeneralSettings;
10-
use Cron\CronExpression;
1110
use Illuminate\Console\Scheduling\Schedule;
1211
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
1312

@@ -44,17 +43,14 @@ protected function schedule(Schedule $schedule): void
4443
->timezone($settings->timezone ?? 'UTC');
4544

4645
/**
47-
* Check if an Ookla Speedtest needs to run.
46+
* Action to run scheduled speedtests.
4847
*/
49-
$schedule->command(RunOoklaSpeedtest::class, ['--scheduled'])->everyMinute()
50-
->timezone($settings->timezone ?? 'UTC')
48+
$schedule->call(function () {
49+
RunScheduledSpeedtests::run();
50+
})
51+
->everyMinute()
5152
->when(function () use ($settings) {
52-
if (blank($settings->speedtest_schedule)) {
53-
return false;
54-
}
55-
56-
return (new CronExpression($settings->speedtest_schedule))
57-
->isDue(now()->timezone($settings->timezone ?? 'UTC'));
53+
return ! blank($settings->speedtest_schedule);
5854
});
5955
}
6056

app/Events/ResultCreated.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

app/Events/SpeedtestCompleted.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\Models\Result;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Foundation\Events\Dispatchable;
8+
use Illuminate\Queue\SerializesModels;
9+
10+
class SpeedtestCompleted
11+
{
12+
use Dispatchable, InteractsWithSockets, SerializesModels;
13+
14+
/**
15+
* Create a new event instance.
16+
*/
17+
public function __construct(
18+
public Result $result,
19+
) {
20+
}
21+
}

app/Events/SpeedtestFailed.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\Models\Result;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Foundation\Events\Dispatchable;
8+
use Illuminate\Queue\SerializesModels;
9+
10+
class SpeedtestFailed
11+
{
12+
use Dispatchable, InteractsWithSockets, SerializesModels;
13+
14+
/**
15+
* Create a new event instance.
16+
*/
17+
public function __construct(
18+
public Result $result,
19+
) {
20+
}
21+
}

app/Events/SpeedtestStarted.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\Models\Result;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Foundation\Events\Dispatchable;
8+
use Illuminate\Queue\SerializesModels;
9+
10+
class SpeedtestStarted
11+
{
12+
use Dispatchable, InteractsWithSockets, SerializesModels;
13+
14+
/**
15+
* Create a new event instance.
16+
*/
17+
public function __construct(
18+
public Result $result,
19+
) {
20+
}
21+
}

app/Filament/Pages/Dashboard.php

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,55 @@
22

33
namespace App\Filament\Pages;
44

5-
use App\Console\Commands\RunOoklaSpeedtest;
5+
use App\Actions\Speedtests\RunOoklaSpeedtest;
66
use App\Filament\Widgets\RecentDownloadChartWidget;
77
use App\Filament\Widgets\RecentJitterChartWidget;
88
use App\Filament\Widgets\RecentPingChartWidget;
99
use App\Filament\Widgets\RecentUploadChartWidget;
1010
use App\Filament\Widgets\StatsOverviewWidget;
1111
use App\Settings\GeneralSettings;
1212
use Filament\Actions\Action;
13+
use Filament\Actions\ActionGroup;
1314
use Filament\Notifications\Notification;
1415
use Filament\Pages\Dashboard as BasePage;
15-
use Illuminate\Support\Facades\Artisan;
16+
use Filament\Support\Enums\ActionSize;
17+
use Filament\Support\Enums\IconPosition;
1618

1719
class Dashboard extends BasePage
1820
{
19-
public bool $publicDashboard = false;
20-
21-
protected static ?string $pollingInterval = null;
22-
2321
protected static ?string $navigationIcon = 'heroicon-o-chart-bar';
2422

2523
protected static ?int $navigationSort = 1;
2624

2725
protected static string $view = 'filament.pages.dashboard';
2826

29-
public function mount()
30-
{
31-
$settings = new GeneralSettings();
32-
33-
$this->publicDashboard = $settings->public_dashboard_enabled;
34-
}
35-
3627
protected function getHeaderActions(): array
3728
{
3829
return [
3930
Action::make('home')
4031
->label('Public Dashboard')
4132
->color('gray')
42-
->hidden(! $this->publicDashboard)
33+
->hidden(fn (GeneralSettings $settings): bool => ! $settings->public_dashboard_enabled)
4334
->url('/'),
44-
Action::make('speedtest')
45-
->label('Queue Speedtest')
35+
ActionGroup::make([
36+
Action::make('ookla speedtest')
37+
->action(function () {
38+
RunOoklaSpeedtest::run();
39+
40+
Notification::make()
41+
->title('Ookla speedtest started')
42+
->success()
43+
->send();
44+
}),
45+
])
46+
->button()
4647
->color('primary')
47-
->action('queueSpeedtest')
48-
->hidden(fn (): bool => ! auth()->user()->is_admin && ! auth()->user()->is_user),
48+
->dropdownPlacement('bottom-end')
49+
->label('Run Speedtest')
50+
->icon('heroicon-o-rocket-launch')
51+
->iconPosition(IconPosition::After)
52+
->hidden(! auth()->user()->is_admin)
53+
->size(ActionSize::Small),
4954
];
5055
}
5156

@@ -59,24 +64,4 @@ protected function getHeaderWidgets(): array
5964
RecentJitterChartWidget::make(),
6065
];
6166
}
62-
63-
public function queueSpeedtest(): void
64-
{
65-
try {
66-
Artisan::call(RunOoklaSpeedtest::class);
67-
} catch (\Throwable $th) {
68-
Notification::make()
69-
->title('Manual speedtest failed!')
70-
->body('The starting a manual speedtest failed, check the logs.')
71-
->warning()
72-
->sendToDatabase(auth()->user());
73-
74-
return;
75-
}
76-
77-
Notification::make()
78-
->title('Speedtest added to the queue')
79-
->success()
80-
->send();
81-
}
8267
}

0 commit comments

Comments
 (0)