Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/Metrics/App/WikiMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function saveMetrics(Wiki $wiki): void {
$quarterlyActions = $this->getNumberOfActions(self::INTERVAL_QUARTERLY);
$numberOfEntities = $this->getNumberOfEntities();
$monthlyNumberOfUsersPerActivityType = $this->getNumberOfUsersPerActivityType();
$numberOfUsersPerWiki = $this->getTotalUserCount();

$dailyMetrics = new WikiDailyMetrics([
'id' => $wiki->id . '_' . date('Y-m-d'),
Expand All @@ -54,6 +55,7 @@ public function saveMetrics(Wiki $wiki): void {
'entity_schema_count' => $numberOfEntities['640'],
'monthly_casual_users' => $monthlyNumberOfUsersPerActivityType[0],
'monthly_active_users' => $monthlyNumberOfUsersPerActivityType[1],
'total_user_count' => $numberOfUsersPerWiki,
]);

// compare current record to old record and only save if there is a change
Expand Down Expand Up @@ -210,4 +212,17 @@ private function getNumberOfEntities(): array {

return $result;
}

private function getTotalUserCount() {
$wikiDb = $this->wiki->wikiDb;
$tableUser = "{$wikiDb->name}.{$wikiDb->prefix}_user";
$query = "SELECT COUNT(*) AS total_users FROM $tableUser";
$manager = app()->db;
$manager->purge('mw');
$conn = $manager->connection('mw');
$pdo = $conn->getPdo();
$result = $pdo->query($query)->fetch(PDO::FETCH_ASSOC);

return $result['total_users'] ?? null;
}
}
2 changes: 2 additions & 0 deletions app/WikiDailyMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class WikiDailyMetrics extends Model {
'entity_schema_count',
'monthly_casual_users',
'monthly_active_users',
'total_user_count',

];

Expand All @@ -51,6 +52,7 @@ class WikiDailyMetrics extends Model {
'entity_schema_count',
'monthly_casual_users',
'monthly_active_users',
'total_user_count',
];

public function areMetricsEqual(WikiDailyMetrics $wikiDailyMetrics): bool {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void {
Schema::table('wiki_daily_metrics', function (Blueprint $table) {
$table->integer('total_user_count')->nullable()->default(null);
});
}

/**
* Reverse the migrations.
*/
public function down(): void {
Schema::table('wiki_daily_metrics', function (Blueprint $table) {
$table->dropColumn('total_user_count');
});
}
};
60 changes: 59 additions & 1 deletion tests/Metrics/WikiMetricsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ public function testSavesEntityCountsCorrectly($expectedItemCount, $expectedProp

// clean up after the test
$wiki->forceDelete();
Schema::dropIfExists($tablePage);

$this->assertDatabaseHas('wiki_daily_metrics', [
'wiki_id' => $wiki->id,
Expand All @@ -364,4 +363,63 @@ public function testSavesEntityCountsCorrectly($expectedItemCount, $expectedProp
'entity_schema_count' => $expectedEntitySchemaCount, // redirects should be ignored
]);
}

public function testSavesTotalUserCountCorrectly() {
$wiki = Wiki::factory()->create([
'domain' => 'usercounttest.wikibase.cloud',
]);

$users = [
[
'user_name' => 'user1',
'user_real_name' => 'user1',
'user_password' => 'user1',
'user_new_password' => 'user1',
'user_email' => 'user1@email.com',
'user_touched' => random_bytes(10),
],
[
'user_name' => 'user2',
'user_real_name' => 'user2',
'user_password' => 'user2',
'user_new_password' => 'user2',
'user_email' => 'user2@email.com',
'user_touched' => random_bytes(10),
],
];
$wikiDb = WikiDb::first();
$wikiDb->update(['wiki_id' => $wiki->id]);

$tableUser = $wikiDb->name . '.' . $wikiDb->prefix . '_user';
Schema::dropIfExists($tableUser);
Schema::create($tableUser, function (Blueprint $table) {
$table->increments('user_id');
$table->string('user_name');
$table->string('user_real_name')->default(0);
$table->string('user_password', 255);
$table->string('user_new_password');
$table->string('user_email');
$table->binary('user_touched');
});

// Insert dummy data
DB::table($tableUser)->insert($users);
WikiDailyMetrics::create([
'id' => $wiki->id . '_' . now()->subDay()->toDateString(),
'wiki_id' => $wiki->id,
'date' => now()->subDay()->toDateString(),
'pages' => 0,
'is_deleted' => 0,
]);

(new WikiMetrics)->saveMetrics($wiki);

// clean up after the test
$wiki->forceDelete();

$this->assertDatabaseHas('wiki_daily_metrics', [
'wiki_id' => $wiki->id,
'total_user_count' => count($users),
]);
}
}
Loading