ScheduleHistory maximum record count
onursahindur opened this issue · 8 comments
Hello thanks for a great plugin.
I want to ask if it is possible to limit the ScheduleHistory count on db?
I tried to approach to this feature from different perspectives however it is always a good idea to ask the developer of the repo first. Do you have any quick solutions or recommendations? Thanks.
As you can see if we do not control it, it may lead to many records on db.
Btw, I applied a solution by using https://laravel.com/docs/10.x/eloquent#observers if someone wants to achieve something like this.
First add a new row to filament-database-schedule.php
/**
* Maximum history item count to keep for a task. Leave it null for limitless.
*/
'history_max_count' => env('FILAMENT_SCHEDULE_HISTORY_MAX_COUNT', 10),
Observer example code below:
<?php
namespace App\Observers;
use HusamTariq\FilamentDatabaseSchedule\Models\Schedule;
use HusamTariq\FilamentDatabaseSchedule\Models\ScheduleHistory;
class ScheduleHistoryObserver
{
/**
* Handle the ScheduleHistory "created" event.
*/
public function created(ScheduleHistory $scheduleHistory): void
{
$maximum_history_count = config('filament-database-schedule.history_max_count');
if (isset($maximum_history_count)) {
$schedule = Schedule::find($scheduleHistory->schedule_id);
$history_count = $schedule->histories()->count();
if ($history_count > $maximum_history_count) {
$willDeletedIds = $schedule->histories()
->orderBy('created_at','desc')
->skip($maximum_history_count)
->pluck('id');
ScheduleHistory::whereIn('id', $willDeletedIds)->delete();
}
}
}
}
Do not forget to register your observer on EventServiceProvider.php
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
//
ScheduleHistory::observe(ScheduleHistoryObserver::class);
}
You are life saver @onursahindur, thanks!
@onursahindur
Could you test this branch? Then I can make a pull request.
https://github.com/TobiasPlaten/filament-database-schedule/tree/ScheduleHistoryMaxCount
I set the configuration to null so that after an update, others won't unintentionally delete the history. So, you'll need to either publish the config or edit your .env file.
It might also make sense to be able to set the number of entries in the schedule rather than globally for all. A schedule that runs only once a day will require far fewer entries than one that runs every hour. For example, I would be interested in the last 7 days regardless of the count.
@TobiasPlaten thank you, that is a great approach.
However when I try to test your solution in filament v3 using laravel 10.x with php >8.2, the dependency got errors.
So we also forked the library and use the solution of mine and yours together.
You can check here: https://github.com/commentout-tr/filament-database-schedule
Also in the observe function in our system if we do not get the command
as follows, we are getting errors.
$schedule = Schedule::find($scheduleHistory->schedule_id);
$history_count = $schedule->histories()->count();
And also the suggestion about setting number of entries in the schedule model rather than globally is a better approach.
@onursahindur
I'll take a detailed look at this and create a solution that can be configured in the schedule.
@onursahindur Here is the current status. Tested with PHP 8.3 and SQLite 3 on a fresh Laravel and Filament installation.
I think this should include everything needed. The following features have been implemented:
Yes, perfect! Thanks ⭐️