Skip to content

Commit

Permalink
feat: calculate rates of user created
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Jun 29, 2023
1 parent 9173187 commit 2a71eb1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
24 changes: 21 additions & 3 deletions app/Jobs/PlatformStatsSummaryJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Jobs;

use App\Wiki;
use App\User;
use Illuminate\Database\DatabaseManager;
use PDO;
use Carbon\Carbon;
Expand Down Expand Up @@ -39,21 +40,33 @@ private function isNullOrEmpty( $value ): bool {
return is_null($value) || intVal($value) === 0;
}

public function prepareStats( array $allStats, $wikis ): array {
public function prepareStats( array $allStats, $wikis, $users ): array {

$deletedWikis = [];
$activeWikis = [];
$inactive = [];
$emptyWikis = [];
$nonDeletedStats = [];
$createdWikis = [];
$createdUsers = [];
foreach ($this->creationRanges as $range) {
$createdWikis[$range] = [];
$createdUsers[$range] = [];
}
$nonDeletedStats = [];

$now = Carbon::now();
$currentTime = $now->timestamp;

foreach ( $users as $user ) {
$createdAt = new Carbon($user->created_at);
foreach ($createdUsers as $range=>$matches) {
$lookback = new \DateInterval($range);
if ($createdAt >= $now->clone()->sub($lookback)) {
$createdUsers[$range][] = $user;
}
}
}

foreach( $wikis as $wiki ) {

if( !is_null($wiki->deleted_at) ) {
Expand Down Expand Up @@ -129,12 +142,17 @@ public function prepareStats( array $allStats, $wikis ): array {
$result['wikis_created_'.$range] = count($items);
}

foreach ($createdUsers as $range=>$items) {
$result['users_created_'.$range] = count($items);
}

return $result;
}

public function handle( DatabaseManager $manager ): void
{
$wikis = Wiki::withTrashed()->with('wikidb')->get();
$users = User::all();

$manager->purge('mw');
$manager->purge('mysql');
Expand All @@ -161,7 +179,7 @@ public function handle( DatabaseManager $manager ): void

// use mw PDO to talk to mediawiki dbs
$allStats = $mediawikiPdo->query($query)->fetchAll(PDO::FETCH_ASSOC);
$summary = $this->prepareStats( $allStats, $wikis );
$summary = $this->prepareStats( $allStats, $wikis, $users );

$manager->purge('mw');
$manager->purge('mysql');
Expand Down
15 changes: 14 additions & 1 deletion tests/Jobs/PlatformStatsSummaryJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,19 @@ public function testGroupings()
],
];

$users = [
[
User::factory()->create([ "created_at" => Carbon::now()->subDays(90)->timestamp ]),
],
[
User::factory()->create([ "created_at" => Carbon::now()->subHours(1)->timestamp ]),
],
[
User::factory()->create([ "created_at" => Carbon::now()->subHours(48)->timestamp ]),
],
];

$groups = $job->prepareStats($stats, $testWikis);
$groups = $job->prepareStats($stats, $testWikis, $users);

$this->assertEquals(
[
Expand All @@ -162,6 +173,8 @@ public function testGroupings()
"platform_summary_version" => "v1",
"wikis_created_PT24H" => 1,
"wikis_created_P30D" => 2,
"users_created_PT24H" => 1,
"users_created_P30D" => 2,
],
$groups,
);
Expand Down

0 comments on commit 2a71eb1

Please sign in to comment.