Skip to content

Commit

Permalink
[1.x] Add Configurable Trim Duration for Pulse Data Storage (#424)
Browse files Browse the repository at this point in the history
* feat: add command to prune data with suggested before hours

* chore: remove prune structure

* feat: add environment to keep data in database storage

* chore: remove unused DateTimeInterface imports from Storage contracts

* Update DatabaseStorage.php

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
tharlei and taylorotwell authored Nov 29, 2024
1 parent 8e0107f commit 1ff6403
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion config/pulse.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@

'trim' => [
'lottery' => [1, 1_000],
'keep' => '7 days',
'keep' => env('PULSE_INGEST_KEEP', '7 days'),
],

'redis' => [
Expand Down
6 changes: 4 additions & 2 deletions src/Storage/DatabaseStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,16 @@ public function trim(): void
{
$now = CarbonImmutable::now();

$keep = $this->config->get('pulse.ingest.trim.keep');

$this->connection()
->table('pulse_values')
->where('timestamp', '<=', $now->subWeek()->getTimestamp())
->where('timestamp', '<=', $now->sub($keep)->getTimestamp())
->delete();

$this->connection()
->table('pulse_entries')
->where('timestamp', '<=', $now->subWeek()->getTimestamp())
->where('timestamp', '<=', $now->sub($keep)->getTimestamp())
->delete();

$this->connection()
Expand Down
19 changes: 19 additions & 0 deletions tests/Feature/Ingests/DatabaseTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB;
use Laravel\Pulse\Facades\Pulse;
Expand Down Expand Up @@ -121,3 +122,21 @@
App::make(DatabaseStorage::class)->trim();
expect(DB::table('pulse_aggregates')->where('period', 10080)->count())->toBe(1);
});

it('can configure days of data to keep when trimming', function () {
Config::set('pulse.ingest.trim.keep', '14 days');

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);
Date::setTestNow('2000-01-01 00:00:06');
Pulse::record('baz', 'xxxx', 1);
Pulse::ingest();

Pulse::stopRecording();
Date::setTestNow('2000-01-15 00:00:05');
App::make(DatabaseStorage::class)->trim();

expect(DB::table('pulse_entries')->pluck('type')->all())->toBe(['baz']);
});
13 changes: 13 additions & 0 deletions tests/Feature/PulseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@
expect($storage->stored)->toHaveCount(1);
});

it('can configure days of data to keep when trimming', function () {
Config::set('pulse.ingest.trim.keep', '30 days');
App::instance(Storage::class, $storage = new StorageFake);

Pulse::record('foo', 'delete', 0, now()->subMonth());
Pulse::record('foo', 'keep', 0, now()->subWeek());
Pulse::record('foo', 'keep', 0);

Pulse::ingest();

expect($storage->stored)->toHaveCount(2);
});

it('can lazily capture entries', function () {
App::instance(Storage::class, $storage = new StorageFake);

Expand Down
4 changes: 3 additions & 1 deletion tests/StorageFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public function store(Collection $items): void
*/
public function trim(): void
{
$this->stored = $this->stored->reject(fn ($record) => $record->timestamp <= now()->subWeek()->timestamp);
$keep = config('pulse.ingest.trim.keep');

$this->stored = $this->stored->reject(fn ($record) => $record->timestamp <= now()->sub($keep)->timestamp);
}

/**
Expand Down

0 comments on commit 1ff6403

Please sign in to comment.