Skip to content

Commit e9f90ed

Browse files
author
Frederik Ring
committed
feat: calculate rates of user created
1 parent 9173187 commit e9f90ed

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

app/Jobs/PlatformStatsSummaryJob.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Jobs;
44

55
use App\Wiki;
6+
use App\User;
67
use Illuminate\Database\DatabaseManager;
78
use PDO;
89
use Carbon\Carbon;
@@ -39,21 +40,33 @@ private function isNullOrEmpty( $value ): bool {
3940
return is_null($value) || intVal($value) === 0;
4041
}
4142

42-
public function prepareStats( array $allStats, $wikis ): array {
43+
public function prepareStats( array $allStats, array $wikis, array $users ): array {
4344

4445
$deletedWikis = [];
4546
$activeWikis = [];
4647
$inactive = [];
4748
$emptyWikis = [];
49+
$nonDeletedStats = [];
4850
$createdWikis = [];
51+
$createdUsers = [];
4952
foreach ($this->creationRanges as $range) {
5053
$createdWikis[$range] = [];
54+
$createdUsers[$range] = [];
5155
}
52-
$nonDeletedStats = [];
5356

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

60+
foreach ( $users as $user ) {
61+
$createdAt = new Carbon($user->created_at);
62+
foreach ($createdUsers as $range=>$matches) {
63+
$lookback = new \DateInterval($range);
64+
if ($createdAt >= $now->clone()->sub($lookback)) {
65+
$createdUsers[$range][] = $user;
66+
}
67+
}
68+
}
69+
5770
foreach( $wikis as $wiki ) {
5871

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

145+
foreach ($createdUsers as $range=>$items) {
146+
$result['users_created_'.$range] = count($items);
147+
}
148+
132149
return $result;
133150
}
134151

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

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

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

166184
$manager->purge('mw');
167185
$manager->purge('mysql');

tests/Jobs/PlatformStatsSummaryJobTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,19 @@ public function testGroupings()
145145
],
146146
];
147147

148+
$users = [
149+
[
150+
User::factory()->create([ "created_at" => Carbon::now()->subDays(90)->timestamp ]),
151+
],
152+
[
153+
User::factory()->create([ "created_at" => Carbon::now()->subHours(1)->timestamp ]),
154+
],
155+
[
156+
User::factory()->create([ "created_at" => Carbon::now()->subHours(48)->timestamp ]),
157+
],
158+
];
148159

149-
$groups = $job->prepareStats($stats, $testWikis);
160+
$groups = $job->prepareStats($stats, $testWikis, $users);
150161

151162
$this->assertEquals(
152163
[
@@ -162,6 +173,8 @@ public function testGroupings()
162173
"platform_summary_version" => "v1",
163174
"wikis_created_PT24H" => 1,
164175
"wikis_created_P30D" => 2,
176+
"users_created_PT24H" => 1,
177+
"users_created_P30D" => 2,
165178
],
166179
$groups,
167180
);

0 commit comments

Comments
 (0)