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

Add global scope to ensure we only calculate hits on endpoints matching the /api route prefix #45

Merged
merged 2 commits into from
Apr 29, 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
4 changes: 4 additions & 0 deletions app/Models/Hit.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,9 @@ protected static function boot()
static::addGlobalScope('order', function (Builder $builder) {
$builder->orderBy('created_at', 'desc');
});

static::addGlobalScope('api', function (Builder $builder) {
$builder->where('endpoint', 'LIKE', '/api/%');
});
}
}
2 changes: 1 addition & 1 deletion app/Notifications/WeeklyStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function toSlack()
$block->type('header')
->text([
'type' => 'plain_text',
'text' => 'PHP Releases Weekly Hits!',
'text' => 'PHP Releases Weekly API Hits!',
]);
})
->block(function ($block) use ($hits) {
Expand Down
24 changes: 24 additions & 0 deletions tests/Feature/StatsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,30 @@ public function it_ignores_bot_user_agents()
]);
}

/** @test */
public function it_filters_out_web_requests(): void
{
Hit::factory()
->count(2)
->create([
'endpoint' => '/',
]);

Hit::factory()
->count(2)
->create([
'endpoint' => '/api/releases',
]);

$hits = Hit::forTimePeriod('week');

$this->assertSame([
'current' => 2,
'previous' => 0,
'changePercent' => 100,
], $hits);
}

/** @test */
public function it_calculates_percent_increase()
{
Expand Down
Loading