Skip to content

Commit

Permalink
feat: add command to prune data with suggested before hours
Browse files Browse the repository at this point in the history
  • Loading branch information
tharlei committed Nov 28, 2024
1 parent 17353de commit 4b52b70
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/Commands/PruneCommand.php
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;
}
}
9 changes: 9 additions & 0 deletions src/Contracts/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laravel\Pulse\Contracts;

use Carbon\CarbonInterval;
use DateTimeInterface;
use Illuminate\Support\Collection;

interface Storage
Expand All @@ -19,6 +20,14 @@ public function store(Collection $items): void;
*/
public function trim(): void;

/**
* Prune the storage.
*
* @param \DateTimeInterface $before
* @return void
*/
public function prune(DateTimeInterface $before): void;

/**
* Purge the storage.
*
Expand Down
1 change: 1 addition & 0 deletions src/PulseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ protected function registerCommands(): void
Commands\CheckCommand::class,
Commands\RestartCommand::class,
Commands\ClearCommand::class,
Commands\PruneCommand::class,
]);

AboutCommand::add('Pulse', fn () => [
Expand Down
32 changes: 32 additions & 0 deletions src/Storage/DatabaseStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Carbon\CarbonImmutable;
use Carbon\CarbonInterval;
use Closure;
use DateTimeInterface;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Database\Connection;
use Illuminate\Database\DatabaseManager;
Expand Down Expand Up @@ -146,6 +147,37 @@ public function trim(): void
->delete());
}

/**
* Prune the storage.
*
* @param \DateTimeInterface $before
* @return void
*/
public function prune(DateTimeInterface $before): void
{
$before = CarbonImmutable::parse($before);

$this->connection()
->table('pulse_values')
->where('timestamp', '<', $before->getTimestamp())
->delete();

$this->connection()
->table('pulse_entries')
->where('timestamp', '<', $before->getTimestamp())
->delete();

$this->connection()
->table('pulse_aggregates')
->distinct()
->pluck('period')
->each(fn (int $period) => $this->connection()
->table('pulse_aggregates')
->where('period', $period)
->where('bucket', '<', $before->subMinutes($period)->getTimestamp())
->delete());
}

/**
* Purge the storage.
*
Expand Down
49 changes: 49 additions & 0 deletions tests/Feature/Commands/PruneCommandTest.php
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']);
});
12 changes: 12 additions & 0 deletions tests/StorageFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests;

use Carbon\CarbonInterval;
use DateTimeInterface;
use Illuminate\Support\Collection;
use Laravel\Pulse\Contracts\Storage;

Expand Down Expand Up @@ -34,6 +35,17 @@ public function trim(): void
$this->stored = $this->stored->reject(fn ($record) => $record->timestamp <= now()->subWeek()->timestamp);
}

/**
* Prune the storage.
*
* @param \DateTimeInterface $before
* @return void
*/
public function prune(DateTimeInterface $before): void
{
$this->stored = $this->stored->reject(fn ($record) => $record->timestamp <= $before->getTimestamp());
}

/**
* Purge the storage.
*
Expand Down

0 comments on commit 4b52b70

Please sign in to comment.