Skip to content
Open
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
12 changes: 12 additions & 0 deletions app/Metrics/App/WikiMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ public function saveMetrics(Wiki $wiki): void {
$this->wiki = $wiki;

$today = now()->format('Y-m-d');

// Skip expensive metrics collection if today's record already exists.
$recordExists = WikiDailyMetrics::where('wiki_id', $wiki->id)
->where('date', $today)
->exists();

if ($recordExists) {
Log::warning("Daily metric already exists for Wiki ID {$wiki->id} on {$today}; skipping metrics collection.");

return;
}

$tripleCount = $this->getNumOfTriples();
$todayPageCount = $wiki->wikiSiteStats()->first()->pages ?? 0;
$isDeleted = (bool) $wiki->deleted_at;
Expand Down
51 changes: 51 additions & 0 deletions tests/Jobs/UpdateWikiDailyMetricJobTest.php

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to add another test to be sure it logs the warning when there is already a record for the given day.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the LogFake pattern, same pattern in our repo ;) please check

Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@
use App\WikiDb;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
use TiMacDonald\Log\LogEntry;
use TiMacDonald\Log\LogFake;

class UpdateWikiDailyMetricJobTest extends TestCase {
use RefreshDatabase;

protected function setUp(): void {
parent::setUp();
Log::swap(new LogFake());
}

public function testDispatchJob() {
Queue::fake();

Expand Down Expand Up @@ -72,4 +80,47 @@ public function testRunJobForAllWikisIncludingDeletedWikis() {
'entity_schema_count' => 0,
]);
}

public function testRunningJobTwiceForSameWikiWithChangedValuesSkipsSecondRunWhenRecordExistsForToday() {
$wiki = Wiki::factory()->create([
'domain' => 'duplicate.wikibase.cloud',
]);

$manager = $this->app->make('db');
$job = new ProvisionWikiDbJob();
$job->handle($manager);

$wikiDb = WikiDb::whereDoesntHave('wiki')->first();
$wikiDb->update(['wiki_id' => $wiki->id]);

$wiki->wikiSiteStats()->create([
'pages' => 10,
'users' => 3,
]);

$dailyMetricJob = new UpdateWikiDailyMetricJob();
$dailyMetricJob->handle();

$wiki->wikiSiteStats()->first()->update([
'pages' => 12,
'users' => 5,
]);

$dailyMetricJob->handle();

$this->assertDatabaseCount('wiki_daily_metrics', 1)
->assertDatabaseHas('wiki_daily_metrics', [
'wiki_id' => $wiki->id,
'date' => Carbon::today()->toDateString(),
'pages' => 10,
]);

Log::assertLogged(function (LogEntry $log) use ($wiki) {
if ($log->level !== 'warning') {
return false;
}

return str_contains($log->message, "Daily metric already exists for Wiki ID {$wiki->id} on " . Carbon::today()->toDateString());
});
}
}
Loading