Skip to content

Commit 56208e7

Browse files
authored
Use dedicated config value for storage (#427)
1 parent f2b9d85 commit 56208e7

File tree

5 files changed

+74
-12
lines changed

5 files changed

+74
-12
lines changed

config/pulse.php

+4
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

+12-4
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,24 @@ 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+
);
133+
134+
if ($now->subDays(7)->isAfter($before)) {
135+
$before = $now->subDays(7);
136+
}
129137

130138
$this->connection()
131139
->table('pulse_values')
132-
->where('timestamp', '<=', $now->sub($keep)->getTimestamp())
140+
->where('timestamp', '<=', $before->getTimestamp())
133141
->delete();
134142

135143
$this->connection()
136144
->table('pulse_entries')
137-
->where('timestamp', '<=', $now->sub($keep)->getTimestamp())
145+
->where('timestamp', '<=', $before->getTimestamp())
138146
->delete();
139147

140148
$this->connection()
@@ -144,7 +152,7 @@ public function trim(): void
144152
->each(fn (int $period) => $this->connection()
145153
->table('pulse_aggregates')
146154
->where('period', $period)
147-
->where('bucket', '<=', $now->subMinutes($period)->getTimestamp())
155+
->where('bucket', '<=', max($now->subMinutes($period)->getTimestamp(), $before->getTimestamp()))
148156
->delete());
149157
}
150158

tests/Feature/Ingests/DatabaseTest.php

+48-5
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,45 @@
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', '2 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');
155+
Pulse::ingest();
156+
157+
Pulse::stopRecording();
158+
Date::setTestNow('2000-01-03 00:00:05');
159+
App::make(DatabaseStorage::class)->trim();
160+
161+
expect(DB::table('pulse_entries')->pluck('type')->all())->toBe(['baz']);
162+
expect(DB::table('pulse_values')->pluck('key')->all())->toBe(['baz']);
163+
});
164+
165+
it('restricts trim duration to 7 days', function () {
166+
Config::set('pulse.storage.trim.keep', '7 days');
167+
168+
Date::setTestNow('2000-01-01 00:00:04');
169+
Pulse::record('foo', 'xxxx', 1)->count();
170+
Pulse::set('type', 'foo', 'value');
171+
Date::setTestNow('2000-01-01 00:00:05');
172+
Pulse::record('bar', 'xxxx', 1)->count();
173+
Pulse::set('type', 'bar', 'value');
174+
Date::setTestNow('2000-01-01 00:00:06');
175+
Pulse::record('baz', 'xxxx', 1)->count();
176+
Pulse::set('type', 'baz', 'value');
135177
Pulse::ingest();
136178

137179
Pulse::stopRecording();
138-
Date::setTestNow('2000-01-15 00:00:05');
180+
Date::setTestNow('2000-01-08 00:00:05');
139181
App::make(DatabaseStorage::class)->trim();
140182

141183
expect(DB::table('pulse_entries')->pluck('type')->all())->toBe(['baz']);
184+
expect(DB::table('pulse_values')->pluck('key')->all())->toBe(['baz']);
142185
});

tests/Feature/PulseTest.php

+1-1
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

+9-2
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)