Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Add Configurable Trim Duration for Pulse Data Storage #424

Merged
merged 5 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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