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] Fix trimming direct to storage ingest #318

Merged
merged 6 commits into from
Feb 19, 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 src/Pulse.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public function ingest(): int
$odds = $this->app->make('config')->get('pulse.ingest.trim.lottery') ?? $this->app->make('config')->get('pulse.ingest.trim_lottery');

Lottery::odds(...$odds)
->winner(fn () => $this->rescue(fn () => $ingest->trim(...)))
->winner(fn () => $this->rescue($ingest->trim(...)))
->choose();

$this->flush();
Expand Down
11 changes: 11 additions & 0 deletions tests/Feature/PulseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@
expect($storage->stored[1]->key)->toBe('keep');
});

it('can trim records', function () {
App::instance(Storage::class, $storage = new StorageFake);

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

Pulse::ingest();

expect($storage->stored)->toHaveCount(1);
});

it('can lazily capture entries', function () {
App::instance(Storage::class, $storage = new StorageFake);

Expand Down
39 changes: 28 additions & 11 deletions tests/Feature/Storage/DatabaseStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,12 @@
});

it('combines duplicate count aggregates before upserting', function () {
Config::set('pulse.ingest.trim.lottery', [0, 1]);
$queries = collect();
DB::listen(fn ($query) => $queries[] = $query);
DB::listen(function (QueryExecuted $event) use (&$queries) {
if (str_starts_with($event->sql, 'insert')) {
$queries[] = $event;
}
});

Pulse::record('type', 'key1')->count();
Pulse::record('type', 'key1')->count();
Expand All @@ -150,9 +153,12 @@
});

it('combines duplicate min aggregates before upserting', function () {
Config::set('pulse.ingest.trim.lottery', [0, 1]);
$queries = collect();
DB::listen(fn ($query) => $queries[] = $query);
DB::listen(function (QueryExecuted $event) use (&$queries) {
if (str_starts_with($event->sql, 'insert')) {
$queries[] = $event;
}
});

Pulse::record('type', 'key1', 200)->min();
Pulse::record('type', 'key1', 100)->min();
Expand All @@ -177,9 +183,12 @@
});

it('combines duplicate max aggregates before upserting', function () {
Config::set('pulse.ingest.trim.lottery', [0, 1]);
$queries = collect();
DB::listen(fn ($query) => $queries[] = $query);
DB::listen(function (QueryExecuted $event) use (&$queries) {
if (str_starts_with($event->sql, 'insert')) {
$queries[] = $event;
}
});

Pulse::record('type', 'key1', 100)->max();
Pulse::record('type', 'key1', 300)->max();
Expand All @@ -204,9 +213,12 @@
});

it('combines duplicate sum aggregates before upserting', function () {
Config::set('pulse.ingest.trim.lottery', [0, 1]);
$queries = collect();
DB::listen(fn ($query) => $queries[] = $query);
DB::listen(function (QueryExecuted $event) use (&$queries) {
if (str_starts_with($event->sql, 'insert')) {
$queries[] = $event;
}
});

Pulse::record('type', 'key1', 100)->sum();
Pulse::record('type', 'key1', 300)->sum();
Expand All @@ -231,9 +243,12 @@
});

it('combines duplicate average aggregates before upserting', function () {
Config::set('pulse.ingest.trim.lottery', [0, 1]);
$queries = collect();
DB::listen(fn ($query) => $queries[] = $query);
DB::listen(function (QueryExecuted $event) use (&$queries) {
if (str_starts_with($event->sql, 'insert')) {
$queries[] = $event;
}
});

Pulse::record('type', 'key1', 100)->avg();
Pulse::record('type', 'key1', 300)->avg();
Expand Down Expand Up @@ -466,7 +481,9 @@
it('collapses values with the same key into a single upsert', function () {
$bindings = [];
DB::listen(function (QueryExecuted $event) use (&$bindings) {
$bindings = $event->bindings;
if (str_starts_with($event->sql, 'insert')) {
$bindings = $event->bindings;
}
});

Pulse::set('read_counter', 'post:321', 123);
Expand Down
2 changes: 1 addition & 1 deletion tests/StorageFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function store(Collection $items): void
*/
public function trim(): void
{
//
$this->stored = $this->stored->reject(fn($record) => $record->timestamp <= now()->subWeek()->timestamp);
}

/**
Expand Down
Loading