Skip to content

Commit 5878900

Browse files
Fix bug on wiki metrics recording (#883)
* Fix bug on wiki metrics recoding * Add test for deleted wikis * adjust testAddRecordsWikiIsDeleted --------- Co-authored-by: dena <dena@wikimedia.de>
1 parent 483513d commit 5878900

3 files changed

Lines changed: 36 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# api
22

3+
## 10x.18.5 - 28 February 2025
4+
- Fix bug on wiki metrics recoding
5+
36
## 10x.18.4 - 19 February 2025
47
- Fix job definition in UpdateWikiDailyMetricJob class
58
- Update actions/cache in github workflow to version 4 to pass CI

app/Metrics/App/WikiMetrics.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ public function saveMetrics(Wiki $wiki): void
1616
$isDeleted = (bool)$wiki->deleted_at;
1717
if ($oldRecord) {
1818
if ($oldRecord->is_deleted) {
19-
\Log::info("WikiMetrics is deleted, no new record for WikiMetrics ID {$wiki->id}.");
19+
\Log::info("Wiki is deleted, no new record for WikiMetrics ID {$wiki->id}.");
2020
return;
2121
}
22-
if ($oldRecord->pages === $todayPageCount) {
23-
\Log::info("Page count unchanged for WikiMetrics ID {$wiki->id}, no new record added.");
24-
return;
22+
if (!$isDeleted) {
23+
if ($oldRecord->pages === $todayPageCount) {
24+
\Log::info("Page count unchanged for WikiMetrics ID {$wiki->id}, no new record added.");
25+
return;
26+
}
2527
}
2628
}
2729
WikiDailyMetrics::create([

tests/Metrics/WikiMetricsTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,32 @@ public function testDoesNotAddDuplicateRecordsWithOnlyDateChange()
4949
'date' => Carbon::today()->toDateString()
5050
]);
5151
}
52+
53+
public function testAddRecordsWikiIsDeleted()
54+
{
55+
$wiki = Wiki::factory()->create([
56+
'domain' => 'thisfake.wikibase.cloud'
57+
]);
58+
//Insert an old metric value for a wiki
59+
WikiDailyMetrics::create([
60+
'id' => $wiki->id. '_'. Carbon::yesterday()->toDateString(),
61+
'wiki_id' => $wiki->id,
62+
'date' => Carbon::yesterday()->toDateString(),
63+
'pages' => 0,
64+
'is_deleted' => 1
65+
]);
66+
//delete the wiki
67+
$wiki->delete();
68+
$wiki->save();
69+
70+
(new WikiMetrics())->saveMetrics($wiki);
71+
72+
//Assert No new record was created for today
73+
$this->assertDatabaseMissing('wiki_daily_metrics', [
74+
'wiki_id' => $wiki->id,
75+
'is_deleted' => 1,
76+
'date' => now()->toDateString()
77+
]);
78+
}
5279
}
5380

0 commit comments

Comments
 (0)