From 3462110e8a8ecd6da014ef19990a2cf1d203d627 Mon Sep 17 00:00:00 2001 From: svenvg93 Date: Wed, 16 Oct 2024 21:39:05 +0200 Subject: [PATCH 1/7] Add pop up --- app/Filament/Pages/Dashboard.php | 81 ++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 25 deletions(-) diff --git a/app/Filament/Pages/Dashboard.php b/app/Filament/Pages/Dashboard.php index 3737cf9bf..754ac4dba 100644 --- a/app/Filament/Pages/Dashboard.php +++ b/app/Filament/Pages/Dashboard.php @@ -13,11 +13,11 @@ use Carbon\Carbon; use Cron\CronExpression; use Filament\Actions\Action; -use Filament\Actions\ActionGroup; +use Filament\Forms; use Filament\Notifications\Notification; use Filament\Pages\Dashboard as BasePage; use Filament\Support\Enums\IconPosition; -use Illuminate\Support\Arr; +use Illuminate\Support\Facades\Http; class Dashboard extends BasePage { @@ -32,7 +32,6 @@ public function getSubheading(): ?string } $cronExpression = new CronExpression(config('speedtest.schedule')); - $nextRunDate = Carbon::parse($cronExpression->getNextRunDate(timeZone: config('app.display_timezone')))->format(config('app.datetime_format')); return 'Next speedtest at: '.$nextRunDate; @@ -48,30 +47,28 @@ protected function getHeaderActions(): array ->color('gray') ->hidden(fn (): bool => ! config('speedtest.public_dashboard')) ->url(shouldOpenInNewTab: true, url: '/'), - ActionGroup::make([ - Action::make('ookla speedtest') - ->action(function () { - $servers = array_filter( - explode(',', config('speedtest.servers')) - ); - - $serverId = null; - - if (count($servers)) { - $serverId = Arr::random($servers); - } - - RunOoklaSpeedtest::run(serverId: $serverId); - - Notification::make() - ->title('Ookla speedtest started') - ->success() - ->send(); - }), - ]) + Action::make('ookla_speedtest') + ->form([ + Forms\Components\Select::make('server_id') + ->label('Select Server') + ->options(fn (callable $get) => $this->getServerSearchOptions($get('server_search'))) + ->searchable() + ->required(), + ]) + ->action(function (array $data) { + $serverId = $data['server_id']; + RunOoklaSpeedtest::run(serverId: $serverId); + + Notification::make() + ->title('Ookla speedtest started') + ->success() + ->send(); + }) + ->modalHeading('Run Speedtest') + ->modalButton('Run Speedtest') + ->modalWidth('lg') ->button() ->color('primary') - ->dropdownPlacement('bottom-end') ->label('Run Speedtest') ->icon('heroicon-o-rocket-launch') ->iconPosition(IconPosition::Before) @@ -79,6 +76,40 @@ protected function getHeaderActions(): array ]; } + protected function getServerSearchOptions(?string $search = ''): array + { + // Make an API request to fetch the servers + $response = Http::get('https://www.speedtest.net/api/js/servers', [ + 'engine' => 'js', + 'search' => $search ?? '', + 'https_functional' => true, + 'limit' => 20, + ]); + + // Check if the response failed + if ($response->failed()) { + return ['' => 'Error retrieving Speedtest servers']; + } + + // Get the JSON response + $servers = $response->json(); + + // Ensure that the response is an array + if (! is_array($servers)) { + return ['' => 'Invalid response format']; + } + + // Map the server options, ensuring each item is valid + return collect($servers)->mapWithKeys(function ($item) { + if (is_array($item) && isset($item['id'], $item['name'], $item['sponsor'])) { + return [$item['id'] => "{$item['name']} ({$item['sponsor']})"]; + } + + return []; + + })->toArray(); + } + protected function getHeaderWidgets(): array { return [ From 771f1f66e05a48f0e1bfc0a3721032acd9d4c0a6 Mon Sep 17 00:00:00 2001 From: svenvg93 Date: Wed, 16 Oct 2024 22:00:42 +0200 Subject: [PATCH 2/7] White line --- app/Filament/Pages/Dashboard.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Filament/Pages/Dashboard.php b/app/Filament/Pages/Dashboard.php index 754ac4dba..cafca5ff2 100644 --- a/app/Filament/Pages/Dashboard.php +++ b/app/Filament/Pages/Dashboard.php @@ -32,6 +32,7 @@ public function getSubheading(): ?string } $cronExpression = new CronExpression(config('speedtest.schedule')); + $nextRunDate = Carbon::parse($cronExpression->getNextRunDate(timeZone: config('app.display_timezone')))->format(config('app.datetime_format')); return 'Next speedtest at: '.$nextRunDate; From 1d6b7630853a8f27dff5dd5c6ff1a6c8cc59d932 Mon Sep 17 00:00:00 2001 From: svenvg93 Date: Thu, 17 Oct 2024 11:24:52 +0200 Subject: [PATCH 3/7] swap name and sponsor, add id --- app/Filament/Pages/Dashboard.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Filament/Pages/Dashboard.php b/app/Filament/Pages/Dashboard.php index cafca5ff2..f9f9f2b16 100644 --- a/app/Filament/Pages/Dashboard.php +++ b/app/Filament/Pages/Dashboard.php @@ -103,7 +103,7 @@ protected function getServerSearchOptions(?string $search = ''): array // Map the server options, ensuring each item is valid return collect($servers)->mapWithKeys(function ($item) { if (is_array($item) && isset($item['id'], $item['name'], $item['sponsor'])) { - return [$item['id'] => "{$item['name']} ({$item['sponsor']})"]; + return [$item['id'] => "{$item['sponsor']} ({$item['name']}, {$item['id']})"]; } return []; From 11196f5f28b631e89a68959fdfe9b4bbef0c328a Mon Sep 17 00:00:00 2001 From: Sven van Ginkel Date: Thu, 21 Nov 2024 21:06:00 +0100 Subject: [PATCH 4/7] Use GetOoklaSpeedtestServers --- app/Actions/GetOoklaSpeedtestServers.php | 2 +- app/Filament/Pages/Dashboard.php | 38 ++---------------------- 2 files changed, 3 insertions(+), 37 deletions(-) diff --git a/app/Actions/GetOoklaSpeedtestServers.php b/app/Actions/GetOoklaSpeedtestServers.php index cf5099ef3..1d483e653 100644 --- a/app/Actions/GetOoklaSpeedtestServers.php +++ b/app/Actions/GetOoklaSpeedtestServers.php @@ -34,7 +34,7 @@ public function handle(): array return $response->mapWithKeys(function (array $item, int $key) { return [ - $item['id'] => $item['id'].': '.$item['name'].' ('.$item['sponsor'].')', + $item['id'] => $item['sponsor'].' ('.$item['name'].', '.$item['id'].')', ]; })->toArray(); } diff --git a/app/Filament/Pages/Dashboard.php b/app/Filament/Pages/Dashboard.php index f9f9f2b16..07e788619 100644 --- a/app/Filament/Pages/Dashboard.php +++ b/app/Filament/Pages/Dashboard.php @@ -2,6 +2,7 @@ namespace App\Filament\Pages; +use App\Actions\GetOoklaSpeedtestServers; use App\Actions\Speedtests\RunOoklaSpeedtest; use App\Filament\Widgets\RecentDownloadChartWidget; use App\Filament\Widgets\RecentDownloadLatencyChartWidget; @@ -17,7 +18,6 @@ use Filament\Notifications\Notification; use Filament\Pages\Dashboard as BasePage; use Filament\Support\Enums\IconPosition; -use Illuminate\Support\Facades\Http; class Dashboard extends BasePage { @@ -52,7 +52,7 @@ protected function getHeaderActions(): array ->form([ Forms\Components\Select::make('server_id') ->label('Select Server') - ->options(fn (callable $get) => $this->getServerSearchOptions($get('server_search'))) + ->options(fn (callable $get) => app(GetOoklaSpeedtestServers::class)->handle($get('server_search'))) ->searchable() ->required(), ]) @@ -77,40 +77,6 @@ protected function getHeaderActions(): array ]; } - protected function getServerSearchOptions(?string $search = ''): array - { - // Make an API request to fetch the servers - $response = Http::get('https://www.speedtest.net/api/js/servers', [ - 'engine' => 'js', - 'search' => $search ?? '', - 'https_functional' => true, - 'limit' => 20, - ]); - - // Check if the response failed - if ($response->failed()) { - return ['' => 'Error retrieving Speedtest servers']; - } - - // Get the JSON response - $servers = $response->json(); - - // Ensure that the response is an array - if (! is_array($servers)) { - return ['' => 'Invalid response format']; - } - - // Map the server options, ensuring each item is valid - return collect($servers)->mapWithKeys(function ($item) { - if (is_array($item) && isset($item['id'], $item['name'], $item['sponsor'])) { - return [$item['id'] => "{$item['sponsor']} ({$item['name']}, {$item['id']})"]; - } - - return []; - - })->toArray(); - } - protected function getHeaderWidgets(): array { return [ From 48d3b02958d4ef90a9262f567a4e1215bc5383d7 Mon Sep 17 00:00:00 2001 From: Sven van Ginkel Date: Thu, 21 Nov 2024 21:27:20 +0100 Subject: [PATCH 5/7] Make it Server selection optional --- app/Filament/Pages/Dashboard.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/Filament/Pages/Dashboard.php b/app/Filament/Pages/Dashboard.php index 07e788619..50235a14f 100644 --- a/app/Filament/Pages/Dashboard.php +++ b/app/Filament/Pages/Dashboard.php @@ -52,16 +52,16 @@ protected function getHeaderActions(): array ->form([ Forms\Components\Select::make('server_id') ->label('Select Server') + ->helperText('Leave blank to run the speedtest without specifying a server.') ->options(fn (callable $get) => app(GetOoklaSpeedtestServers::class)->handle($get('server_search'))) - ->searchable() - ->required(), + ->searchable(), ]) ->action(function (array $data) { - $serverId = $data['server_id']; + $serverId = $data['server_id'] ?? null; RunOoklaSpeedtest::run(serverId: $serverId); Notification::make() - ->title('Ookla speedtest started') + ->title('Speedtest started') ->success() ->send(); }) From 74744fb4e4d9c751247d8603c1667f014558ec58 Mon Sep 17 00:00:00 2001 From: Alex Justesen Date: Fri, 22 Nov 2024 11:26:28 -0500 Subject: [PATCH 6/7] Update Dashboard.php --- app/Filament/Pages/Dashboard.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Filament/Pages/Dashboard.php b/app/Filament/Pages/Dashboard.php index 581a639cc..fd627ba13 100644 --- a/app/Filament/Pages/Dashboard.php +++ b/app/Filament/Pages/Dashboard.php @@ -60,7 +60,7 @@ protected function getHeaderActions(): array ]) ->action(function (array $data) { $serverId = $data['server_id'] ?? null; - + StartSpeedtest::run(serverId: $serverId); Notification::make() From 8640396328232fd0beb98805a773a5a8b2c4e0a5 Mon Sep 17 00:00:00 2001 From: Alex Justesen Date: Fri, 22 Nov 2024 11:29:38 -0500 Subject: [PATCH 7/7] removed spaces --- app/Filament/Pages/Dashboard.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Filament/Pages/Dashboard.php b/app/Filament/Pages/Dashboard.php index fd627ba13..d6b80383f 100644 --- a/app/Filament/Pages/Dashboard.php +++ b/app/Filament/Pages/Dashboard.php @@ -60,7 +60,7 @@ protected function getHeaderActions(): array ]) ->action(function (array $data) { $serverId = $data['server_id'] ?? null; - + StartSpeedtest::run(serverId: $serverId); Notification::make()