22
33namespace App \Metrics \App ;
44
5+ use App \QueryserviceNamespace ;
56use App \Wiki ;
67use App \WikiDailyMetrics ;
78use Illuminate \Support \Arr ;
9+ use Illuminate \Support \Facades \Http ;
10+ use Illuminate \Support \Facades \Log ;
811
912class 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 ,
0 commit comments