-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add command to prune data with suggested before hours
- Loading branch information
Showing
6 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Laravel\Pulse\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Console\ConfirmableTrait; | ||
use Laravel\Pulse\Pulse; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class PruneCommand extends Command | ||
{ | ||
use ConfirmableTrait; | ||
|
||
/** | ||
* The command's signature. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'pulse:prune {--hours=24 : The number of hours to retain Pulse data}'; | ||
|
||
/** | ||
* The command's description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Prune stale entries from the Prune database'; | ||
|
||
/** | ||
* Handle the command. | ||
*/ | ||
public function handle(Pulse $pulse): int | ||
{ | ||
if (! $this->confirmToProceed()) { | ||
return Command::FAILURE; | ||
} | ||
|
||
$pulse->prune( | ||
now()->subHours( | ||
(int) $this->option('hours') | ||
) | ||
); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Artisan; | ||
use Illuminate\Support\Facades\Date; | ||
use Illuminate\Support\Facades\DB; | ||
use Laravel\Pulse\Facades\Pulse; | ||
|
||
it('prune Pulse data', function () { | ||
# Arrange | ||
Date::setTestNow('2000-01-01 00:00:04'); | ||
Pulse::set('type', 'foo', 'value'); | ||
Date::setTestNow('2000-01-01 00:00:05'); | ||
Pulse::set('type', 'bar', 'value'); | ||
Date::setTestNow('2000-01-01 00:00:06'); | ||
Pulse::set('type', 'baz', 'value'); | ||
Pulse::ingest(); | ||
|
||
Pulse::stopRecording(); | ||
Date::setTestNow('2000-01-08 00:00:05'); | ||
|
||
# Act | ||
Artisan::call('pulse:prune'); | ||
|
||
# Assert | ||
expect(DB::table('pulse_values')->count())->toBe(0); | ||
expect(DB::table('pulse_entries')->count())->toBe(0); | ||
expect(DB::table('pulse_aggregates')->count())->toBe(0); | ||
}); | ||
|
||
it('prune entries from the suggested before hours', function () { | ||
// Entries will be pruned | ||
Date::setTestNow('2000-01-01 00:00:04'); | ||
Pulse::record('foo', 'xxxx', 1); | ||
Date::setTestNow('2000-01-01 00:00:05'); | ||
Pulse::record('bar', 'xxxx', 1); | ||
|
||
// Entries will be kept | ||
Date::setTestNow("2000-01-07 00:00:00"); | ||
Pulse::record('baz', 'xxxx', 1); | ||
Pulse::ingest(); | ||
|
||
Pulse::stopRecording(); | ||
Date::setTestNow('2000-01-09 00:00:00'); | ||
|
||
# Act | ||
Artisan::call("pulse:prune --hours=168"); | ||
|
||
expect(DB::table('pulse_entries')->pluck('type')->all())->toBe(['baz']); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters