Skip to content

Commit c93c9fb

Browse files
rosalieperdeer-wmde
andcommitted
Add metrics: monthly casual users and active users (#936)
* add DB migration * Add metrics monthly casual users and active users * add new metrics to WikiDailyMetrics::$metricNames as well * fix migration reversal code * update changelog --------- Co-authored-by: dena <dena@wikimedia.de>
1 parent b24e620 commit c93c9fb

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

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.19.6
4+
- Add metrics, monthly casual users and active users
5+
36
## 10x.19.5
47
- Add POST endpoint for wiki profiles
58

app/Metrics/App/WikiMetrics.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public function saveMetrics(Wiki $wiki): void
3333
$monthlyActions = $this->getNumberOfActions(self::INTERVAL_MONTHLY);
3434
$quarterlyActions = $this->getNumberOfActions(self::INTERVAL_QUARTERLY);
3535

36+
$monthlyNumberOfUsersPerActivityType = $this->getNumberOfUsersPerActivityType();
37+
3638
$dailyMetrics = new WikiDailyMetrics([
3739
'id' => $wiki->id . '_' . date('Y-m-d'),
3840
'pages' => $todayPageCount,
@@ -44,6 +46,8 @@ public function saveMetrics(Wiki $wiki): void
4446
'weekly_actions' => $weeklyActions,
4547
'monthly_actions' => $monthlyActions,
4648
'quarterly_actions' => $quarterlyActions,
49+
'monthly_casual_users' => $monthlyNumberOfUsersPerActivityType[0],
50+
'monthly_active_users' => $monthlyNumberOfUsersPerActivityType[1],
4751
]);
4852

4953
// compare current record to old record and only save if there is a change
@@ -130,4 +134,41 @@ protected function getNumberOfActions(string $interval): null|int
130134

131135
return $actions;
132136
}
137+
138+
private function getNumberOfUsersPerActivityType() : array
139+
{
140+
$wikiDb = $this->wiki->wikiDb;
141+
$tableRecentChanges = $wikiDb->name . '.' . $wikiDb->prefix . '_recentchanges';
142+
$tableActor = $wikiDb->name . '.' . $wikiDb->prefix . '_actor';
143+
$query = "SELECT
144+
COUNT(CASE WHEN activity_count >= 1 AND activity_count < 5 THEN 1 END) AS monthly_casual_users,
145+
COUNT(CASE WHEN activity_count >= 5 THEN 1 END) AS monthly_active_users
146+
FROM (
147+
SELECT
148+
rc.rc_actor,
149+
COUNT(*) AS activity_count
150+
FROM
151+
$tableRecentChanges AS rc
152+
INNER JOIN $tableActor AS a ON rc.rc_actor = a.actor_id
153+
WHERE rc.rc_timestamp >= DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 MONTH), '%Y%m%d%H%i%S') -- monthly
154+
/*
155+
Conditions below added for consistency with Wikidata: https://phabricator.wikimedia.org/diffusion/ADES/browse/master/src/wikidata/site_stats/sql/active_user_changes.sql
156+
*/
157+
AND a.actor_user != 0
158+
AND rc.rc_bot = 0
159+
AND (rc.rc_log_type != 'newusers' OR rc.rc_log_type IS NULL)
160+
GROUP BY rc.rc_actor
161+
) AS actor_activity";
162+
163+
$manager = app()->db;
164+
$manager->purge('mw');
165+
$conn = $manager->connection('mw');
166+
$pdo = $conn->getPdo();
167+
$result = $pdo->query($query)->fetch();
168+
169+
return [
170+
Arr::get($result, 'monthly_casual_users', null),
171+
Arr::get($result, 'monthly_active_users',null)
172+
];
173+
}
133174
}

app/WikiDailyMetrics.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class WikiDailyMetrics extends Model
2828
'monthly_actions',
2929
'quarterly_actions',
3030
'number_of_triples',
31+
'monthly_casual_users',
32+
'monthly_active_users',
3133

3234
];
3335

@@ -40,6 +42,8 @@ class WikiDailyMetrics extends Model
4042
'monthly_actions',
4143
'quarterly_actions',
4244
'number_of_triples',
45+
'monthly_casual_users',
46+
'monthly_active_users',
4347
];
4448

4549
public function areMetricsEqual(WikiDailyMetrics $wikiDailyMetrics): bool
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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('monthly_casual_users')->nullable();
16+
$table->integer('monthly_active_users')->nullable();
17+
});
18+
}
19+
20+
/**
21+
* Reverse the migrations.
22+
*/
23+
public function down(): void
24+
{
25+
Schema::table('wiki_daily_metrics', function (Blueprint $table) {
26+
$table->dropColumn('monthly_casual_users');
27+
$table->dropColumn('monthly_active_users');
28+
});
29+
}
30+
};

0 commit comments

Comments
 (0)