Skip to content

Commit 4d22bee

Browse files
committed
WIP
1 parent 34c74eb commit 4d22bee

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

app/Metrics/App/WikiMetrics.php

+55-4
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,62 @@
44

55
use App\Wiki;
66
use App\WikiDailyMetrics;
7+
use Illuminate\Support\Facades\DB;
8+
use Illuminate\Support\Arr;
79

810
class WikiMetrics
911
{
12+
const INTERVAL_DAILY = 'INTERVAL 1 DAY';
13+
const INTERVAL_WEEKLY = ' INTERVAL 1 WEEK';
14+
const INTERVAL_MONTHLY = 'INTERVAL 1 MONTH';
15+
const INTERVAL_QUARTERLY = 'INTERVAL 3 MONTH';
16+
17+
const QUERY_NUMBER_OF_ACTIONS = <<<EOF
18+
SELECT
19+
SUM(rc_timestamp >= DATE_FORMAT(DATE_SUB(NOW(), ?), '%Y%m%d%H%i%S')) AS sum_actions
20+
FROM
21+
? AS rc
22+
INNER JOIN ? AS a ON rc.rc_actor = a.actor_id
23+
// Conditions below added for consistency with Wikidata: https://phabricator.wikimedia.org/diffusion/ADES/browse/master/src/wikidata/site_stats/sql/active_user_changes.sql
24+
AND a.actor_user != 0
25+
AND rc.rc_bot = 0
26+
AND ( rc.rc_log_type != 'newusers' OR rc.rc_log_type IS NULL)
27+
EOF;
28+
29+
protected $wiki;
30+
1031
public function saveMetrics(Wiki $wiki): void
1132
{
33+
$this->wiki = $wiki;
34+
1235
$today = now()->format('Y-m-d');
1336
$oldRecord = WikiDailyMetrics::where('wiki_id', $wiki->id)->latest('date')->first();
1437
$todayPageCount = $wiki->wikiSiteStats()->first()->pages ?? 0;
1538
$isDeleted = (bool)$wiki->deleted_at;
1639

40+
$dailyActions = $this->getNumberOfActions(self::INTERVAL_DAILY);
41+
$weeklyActions = $this->getNumberOfActions(self::INTERVAL_WEEKLY);
42+
$monthlyActions = $this->getNumberOfActions(self::INTERVAL_MONTHLY);
43+
$quarterlyActions = $this->getNumberOfActions(self::INTERVAL_QUARTERLY);
44+
1745
$dailyMetrics = new WikiDailyMetrics([
1846
'id' => $wiki->id . '_' . date('Y-m-d'),
1947
'pages' => $todayPageCount,
2048
'is_deleted' => $isDeleted,
2149
'date' => $today,
22-
'wiki_id' => $wiki->id
50+
'wiki_id' => $wiki->id,
51+
'daily_actions'=> $dailyActions,
52+
'weekly_actions'=> $weeklyActions,
53+
'monthly_actions'=> $monthlyActions,
54+
'quarterly_actions'=> $quarterlyActions,
2355
]);
24-
56+
57+
// compare current record to old record and only save if there is a change
2558
if ($oldRecord) {
2659
if ($oldRecord->is_deleted) {
2760
\Log::info("Wiki is deleted, no new record for WikiMetrics ID {$wiki->id}.");
2861
return;
2962
}
30-
3163
if (!$isDeleted) {
3264
if ($oldRecord->areMetricsEqual($dailyMetrics)) {
3365
\Log::info("Record unchanged for WikiMetrics ID {$wiki->id}, no new record added.");
@@ -38,6 +70,25 @@ public function saveMetrics(Wiki $wiki): void
3870

3971
$dailyMetrics->save();
4072

41-
\Log::info("New metric recorded for WikiMetrics ID {$wiki->id}");
73+
\Log::info("New metric recorded for Wiki ID {$wiki->id}");
74+
}
75+
76+
protected function getNumberOfActions($interval): mixed
77+
{
78+
$actions = null;
79+
80+
$wikiDb = Wiki::with('wikiDb')->where('id', $this->wiki->id)->first()->wikiDb;
81+
$tableRecentChanges = $wikiDb->name . '.' . $wikiDb->prefix . '_recentchanges';
82+
$tableActor = $wikiDb->name . '.' . $wikiDb->prefix . '_actor';
83+
84+
$result = DB::select(self::QUERY_NUMBER_OF_ACTIONS, [
85+
$interval,
86+
$tableRecentChanges,
87+
$tableActor,
88+
]);
89+
90+
$actions = Arr::get($result, 'sum_actions', null);
91+
92+
return $actions;
4293
}
4394
}

tests/Jobs/UpdateWikiDailyMetricJobTest.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,20 @@ public function testRunJobForAllWikisIncludingDeletedWikis()
3939

4040
$this->assertDatabaseHas('wiki_daily_metrics', [
4141
'wiki_id' => $activeWiki->id,
42-
'date' => Carbon::today()->toDateString()
42+
'date' => Carbon::today()->toDateString(),
43+
'daily_actions' => null,
44+
'weekly_actions' => null,
45+
'monthly_actions' => null,
46+
'quarterly_actions' => null,
4347
]);
4448

4549
$this->assertDatabaseHas('wiki_daily_metrics', [
4650
'wiki_id' => $deletedWiki->id,
47-
'date' => Carbon::today()->toDateString()
51+
'date' => Carbon::today()->toDateString(),
52+
'daily_actions' => null,
53+
'weekly_actions' => null,
54+
'monthly_actions' => null,
55+
'quarterly_actions' => null,
4856
]);
4957
}
5058

0 commit comments

Comments
 (0)