Skip to content

Commit c803c19

Browse files
committed
wikiMetrics: fix zero count occassions (#972)
* add dataProvider for test * add the fix * lintin * more linting * even more linting * linting
1 parent 28c5037 commit c803c19

2 files changed

Lines changed: 166 additions & 65 deletions

File tree

app/Metrics/App/WikiMetrics.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,15 @@ private function getNumberOfEntities(): array {
199199
$conn = $manager->connection('mw');
200200
$pdo = $conn->getPdo();
201201
$result = $pdo->query($query)->fetchAll(PDO::FETCH_ASSOC);
202-
if (count($result) === 0) {
203-
return [120 => 0, 122 => 0, 146 => 0, 640 => 0];
204-
}
205202

206-
return array_column($result, 'count', 'namespace');
203+
$result = array_column($result, 'count', 'namespace');
204+
205+
// make sure the array keys are set
206+
$result[120] ??= 0;
207+
$result[122] ??= 0;
208+
$result[146] ??= 0;
209+
$result[640] ??= 0;
210+
211+
return $result;
207212
}
208213
}

tests/Metrics/WikiMetricsTest.php

Lines changed: 157 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,157 @@ public function testSaveNullForFailedRequestOfTriplesCount() {
168168
]);
169169
}
170170

171-
public function testSavesEntityCountsCorrectly() {
171+
public static function dummyDataProvider() {
172+
$item1 = [
173+
'page_namespace' => 120,
174+
'page_is_redirect' => 0,
175+
'page_title' => 'foo',
176+
'page_random' => 0,
177+
'page_touched' => random_bytes(10),
178+
'page_latest' => 1,
179+
'page_len' => 2,
180+
];
181+
182+
$item2 = [
183+
'page_namespace' => 120,
184+
'page_is_redirect' => 0,
185+
'page_title' => 'bar',
186+
'page_random' => 0,
187+
'page_touched' => random_bytes(10),
188+
'page_latest' => 0,
189+
'page_len' => 2,
190+
];
191+
192+
$property = [
193+
'page_namespace' => 122,
194+
'page_is_redirect' => 0,
195+
'page_title' => 'foo',
196+
'page_random' => 0,
197+
'page_touched' => random_bytes(10),
198+
'page_latest' => 1,
199+
'page_len' => 2,
200+
];
201+
202+
$entitySchema = [
203+
'page_namespace' => 640,
204+
'page_is_redirect' => 0,
205+
'page_title' => 'bar',
206+
'page_random' => 0,
207+
'page_touched' => random_bytes(10),
208+
'page_latest' => 1,
209+
'page_len' => 2,
210+
];
211+
212+
$entitySchemaRedirect = [
213+
'page_namespace' => 640,
214+
'page_is_redirect' => 1,
215+
'page_title' => 'foo',
216+
'page_random' => 0,
217+
'page_touched' => random_bytes(10),
218+
'page_latest' => 1,
219+
'page_len' => 2,
220+
];
221+
222+
$lexeme = [
223+
'page_namespace' => 146,
224+
'page_is_redirect' => 0,
225+
'page_title' => 'foo',
226+
'page_random' => 0,
227+
'page_touched' => random_bytes(10),
228+
'page_latest' => 1,
229+
'page_len' => 2,
230+
];
231+
232+
// all relevant data types
233+
yield [
234+
'expectedItemCount' => 2,
235+
'expectedPropertyCount' => 1,
236+
'expectedLexemeCount' => 1,
237+
'expectedEntitySchemaCount' => 1,
238+
239+
'pageData' => [
240+
$item1,
241+
$item2,
242+
$property,
243+
$lexeme,
244+
$entitySchema,
245+
$entitySchemaRedirect,
246+
],
247+
];
248+
249+
// zero items
250+
yield [
251+
'expectedItemCount' => 0,
252+
'expectedPropertyCount' => 1,
253+
'expectedLexemeCount' => 1,
254+
'expectedEntitySchemaCount' => 1,
255+
256+
'pageData' => [
257+
// $item1,
258+
// $item2,
259+
$property,
260+
$lexeme,
261+
$entitySchema,
262+
$entitySchemaRedirect,
263+
],
264+
];
265+
266+
// zero properties
267+
yield [
268+
'expectedItemCount' => 2,
269+
'expectedPropertyCount' => 0,
270+
'expectedLexemeCount' => 1,
271+
'expectedEntitySchemaCount' => 1,
272+
273+
'pageData' => [
274+
$item1,
275+
$item2,
276+
// $property,
277+
$lexeme,
278+
$entitySchema,
279+
$entitySchemaRedirect,
280+
],
281+
];
282+
283+
// zero Lexemes
284+
yield [
285+
'expectedItemCount' => 2,
286+
'expectedPropertyCount' => 1,
287+
'expectedLexemeCount' => 0,
288+
'expectedEntitySchemaCount' => 1,
289+
290+
'pageData' => [
291+
$item1,
292+
$item2,
293+
$property,
294+
// $lexeme,
295+
$entitySchema,
296+
$entitySchemaRedirect,
297+
],
298+
];
299+
300+
// zero EntitySchemas
301+
yield [
302+
'expectedItemCount' => 2,
303+
'expectedPropertyCount' => 1,
304+
'expectedLexemeCount' => 1,
305+
'expectedEntitySchemaCount' => 0,
306+
307+
'pageData' => [
308+
$item1,
309+
$item2,
310+
$property,
311+
$lexeme,
312+
// $entitySchema,
313+
$entitySchemaRedirect, // should not count
314+
],
315+
];
316+
}
317+
318+
/**
319+
* @dataProvider dummyDataProvider
320+
*/
321+
public function testSavesEntityCountsCorrectly($expectedItemCount, $expectedPropertyCount, $expectedLexemeCount, $expectedEntitySchemaCount, $pageData) {
172322
$wiki = Wiki::factory()->create([
173323
'domain' => 'entitycounttest.wikibase.cloud',
174324
]);
@@ -191,66 +341,12 @@ public function testSavesEntityCountsCorrectly() {
191341
});
192342

193343
// Insert dummy data
194-
DB::table($tablePage)->insert([
195-
[
196-
'page_namespace' => 120,
197-
'page_is_redirect' => 0,
198-
'page_title' => 'foo',
199-
'page_random' => 0,
200-
'page_touched' => random_bytes(10),
201-
'page_latest' => 1,
202-
'page_len' => 2,
203-
], // item
204-
[
205-
'page_namespace' => 120,
206-
'page_is_redirect' => 0,
207-
'page_title' => 'bar',
208-
'page_random' => 0,
209-
'page_touched' => random_bytes(10),
210-
'page_latest' => 0,
211-
'page_len' => 2,
212-
], // item
213-
[
214-
'page_namespace' => 122,
215-
'page_is_redirect' => 0,
216-
'page_title' => 'foo',
217-
'page_random' => 0,
218-
'page_touched' => random_bytes(10),
219-
'page_latest' => 1,
220-
'page_len' => 2], // property
221-
[
222-
'page_namespace' => 640,
223-
'page_is_redirect' => 0,
224-
'page_title' => 'bar',
225-
'page_random' => 0,
226-
'page_touched' => random_bytes(10),
227-
'page_latest' => 1,
228-
'page_len' => 2,
229-
], // entity schema
230-
[
231-
'page_namespace' => 146,
232-
'page_is_redirect' => 0,
233-
'page_title' => 'foo',
234-
'page_random' => 0,
235-
'page_touched' => random_bytes(10),
236-
'page_latest' => 1,
237-
'page_len' => 2,
238-
], // lexeme
239-
[
240-
'page_namespace' => 640,
241-
'page_is_redirect' => 1,
242-
'page_title' => 'foo',
243-
'page_random' => 0,
244-
'page_touched' => random_bytes(10),
245-
'page_latest' => 1,
246-
'page_len' => 2,
247-
], // entity schema
248-
]);
344+
DB::table($tablePage)->insert($pageData);
249345
WikiDailyMetrics::create([
250346
'id' => $wiki->id . '_' . now()->subDay()->toDateString(),
251347
'wiki_id' => $wiki->id,
252348
'date' => now()->subDay()->toDateString(),
253-
'pages' => 6,
349+
'pages' => count($pageData),
254350
'is_deleted' => 0,
255351
]);
256352

@@ -262,10 +358,10 @@ public function testSavesEntityCountsCorrectly() {
262358

263359
$this->assertDatabaseHas('wiki_daily_metrics', [
264360
'wiki_id' => $wiki->id,
265-
'item_count' => 2,
266-
'property_count' => 1,
267-
'lexeme_count' => 1,
268-
'entity_schema_count' => 1, // the redirect should be ignored
361+
'item_count' => $expectedItemCount,
362+
'property_count' => $expectedPropertyCount,
363+
'lexeme_count' => $expectedLexemeCount,
364+
'entity_schema_count' => $expectedEntitySchemaCount, // redirects should be ignored
269365
]);
270366
}
271367
}

0 commit comments

Comments
 (0)