Skip to content

Commit

Permalink
Add purge command
Browse files Browse the repository at this point in the history
  • Loading branch information
saeedvaziry committed Jan 19, 2022
1 parent 6eff39c commit 521fe46
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 2 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 7 additions & 1 deletion config/monitoring.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
];
52 changes: 52 additions & 0 deletions src/Commands/PurgeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace SaeedVaziry\Monitoring\Commands;

use Exception;
use Illuminate\Console\Command;
use SaeedVaziry\Monitoring\Models\MonitoringRecord;

class PurgeCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'monitoring:purge';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Purge old data';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return void
* @throws Exception
*
*/
public function handle()
{
MonitoringRecord::query()
->where(
'created_at',
'<',
date('Y-m-d', strtotime(config('monitoring.purge_before', '-1 day')))
)
->delete();
}
}
6 changes: 6 additions & 0 deletions src/MonitoringServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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([
Expand Down Expand Up @@ -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');
Expand All @@ -74,6 +79,7 @@ public function provides()
{
return [
'command.monitoring.record',
'command.monitoring.purge',
];
}
}
42 changes: 42 additions & 0 deletions tests/Commands/PurgeCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace SaeedVaziry\Monitoring\Tests\Commands;

use Illuminate\Foundation\Testing\RefreshDatabase;
use SaeedVaziry\Monitoring\Database\Factories\MonitoringRecordFactory;
use SaeedVaziry\Monitoring\Tests\TestCase;

class PurgeCommandTest extends TestCase
{
use RefreshDatabase;

public function testPurge()
{
config()->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);
}
}
2 changes: 1 addition & 1 deletion tests/Commands/RecordCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class RecordCommandTest extends TestCase
{
use RefreshDatabase;

public function testUpdateIps()
public function testRecord()
{
$this->artisan('monitoring:record');

Expand Down

0 comments on commit 521fe46

Please sign in to comment.