Skip to content

Commit 0363fbe

Browse files
rosalieperdeer-wmde
authored andcommitted
Add number of triples count to the wiki daily metric table (#919)
* Add number of triples count to the wiki daily metric table * use the queryservice namespace entry point to count the triples * fix tests
1 parent a88bd41 commit 0363fbe

4 files changed

Lines changed: 144 additions & 9 deletions

File tree

app/Metrics/App/WikiMetrics.php

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace App\Metrics\App;
44

5+
use App\QueryserviceNamespace;
56
use App\Wiki;
67
use App\WikiDailyMetrics;
78
use Illuminate\Support\Arr;
9+
use Illuminate\Support\Facades\Http;
10+
use Illuminate\Support\Facades\Log;
811

912
class WikiMetrics
1013
{
@@ -21,6 +24,7 @@ public function saveMetrics(Wiki $wiki): void
2124

2225
$today = now()->format('Y-m-d');
2326
$oldRecord = WikiDailyMetrics::where('wiki_id', $wiki->id)->latest('date')->first();
27+
$tripleCount = $this->getNumOfTriples();
2428
$todayPageCount = $wiki->wikiSiteStats()->first()->pages ?? 0;
2529
$isDeleted = (bool)$wiki->deleted_at;
2630

@@ -35,37 +39,62 @@ public function saveMetrics(Wiki $wiki): void
3539
'is_deleted' => $isDeleted,
3640
'date' => $today,
3741
'wiki_id' => $wiki->id,
38-
'daily_actions'=> $dailyActions,
39-
'weekly_actions'=> $weeklyActions,
40-
'monthly_actions'=> $monthlyActions,
41-
'quarterly_actions'=> $quarterlyActions,
42+
'number_of_triples' => $tripleCount,
43+
'daily_actions' => $dailyActions,
44+
'weekly_actions' => $weeklyActions,
45+
'monthly_actions' => $monthlyActions,
46+
'quarterly_actions' => $quarterlyActions,
4247
]);
4348

4449
// compare current record to old record and only save if there is a change
4550
if ($oldRecord) {
4651
if ($oldRecord->is_deleted) {
47-
\Log::info("Wiki is deleted, no new record for WikiMetrics ID {$wiki->id}.");
52+
Log::info("Wiki is deleted, no new record for Wiki ID {$wiki->id}.");
4853
return;
4954
}
5055
if (!$isDeleted) {
5156
if ($oldRecord->areMetricsEqual($dailyMetrics)) {
52-
\Log::info("Record unchanged for WikiMetrics ID {$wiki->id}, no new record added.");
57+
Log::info("Record unchanged for Wiki ID {$wiki->id}, no new record added.");
5358
return;
5459
}
5560
}
5661
}
5762

5863
$dailyMetrics->save();
5964

60-
\Log::info("New metric recorded for Wiki ID {$wiki->id}");
65+
Log::info("New metric recorded for Wiki ID {$wiki->id}");
66+
}
67+
protected function getNumOfTriples(): ?int
68+
{
69+
$qsNamespace = QueryserviceNamespace::whereWikiId($this->wiki->id)->first();
70+
71+
if( !$qsNamespace ) {
72+
Log::info( new \RuntimeException("Namespace for wiki {$this->wiki->id} not found.") );
73+
return null;
74+
}
75+
76+
$endpoint = $qsNamespace->backend . '/bigdata/namespace/' . $qsNamespace->namespace. '/sparql';
77+
$query = 'SELECT (COUNT(*) AS ?triples) WHERE { ?s ?p ?o }';
78+
79+
$response = Http::withHeaders([
80+
'Accept' => 'application/sparql-results+json'
81+
])->get($endpoint, [
82+
'query' => $query
83+
]);
84+
85+
if ($response->successful()) {
86+
$data = $response->json();
87+
return $data['results']['bindings'][0]['triples']['value'];
88+
}
89+
return null;
6190
}
6291

