diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index c69e12152..e0b0e3aa2 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -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; @@ -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. diff --git a/app/Filament/Pages/Settings/GeneralPage.php b/app/Filament/Pages/Settings/GeneralPage.php index 5874b6a48..3ca1bae59 100644 --- a/app/Filament/Pages/Settings/GeneralPage.php +++ b/app/Filament/Pages/Settings/GeneralPage.php @@ -92,6 +92,14 @@ public function form(Form $form): Form ->hint(new HtmlString('🔗Cron Generator')) ->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.') @@ -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([ diff --git a/app/Models/Result.php b/app/Models/Result.php index 43425a781..822d37239 100644 --- a/app/Models/Result.php +++ b/app/Models/Result.php @@ -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. @@ -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. */ diff --git a/app/Settings/GeneralSettings.php b/app/Settings/GeneralSettings.php index b2f22d3a3..234bf6f4b 100644 --- a/app/Settings/GeneralSettings.php +++ b/app/Settings/GeneralSettings.php @@ -8,6 +8,8 @@ class GeneralSettings extends Settings { public bool $auth_enabled; + public int $prune_results_older_than; + public ?string $speedtest_schedule; /** @var string[] */ diff --git a/database/settings/2024_02_19_022251_add_prune_results_to_general_settings.php b/database/settings/2024_02_19_022251_add_prune_results_to_general_settings.php new file mode 100644 index 000000000..e7bf5e819 --- /dev/null +++ b/database/settings/2024_02_19_022251_add_prune_results_to_general_settings.php @@ -0,0 +1,11 @@ +migrator->add('general.prune_results_older_than', 0); + } +};