Skip to content

Commit 55d65c8

Browse files
authored
delete speedtest results from the UI (alexjustesen#321)
1 parent 1fb4872 commit 55d65c8

File tree

5 files changed

+139
-1
lines changed

5 files changed

+139
-1
lines changed

app/Filament/Pages/DeleteData.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace App\Filament\Pages;
4+
5+
use App\Jobs\DeleteResultsData;
6+
use Filament\Notifications\Notification;
7+
use Filament\Pages\Actions\Action;
8+
use Filament\Pages\Page;
9+
10+
class DeleteData extends Page
11+
{
12+
protected static ?string $navigationGroup = 'System';
13+
14+
protected static ?string $navigationIcon = 'heroicon-o-trash';
15+
16+
protected static ?int $navigationSort = 2;
17+
18+
protected static ?string $slug = 'system/delete-data';
19+
20+
protected static string $view = 'filament.pages.delete-data';
21+
22+
protected ?string $maxContentWidth = '3xl';
23+
24+
public function getActions(): array
25+
{
26+
return [
27+
Action::make('delete')
28+
->color('danger')
29+
->icon('heroicon-o-trash')
30+
->action(fn () => $this->deleteData())
31+
->requiresConfirmation()
32+
->modalHeading('Confirmation')
33+
->modalSubheading('Are you really-really-really sure you want to do this?')
34+
->modalContent(view('filament.pages.modals.delete-results-data-confirmation'))
35+
->modalButton('Yes, I\'m sure'),
36+
];
37+
}
38+
39+
protected function deleteData()
40+
{
41+
DeleteResultsData::dispatch();
42+
43+
Notification::make()
44+
->title('Deleting speedtest results data')
45+
->body('The job has been added to the queue and will be completed shortly.')
46+
->warning()
47+
->send();
48+
}
49+
}

app/Filament/Resources/UserResource.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ class UserResource extends Resource
2424
{
2525
protected static ?string $model = User::class;
2626

27+
protected static ?string $navigationGroup = 'System';
28+
2729
protected static ?string $navigationIcon = 'heroicon-o-collection';
2830

29-
protected static ?string $navigationGroup = 'System';
31+
protected static ?int $navigationSort = 0;
32+
33+
protected static ?string $slug = 'system/users';
3034

3135
public static function form(Form $form): Form
3236
{

app/Jobs/DeleteResultsData.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use App\Models\Result;
6+
use App\Models\User;
7+
use Filament\Notifications\Notification;
8+
use Illuminate\Bus\Queueable;
9+
use Illuminate\Contracts\Queue\ShouldBeUnique;
10+
use Illuminate\Contracts\Queue\ShouldQueue;
11+
use Illuminate\Foundation\Bus\Dispatchable;
12+
use Illuminate\Queue\InteractsWithQueue;
13+
use Illuminate\Queue\SerializesModels;
14+
use Illuminate\Support\Facades\DB;
15+
16+
class DeleteResultsData implements ShouldQueue, ShouldBeUnique
17+
{
18+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
19+
20+
/**
21+
* Create a new job instance.
22+
*
23+
* @return void
24+
*/
25+
public function __construct()
26+
{
27+
}
28+
29+
/**
30+
* Execute the job.
31+
*
32+
* @return void
33+
*/
34+
public function handle()
35+
{
36+
$count = Result::count();
37+
38+
$recipient = User::first();
39+
40+
try {
41+
DB::table('results')->truncate();
42+
} catch (\Throwable $th) {
43+
Notification::make()
44+
->title('There was a problem deleting speedtest results data')
45+
->body('Check the logs.')
46+
->danger()
47+
->sendToDatabase($recipient);
48+
49+
return 0;
50+
}
51+
52+
Notification::make()
53+
->title('Speedtest results deleted')
54+
->body($count.' speedtest results were deleted from the database.')
55+
->success()
56+
->sendToDatabase($recipient);
57+
}
58+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<x-filament::page>
2+
<x-filament::card>
3+
<x-slot name="header">
4+
<h2 class="text-lg font-bold -tracking-tight">Results Data</h2>
5+
</x-slot>
6+
7+
<div class="space-y-4">
8+
<div>
9+
<p>Deleting results data will remove all speedtest results from the database, this cannot be undone.</p>
10+
</div>
11+
12+
<div>
13+
<p>The following <span class="underline">will not</span> be reset by clearing results data.</p>
14+
15+
<ul class="list-disc list-inside mt-2">
16+
<li>- Settings including general settings, notification settings and threshold settings.</li>
17+
<li>- Integrations and data sent to external data destinations like InfluxDB.</li>
18+
</ul>
19+
</div>
20+
</div>
21+
</x-filament::card>
22+
</x-filament::page>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div>
2+
<div class="space-y-4">
3+
<p>This will delete all speedtest results data from the database, this cannot be undone. You have been warned.</p>
4+
</div>
5+
</div>

0 commit comments

Comments
 (0)