From 9a66446ff37690104adde9b7c31e51befa2940d0 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 2 Dec 2024 09:51:54 +1100 Subject: [PATCH] Use dedicated config value for storage --- config/pulse.php | 4 ++++ src/Storage/DatabaseStorage.php | 12 +++++++---- tests/Feature/Ingests/DatabaseTest.php | 29 ++++++++++++++++++++++---- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/config/pulse.php b/config/pulse.php index d78c73b4..1edfa4ad 100644 --- a/config/pulse.php +++ b/config/pulse.php @@ -59,6 +59,10 @@ 'storage' => [ 'driver' => env('PULSE_STORAGE_DRIVER', 'database'), + 'trim' => [ + 'keep' => env('PULSE_STORAGE_KEEP', '7 days'), + ], + 'database' => [ 'connection' => env('PULSE_DB_CONNECTION'), 'chunk' => 1000, diff --git a/src/Storage/DatabaseStorage.php b/src/Storage/DatabaseStorage.php index a3213320..82e30b1a 100644 --- a/src/Storage/DatabaseStorage.php +++ b/src/Storage/DatabaseStorage.php @@ -125,16 +125,20 @@ public function trim(): void { $now = CarbonImmutable::now(); - $keep = $this->config->get('pulse.ingest.trim.keep'); + $keep = $this->config->get('pulse.storage.trim.keep') ?? '7 days'; + + $before = $now->subMilliseconds( + (int) CarbonInterval::fromString($keep)->totalMilliseconds + ); $this->connection() ->table('pulse_values') - ->where('timestamp', '<=', $now->sub($keep)->getTimestamp()) + ->where('timestamp', '<=', $before->getTimestamp()) ->delete(); $this->connection() ->table('pulse_entries') - ->where('timestamp', '<=', $now->sub($keep)->getTimestamp()) + ->where('timestamp', '<=', $before->getTimestamp()) ->delete(); $this->connection() @@ -144,7 +148,7 @@ public function trim(): void ->each(fn (int $period) => $this->connection() ->table('pulse_aggregates') ->where('period', $period) - ->where('bucket', '<=', $now->subMinutes($period)->getTimestamp()) + ->where('bucket', '<=', max($now->subMinutes($period)->getTimestamp(), $before->getTimestamp())) ->delete()); } diff --git a/tests/Feature/Ingests/DatabaseTest.php b/tests/Feature/Ingests/DatabaseTest.php index 3ad6ed1e..43452de7 100644 --- a/tests/Feature/Ingests/DatabaseTest.php +++ b/tests/Feature/Ingests/DatabaseTest.php @@ -102,6 +102,23 @@ expect(DB::table('pulse_aggregates')->where('period', 1440)->count())->toBe(1); }); +it('trims aggregates to the configured storage duration when shorter than the bucket period duration', function () { + Config::set('pulse.storage.trim.keep', '23 minutes'); + Date::setTestNow('2000-01-01 00:00:00'); // Bucket: 2000-01-01 00:00:00 + Pulse::record('foo', 'xxxx', 1)->count(); + Pulse::ingest(); + Pulse::stopRecording(); + expect(DB::table('pulse_aggregates')->where('period', 1440)->count())->toBe(1); + + Date::setTestNow('2000-01-01 00:22:59'); + App::make(DatabaseStorage::class)->trim(); + expect(DB::table('pulse_aggregates')->where('period', 1440)->count())->toBe(1); + + Date::setTestNow('2000-01-01 00:23:00'); + Pulse::ignore(fn () => App::make(DatabaseStorage::class)->trim()); + expect(DB::table('pulse_aggregates')->where('period', 1440)->count())->toBe(0); +}); + it('trims aggregates once the 7 day bucket is no longer relevant', function () { Date::setTestNow('2000-01-01 02:23:59'); // Bucket: 1999-12-31 23:36:00 Pulse::record('foo', 'xxxx', 1)->count(); @@ -124,14 +141,17 @@ }); it('can configure days of data to keep when trimming', function () { - Config::set('pulse.ingest.trim.keep', '14 days'); + Config::set('pulse.storage.trim.keep', '14 days'); Date::setTestNow('2000-01-01 00:00:04'); - Pulse::record('foo', 'xxxx', 1); + Pulse::record('foo', 'xxxx', 1)->count(); + Pulse::set('type', 'foo', 'value'); Date::setTestNow('2000-01-01 00:00:05'); - Pulse::record('bar', 'xxxx', 1); + Pulse::record('bar', 'xxxx', 1)->count(); + Pulse::set('type', 'bar', 'value'); Date::setTestNow('2000-01-01 00:00:06'); - Pulse::record('baz', 'xxxx', 1); + Pulse::record('baz', 'xxxx', 1)->count(); + Pulse::set('type', 'baz', 'value'); Pulse::ingest(); Pulse::stopRecording(); @@ -139,4 +159,5 @@ App::make(DatabaseStorage::class)->trim(); expect(DB::table('pulse_entries')->pluck('type')->all())->toBe(['baz']); + expect(DB::table('pulse_values')->pluck('key')->all())->toBe(['baz']); });