diff --git a/config/pulse.php b/config/pulse.php index 7c00f819..d78c73b4 100644 --- a/config/pulse.php +++ b/config/pulse.php @@ -83,7 +83,7 @@ 'trim' => [ 'lottery' => [1, 1_000], - 'keep' => '7 days', + 'keep' => env('PULSE_INGEST_KEEP', '7 days'), ], 'redis' => [ diff --git a/src/Storage/DatabaseStorage.php b/src/Storage/DatabaseStorage.php index 3cabc64f..a3213320 100644 --- a/src/Storage/DatabaseStorage.php +++ b/src/Storage/DatabaseStorage.php @@ -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() diff --git a/tests/Feature/Ingests/DatabaseTest.php b/tests/Feature/Ingests/DatabaseTest.php index 9fd0500c..3ad6ed1e 100644 --- a/tests/Feature/Ingests/DatabaseTest.php +++ b/tests/Feature/Ingests/DatabaseTest.php @@ -1,6 +1,7 @@ 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']); +}); diff --git a/tests/Feature/PulseTest.php b/tests/Feature/PulseTest.php index c9223a6a..c8245238 100644 --- a/tests/Feature/PulseTest.php +++ b/tests/Feature/PulseTest.php @@ -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); diff --git a/tests/StorageFake.php b/tests/StorageFake.php index 5f9c815f..b52e5806 100644 --- a/tests/StorageFake.php +++ b/tests/StorageFake.php @@ -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); } /**