6392
protected function getNumberOfActions(string $interval): null|int
6493
{
6594
$actions = null;
6695

6796
// safeguard
68-
if (false === in_array($interval,
97+
if (false === in_array($interval,
6998
[
7099
self::INTERVAL_DAILY,
71100
self::INTERVAL_WEEKLY,

app/WikiDailyMetrics.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class WikiDailyMetrics extends Model
2727
'weekly_actions',
2828
'monthly_actions',
2929
'quarterly_actions',
30+
'number_of_triples',
31+
3032
];
3133

3234
// list of properties which are actual wiki metrics
@@ -36,7 +38,8 @@ class WikiDailyMetrics extends Model
3638
'daily_actions',
3739
'weekly_actions',
3840
'monthly_actions',
39-
'quarterly_actions'
41+
'quarterly_actions',
42+
'number_of_triples',
4043
];
4144

4245
public function areMetricsEqual(WikiDailyMetrics $wikiDailyMetrics): bool
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('wiki_daily_metrics', function (Blueprint $table) {
15+
$table->integer('number_of_triples')->nullable()->default(null);
16+
});
17+
}
18+
19+
/**
20+
* Reverse the migrations.
21+
*/
22+
public function down(): void
23+
{
24+
Schema::table('wiki_daily_metrics', function (Blueprint $table) {
25+
$table->dropColumn('number_of_triples');
26+
});
27+
}
28+
};

tests/Metrics/WikiMetricsTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
namespace Tests\Metrics;
44

55
use App\Metrics\App\WikiMetrics;
6+
use App\QueryserviceNamespace;
67
use App\Wiki;
78
use App\WikiDb;
89
use App\WikiDailyMetrics;
910
use App\Jobs\ProvisionWikiDbJob;
1011
use Carbon\Carbon;
1112
use Illuminate\Foundation\Testing\RefreshDatabase;
13+
use Illuminate\Support\Facades\DB;
14+
use Illuminate\Support\Facades\Http;
1215
use Tests\TestCase;
1316

1417
class WikiMetricsTest extends TestCase
@@ -95,5 +98,77 @@ public function testAddRecordsWikiIsDeleted()
9598
'date' => now()->toDateString()
9699
]);
97100
}
101+
public function testItSaveTripleCountSuccessfully()
102+
{
103+
$wiki = Wiki::factory()->create([
104+
'domain' => 'somewikiforunittest.wikibase.cloud'
105+
]);
106+
$wikiDb = WikiDb::first();
107+
$wikiDb->update( ['wiki_id' => $wiki->id] );
108+
$namespace = 'asdf';
109+
$host = config('app.queryservice_host');
110+
111+
$dbRow = QueryserviceNamespace::create([
112+
'namespace' => $namespace,
113+
'backend' => $host,
114+
]);
115+
116+
DB::table('queryservice_namespaces')->where(['id'=>$dbRow->id])->limit(1)->update(['wiki_id' => $wiki->id]);
117+
WikiDailyMetrics::create([
118+
'id' => $wiki->id. '_'. Carbon::yesterday()->toDateString(),
119+
'wiki_id' => $wiki->id,
120+
'date' => Carbon::yesterday()->toDateString(),
121+
'pages' => 0,
122+
'is_deleted' => 0
123+
]);
124+
Http::fake([
125+
'*' => Http::response([
126+
'results' => [
127+
'bindings' => [
128+
[
129+
'triples' => ['type' => 'literal', 'value' => '12345']
130+
]
131+
]
132+
]
133+
], 200)
134+
]);
135+
(new WikiMetrics())->saveMetrics($wiki);
136+
$this->assertDatabaseHas('wiki_daily_metrics', [
137+
'wiki_id' => $wiki->id,
138+
'number_of_triples' => 12345
139+
]);
140+
}
141+
public function testSaveNullForFailedRequestOfTriplesCount()
142+
{
143+
$wiki = Wiki::factory()->create([
144+
'domain' => 'somewikitest.wikibase.cloud'
145+
]);
146+
$wikiDb = WikiDb::first();
147+
$wikiDb->update( ['wiki_id' => $wiki->id] );
148+
$namespace = 'asdf';
149+
$host = config('app.queryservice_host');
150+
151+
$dbRow = QueryserviceNamespace::create([
152+
'namespace' => $namespace,
153+
'backend' => $host,
154+
]);
155+
156+
DB::table('queryservice_namespaces')->where(['id'=>$dbRow->id])->limit(1)->update(['wiki_id' => $wiki->id]);
157+
WikiDailyMetrics::create([
158+
'id' => $wiki->id. '_'. Carbon::yesterday()->toDateString(),
159+
'wiki_id' => $wiki->id,
160+
'date' => Carbon::yesterday()->toDateString(),
161+
'pages' => 0,
162+
'is_deleted' => 0
163+
]);
164+
Http::fake([
165+
'*' => Http::response('Error', 500)
166+
]);
167+
(new WikiMetrics())->saveMetrics($wiki);
168+
$this->assertDatabaseHas('wiki_daily_metrics', [
169+
'wiki_id' => $wiki->id,
170+
'number_of_triples' => null
171+
]);
172+
}
98173
}
99174

0 commit comments

Comments
 (0)