Skip to content

Commit cdf3040

Browse files
committed
Use dedicated config value for storage
1 parent f2b9d85 commit cdf3040

File tree

5 files changed

+47
-11
lines changed

5 files changed

+47
-11
lines changed

config/pulse.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@
5959
'storage' => [
6060
'driver' => env('PULSE_STORAGE_DRIVER', 'database'),
6161

62+
'trim' => [
63+
'keep' => env('PULSE_STORAGE_KEEP', '7 days'),
64+
],
65+
6266
'database' => [
6367
'connection' => env('PULSE_DB_CONNECTION'),
6468
'chunk' => 1000,

src/Storage/DatabaseStorage.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,20 @@ public function trim(): void
125125
{
126126
$now = CarbonImmutable::now();
127127

128-
$keep = $this->config->get('pulse.ingest.trim.keep');
128+
$keep = $this->config->get('pulse.storage.trim.keep') ?? '7 days';
129+
130+
$before = $now->subMilliseconds(
131+
(int) CarbonInterval::fromString($keep)->totalMilliseconds
132+
);
129133

130134
$this->connection()
131135
->table('pulse_values')
132-
->where('timestamp', '<=', $now->sub($keep)->getTimestamp())
136+
->where('timestamp', '<=', $before->getTimestamp())
133137
->delete();
134138

135139
$this->connection()
136140
->table('pulse_entries')
137-
->where('timestamp', '<=', $now->sub($keep)->getTimestamp())
141+
->where('timestamp', '<=', $before->getTimestamp())
138142
->delete();
139143

140144
$this->connection()
@@ -144,7 +148,7 @@ public function trim(): void
144148
->each(fn (int $period) => $this->connection()
145149
->table('pulse_aggregates')
146150
->where('period', $period)
147-
->where('bucket', '<=', $now->subMinutes($period)->getTimestamp())
151+
->where('bucket', '<=', max($now->subMinutes($period)->getTimestamp(), $before->getTimestamp()))
148152
->delete());
149153
}
150154

tests/Feature/Ingests/DatabaseTest.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,23 @@
102102
expect(DB::table('pulse_aggregates')->where('period', 1440)->count())->toBe(1);
103103
});
104104

105+
it('trims aggregates to the configured storage duration when configured trim is shorter than the bucket period duration', function () {
106+
Config::set('pulse.storage.trim.keep', '23 minutes');
107+
Date::setTestNow('2000-01-01 00:00:00'); // Bucket: 2000-01-01 00:00:00
108+
Pulse::record('foo', 'xxxx', 1)->count();
109+
Pulse::ingest();
110+
Pulse::stopRecording();
111+
expect(DB::table('pulse_aggregates')->where('period', 1440)->count())->toBe(1);
112+
113+
Date::setTestNow('2000-01-01 00:22:59');
114+
App::make(DatabaseStorage::class)->trim();
115+
expect(DB::table('pulse_aggregates')->where('period', 1440)->count())->toBe(1);
116+
117+
Date::setTestNow('2000-01-01 00:23:00');
118+
Pulse::ignore(fn () => App::make(DatabaseStorage::class)->trim());
119+
expect(DB::table('pulse_aggregates')->where('period', 1440)->count())->toBe(0);
120+
});
121+
105122
it('trims aggregates once the 7 day bucket is no longer relevant', function () {
106123
Date::setTestNow('2000-01-01 02:23:59'); // Bucket: 1999-12-31 23:36:00
107124
Pulse::record('foo', 'xxxx', 1)->count();
@@ -124,19 +141,23 @@
124141
});
125142

126143
it('can configure days of data to keep when trimming', function () {
127-
Config::set('pulse.ingest.trim.keep', '14 days');
144+
Config::set('pulse.storage.trim.keep', '14 days');
128145

129146
Date::setTestNow('2000-01-01 00:00:04');
130-
Pulse::record('foo', 'xxxx', 1);
147+
Pulse::record('foo', 'xxxx', 1)->count();
148+
Pulse::set('type', 'foo', 'value');
131149
Date::setTestNow('2000-01-01 00:00:05');
132-
Pulse::record('bar', 'xxxx', 1);
150+
Pulse::record('bar', 'xxxx', 1)->count();
151+
Pulse::set('type', 'bar', 'value');
133152
Date::setTestNow('2000-01-01 00:00:06');
134-
Pulse::record('baz', 'xxxx', 1);
153+
Pulse::record('baz', 'xxxx', 1)->count();
154+
Pulse::set('type', 'baz', 'value');
135155
Pulse::ingest();
136156

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

141161
expect(DB::table('pulse_entries')->pluck('type')->all())->toBe(['baz']);
162+
expect(DB::table('pulse_values')->pluck('key')->all())->toBe(['baz']);
142163
});

tests/Feature/PulseTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
});
5050

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

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

tests/StorageFake.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests;
44

5+
use Carbon\CarbonImmutable;
56
use Carbon\CarbonInterval;
67
use Illuminate\Support\Collection;
78
use Laravel\Pulse\Contracts\Storage;
@@ -31,9 +32,15 @@ public function store(Collection $items): void
3132
*/
3233
public function trim(): void
3334
{
34-
$keep = config('pulse.ingest.trim.keep');
35+
$now = CarbonImmutable::now();
3536

36-
$this->stored = $this->stored->reject(fn ($record) => $record->timestamp <= now()->sub($keep)->timestamp);
37+
$keep = config('pulse.storage.trim.keep') ?? '7 days';
38+
39+
$before = $now->subMilliseconds(
40+
(int) CarbonInterval::fromString($keep)->totalMilliseconds
41+
);
42+
43+
$this->stored = $this->stored->reject(fn ($record) => $record->timestamp <= $before->timestamp);
3744
}
3845

3946
/**

0 commit comments

Comments
 (0)