Skip to content

Commit 1e7a363

Browse files
committed
refactor: collect creation rates from db query
1 parent 97317f1 commit 1e7a363

File tree

4 files changed

+57
-61
lines changed

4 files changed

+57
-61
lines changed

app/Jobs/PlatformStatsSummaryJob.php

Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,44 +28,40 @@
2828
class PlatformStatsSummaryJob extends Job
2929
{
3030
private $inactiveThreshold;
31-
private $creationRanges;
31+
private $creationRateRanges;
3232

3333
private $platformSummaryStatsVersion = "v1";
3434
public function __construct() {
3535
$this->inactiveThreshold = Config::get('wbstack.platform_summary_inactive_threshold');
36-
$this->creationRanges = Config::get('wbstack.platform_summary_creation_ranges');
36+
$this->creationRateRanges = Config::get('wbstack.platform_summary_creation_rate_ranges');
3737
}
3838

3939
private function isNullOrEmpty( $value ): bool {
4040
return is_null($value) || intVal($value) === 0;
4141
}
4242

43-
public function prepareStats( array $allStats, $wikis, $users ): array {
43+
public function getCreationStats(): array {
44+
$result = [];
45+
$now = Carbon::now();
46+
foreach ($this->creationRateRanges as $range) {
47+
$limit = $now->clone()->sub(new \DateInterval($range));
48+
$wikis = Wiki::where('created_at', '>=', $limit)->count();
49+
$result['wikis_created_'.$range] = $wikis;
50+
$users = User::where('created_at', '>=', $limit)->count();
51+
$result['users_created_'.$range] = $users;
52+
}
53+
return $result;
54+
}
55+
56+
public function prepareStats( array $allStats, $wikis): array {
4457

4558
$deletedWikis = [];
4659
$activeWikis = [];
4760
$inactive = [];
4861
$emptyWikis = [];
4962
$nonDeletedStats = [];
50-
$createdWikis = [];
51-
$createdUsers = [];
52-
foreach ($this->creationRanges as $range) {
53-
$createdWikis[$range] = [];
54-
$createdUsers[$range] = [];
55-
}
5663

57-
$now = Carbon::now();
58-
$currentTime = $now->timestamp;
59-
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-
}
64+
$currentTime = Carbon::now()->timestamp;
6965

7066
foreach( $wikis as $wiki ) {
7167

@@ -74,14 +70,6 @@ public function prepareStats( array $allStats, $wikis, $users ): array {
7470
continue;
7571
}
7672

77-
$createdAt = new Carbon($wiki->created_at);
78-
foreach ($createdWikis as $range=>$matches) {
79-
$lookback = new \DateInterval($range);
80-
if ($createdAt >= $now->clone()->sub($lookback)) {
81-
$createdWikis[$range][] = $wiki;
82-
}
83-
}
84-
8573
$wikiDb = $wiki->wikiDb()->first();
8674

8775
if( !$wikiDb ) {
@@ -125,7 +113,7 @@ public function prepareStats( array $allStats, $wikis, $users ): array {
125113
$totalNonDeletedPages = array_sum(array_column($nonDeletedStats, 'pages'));
126114
$totalNonDeletedEdits = array_sum(array_column($nonDeletedStats, 'edits'));
127115

128-
$result = [
116+
return [
129117
'platform_summary_version' => $this->platformSummaryStatsVersion,
130118
'total' => count($wikis),
131119
'deleted' => count($deletedWikis),
@@ -137,22 +125,11 @@ public function prepareStats( array $allStats, $wikis, $users ): array {
137125
'total_non_deleted_pages' => $totalNonDeletedPages,
138126
'total_non_deleted_edits' => $totalNonDeletedEdits,
139127
];
140-
141-
foreach ($createdWikis as $range=>$items) {
142-
$result['wikis_created_'.$range] = count($items);
143-
}
144-
145-
foreach ($createdUsers as $range=>$items) {
146-
$result['users_created_'.$range] = count($items);
147-
}
148-
149-
return $result;
150128
}
151129

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

157134
$manager->purge('mw');
158135
$manager->purge('mysql');
@@ -179,7 +156,10 @@ public function handle( DatabaseManager $manager ): void
179156

180157
// use mw PDO to talk to mediawiki dbs
181158
$allStats = $mediawikiPdo->query($query)->fetchAll(PDO::FETCH_ASSOC);
182-
$summary = $this->prepareStats( $allStats, $wikis, $users );
159+
$summary = $this->prepareStats( $allStats, $wikis );
160+
161+
$creationStats = $this->getCreationStats();
162+
$summary = array_merge($summary, $creationStats);
183163

184164
$manager->purge('mw');
185165
$manager->purge('mysql');

config/wbstack.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
'wiki_max_per_user' => env('WBSTACK_MAX_PER_USER', false),
1313

1414
'platform_summary_inactive_threshold' => env('WBSTACK_SUMMARY_INACTIVE_THRESHOLD', 60 * 60 * 24 * 90),
15-
'platform_summary_creation_ranges' => array_filter(
16-
explode(',', env('WBSTACK_SUMMARY_CREATION_RANGES', '')),
15+
'platform_summary_creation_rate_ranges' => array_filter(
16+
explode(',', env('WBSTACK_SUMMARY_CREATION_RATE_RANGES', '')),
1717
function ($item) { return $item !== ''; }
1818
),
1919

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
<env name="CACHE_DRIVER" value="array"/>
1616
<env name="QUEUE_CONNECTION" value="sync"/>
1717
<env name="PLATFORM_MW_BACKEND_HOST" value="mediawiki-139-app-backend.default.svc.cluster.default"/>
18-
<env name="WBSTACK_SUMMARY_CREATION_RANGES" value="PT24H,P30D"/>
18+
<env name="WBSTACK_SUMMARY_CREATION_RATE_RANGES" value="PT24H,P30D"/>
1919
</php>
2020
</phpunit>

tests/Jobs/PlatformStatsSummaryJobTest.php

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests\Jobs;
44

5+
use Illuminate\Foundation\Testing\RefreshDatabase;
56
use Illuminate\Foundation\Testing\DatabaseTransactions;
67
use Tests\TestCase;
78
use App\User;
@@ -16,7 +17,7 @@
1617

1718
class PlatformStatsSummaryJobTest extends TestCase
1819
{
19-
use DatabaseTransactions;
20+
use RefreshDatabase;
2021

2122
private $numWikis = 5;
2223
private $wikis = [];
@@ -84,10 +85,11 @@ public function testGroupings()
8485
$job->setJob($mockJob);
8586

8687
$testWikis = [
87-
Wiki::factory()->create( [ 'deleted_at' => null, 'domain' => 'wiki1.com', 'created_at' => Carbon::now()->subMinutes(4)->timestamp ] ),
88-
Wiki::factory()->create( [ 'deleted_at' => null, 'domain' => 'wiki2.com', 'created_at' => Carbon::now()->subHours(72)->timestamp ] ),
89-
Wiki::factory()->create( [ 'deleted_at' => Carbon::now()->subDays(90)->timestamp, 'domain' => 'wiki3.com', 'created_at' => Carbon::now()->subHours(72)->timestamp ] ),
90-
Wiki::factory()->create( [ 'deleted_at' => null, 'domain' => 'wiki4.com', 'created_at' => Carbon::now()->subYears(6)->timestamp ] )
88+
Wiki::factory()->create( [ 'deleted_at' => null, 'domain' => 'wiki1.com' ] ),
89+
Wiki::factory()->create( [ 'deleted_at' => null, 'domain' => 'wiki2.com' ] ),
90+
Wiki::factory()->create( [ 'deleted_at' => Carbon::now()->subDays(90)->timestamp, 'domain' => 'wiki3.com' ] ),
91+
Wiki::factory()->create( [ 'deleted_at' => null, 'domain' => 'wiki4.com' ] )
92+
9193
];
9294

9395
foreach($testWikis as $wiki) {
@@ -145,13 +147,8 @@ public function testGroupings()
145147
],
146148
];
147149

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

154-
$groups = $job->prepareStats($stats, $testWikis, $users);
151+
$groups = $job->prepareStats($stats, $testWikis);
155152

156153
$this->assertEquals(
157154
[
@@ -164,15 +161,34 @@ public function testGroupings()
164161
"total_non_deleted_active_users" => 1,
165162
"total_non_deleted_pages" => 2,
166163
"total_non_deleted_edits" => 1,
167-
"platform_summary_version" => "v1",
168-
"wikis_created_PT24H" => 1,
169-
"wikis_created_P30D" => 2,
170-
"users_created_PT24H" => 1,
171-
"users_created_P30D" => 2,
164+
"platform_summary_version" => "v1"
172165
],
173166
$groups,
174167
);
175168
}
176169

170+
function testCreationStats() {
171+
$mockJob = $this->createMock(Job::class);
172+
$mockJob->expects($this->never())->method('fail');
173+
174+
$job = new PlatformStatsSummaryJob();
175+
$job->setJob($mockJob);
176+
177+
$testWikis = [];
178+
$testUsers = [];
179+
180+
$stats = $job->getCreationStats();
181+
182+
$this->assertEquals(
183+
[
184+
'wikis_created_PT24H' => 0,
185+
'wikis_created_P30D' => 0,
186+
'users_created_PT24H' => 0,
187+
'users_created_P30D' => 0,
188+
],
189+
$stats,
190+
);
191+
192+
}
177193

178194
}

0 commit comments

Comments
 (0)