From 521fe467f72d5dcb7f42074bc71065ddee585baf Mon Sep 17 00:00:00 2001 From: Saeed Vaziry Date: Wed, 19 Jan 2022 12:27:59 +0400 Subject: [PATCH] Add purge command --- README.md | 9 +++++ config/monitoring.php | 8 ++++- src/Commands/PurgeCommand.php | 52 ++++++++++++++++++++++++++++ src/MonitoringServiceProvider.php | 6 ++++ tests/Commands/PurgeCommandTest.php | 42 ++++++++++++++++++++++ tests/Commands/RecordCommandTest.php | 2 +- 6 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 src/Commands/PurgeCommand.php create mode 100644 tests/Commands/PurgeCommandTest.php diff --git a/README.md b/README.md index 919b88b..e650236 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,15 @@ Monitoring::memory()->usage(); // returns Memory usage Monitoring::disk()->usage(); // returns Disk usage ``` +## Purge Records + +Without purging, the `monitoring_records` table can accumulate records very quickly. To mitigate this, you should schedule the monitoring:purge Artisan command to run daily or any time you wish. +You can also, Set the `purge_before` configuration at `config/monitoring.php`. + +```php +$schedule->command('monitoring:purge')->daily(); +``` + ## Contributing Please feel free to submit an issue or open a PR. diff --git a/config/monitoring.php b/config/monitoring.php index d5ab0cc..188a619 100644 --- a/config/monitoring.php +++ b/config/monitoring.php @@ -69,5 +69,11 @@ /* * You can enable or disable migrations here */ - 'migrations' => true + 'migrations' => true, + + /* + * Purge recorded data + * Supports PHP strtotime options like: '-1 day', '-2 hours', ... + */ + 'purge_before' => '-1 day' ]; diff --git a/src/Commands/PurgeCommand.php b/src/Commands/PurgeCommand.php new file mode 100644 index 0000000..d48cde0 --- /dev/null +++ b/src/Commands/PurgeCommand.php @@ -0,0 +1,52 @@ +where( + 'created_at', + '<', + date('Y-m-d', strtotime(config('monitoring.purge_before', '-1 day'))) + ) + ->delete(); + } +} diff --git a/src/MonitoringServiceProvider.php b/src/MonitoringServiceProvider.php index b580361..88607dc 100644 --- a/src/MonitoringServiceProvider.php +++ b/src/MonitoringServiceProvider.php @@ -3,6 +3,7 @@ namespace SaeedVaziry\Monitoring; use Illuminate\Support\ServiceProvider; +use SaeedVaziry\Monitoring\Commands\PurgeCommand; use SaeedVaziry\Monitoring\Commands\RecordCommand; class MonitoringServiceProvider extends ServiceProvider @@ -26,6 +27,9 @@ public function register() $this->app->singleton('command.monitoring.record', function () { return new RecordCommand(); }); + $this->app->singleton('command.monitoring.purge', function () { + return new PurgeCommand(); + }); // publish config $this->publishes([ @@ -57,6 +61,7 @@ public function boot() // register command $this->commands(RecordCommand::class); + $this->commands(PurgeCommand::class); // register routes $this->loadRoutesFrom(__DIR__ . '/Http/routes.php'); @@ -74,6 +79,7 @@ public function provides() { return [ 'command.monitoring.record', + 'command.monitoring.purge', ]; } } diff --git a/tests/Commands/PurgeCommandTest.php b/tests/Commands/PurgeCommandTest.php new file mode 100644 index 0000000..97ea603 --- /dev/null +++ b/tests/Commands/PurgeCommandTest.php @@ -0,0 +1,42 @@ +set('monitoring.purge_before', '-1 day'); + + MonitoringRecordFactory::times(10)->create([ + 'created_at' => now()->subDays(10) + ]); + + $this->artisan('monitoring:purge'); + + $this->assertDatabaseCount('monitoring_records', 0); + } + + public function testPurgeLastHour() + { + config()->set('monitoring.purge_before', '-1 hour'); + + MonitoringRecordFactory::times(10)->create([ + 'created_at' => now()->subMinutes(10) + ]); + + MonitoringRecordFactory::times(10)->create([ + 'created_at' => now()->subDays(10) + ]); + + $this->artisan('monitoring:purge'); + + $this->assertDatabaseCount('monitoring_records', 10); + } +} diff --git a/tests/Commands/RecordCommandTest.php b/tests/Commands/RecordCommandTest.php index c5151b2..3bfd0b1 100644 --- a/tests/Commands/RecordCommandTest.php +++ b/tests/Commands/RecordCommandTest.php @@ -9,7 +9,7 @@ class RecordCommandTest extends TestCase { use RefreshDatabase; - public function testUpdateIps() + public function testRecord() { $this->artisan('monitoring:record');