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
10 changes: 10 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Console\Commands\RunOoklaSpeedtest;
use App\Console\Commands\SystemMaintenance;
use App\Console\Commands\VersionChecker;
use App\Models\Result;
use App\Settings\GeneralSettings;
use Cron\CronExpression;
use Illuminate\Console\Scheduling\Schedule;
Expand All @@ -19,6 +20,15 @@ protected function schedule(Schedule $schedule): void
{
$settings = new GeneralSettings();

/**
* Checks if Result model records should be pruned.
*/
if ($settings->prune_results_older_than > 0) {
$schedule->command('model:prune', [
'--model' => [Result::class],
])->daily();
}

/**
* Perform system maintenance weekly on Sunday morning,
* start off the week nice and fresh.
Expand Down
10 changes: 9 additions & 1 deletion app/Filament/Pages/Settings/GeneralPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ public function form(Form $form): Form
->hint(new HtmlString('&#x1f517;<a href="https://crontab.cronhub.io/" target="_blank" rel="nofollow">Cron Generator</a>'))
->nullable()
->columnSpan(1),
Forms\Components\TextInput::make('prune_results_older_than')
->helperText('Set to zero to disable pruning.')
->suffix('days')
->numeric()
->required()
->minValue(0)
->maxValue(9999)
->columnSpan(1),
Forms\Components\Select::make('speedtest_server')
->label('Speedtest servers')
->helperText('Leave empty to let the system pick the best server.')
Expand All @@ -102,7 +110,7 @@ public function form(Form $form): Form
->options(GetOoklaSpeedtestServers::run())
->getSearchResultsUsing(fn (string $search): array => $this->getServerSearchOptions($search))
->getOptionLabelsUsing(fn (array $values): array => $this->getServerLabels($values))
->columnSpan('full'),
->columnSpanFull(),
])
->compact()
->columns([
Expand Down
15 changes: 14 additions & 1 deletion app/Models/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

use App\Enums\ResultStatus;
use App\Events\ResultCreated;
use App\Settings\GeneralSettings;
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;
use Illuminate\Support\Arr;

class Result extends Model
{
use HasFactory;
use HasFactory, Prunable;

/**
* The attributes that aren't mass assignable.
Expand Down Expand Up @@ -76,6 +79,16 @@ public function formatForInfluxDB2()
];
}

/**
* Get the prunable model query.
*/
public function prunable(): Builder
{
$settings = new GeneralSettings();

return static::where('created_at', '<=', now()->subDays($settings->prune_results_older_than));
}

/**
* Get the result's download in bits.
*/
Expand Down
2 changes: 2 additions & 0 deletions app/Settings/GeneralSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class GeneralSettings extends Settings
{
public bool $auth_enabled;

public int $prune_results_older_than;

public ?string $speedtest_schedule;

/** @var string[] */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Spatie\LaravelSettings\Migrations\SettingsMigration;

return new class extends SettingsMigration
{
public function up(): void
{
$this->migrator->add('general.prune_results_older_than', 0);
}
};