Skip to content

Commit f2eef36

Browse files
committed
feat(platform-stats): provide number of recently created wikis
1 parent ecc032f commit f2eef36

File tree

3 files changed

+47
-28
lines changed

3 files changed

+47
-28
lines changed

app/Jobs/PlatformStatsSummaryJob.php

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
use PDO;
88
use Carbon\Carbon;
99
use Illuminate\Support\Facades\Log;
10-
use Illuminate\Notifications\Notifiable;
11-
use App\Notifications\PlatformStatsSummaryNotification;
1210
use Illuminate\Support\Facades\Config;
13-
use Illuminate\Support\Facades\Notification;
1411
use Illuminate\Support\Facades\App;
1512

1613
/*
@@ -30,10 +27,12 @@
3027
class PlatformStatsSummaryJob extends Job
3128
{
3229
private $inactiveThreshold;
30+
private $newRanges;
3331

3432
private $platformSummaryStatsVersion = "v1";
3533
public function __construct() {
3634
$this->inactiveThreshold = Config::get('wbstack.platform_summary_inactive_threshold');
35+
$this->newRanges = Config::get('wbstack.platform_summary_new_ranges');
3736
}
3837

3938
private function isNullOrEmpty( $value ): bool {
@@ -43,12 +42,17 @@ private function isNullOrEmpty( $value ): bool {
4342
public function prepareStats( array $allStats, $wikis ): array {
4443

4544
$deletedWikis = [];
46-
$activeWikis= [];
47-
$inactive = [];
48-
$emptyWikis = [];
49-
45+
$activeWikis = [];
46+
$inactive = [];
47+
$emptyWikis = [];
48+
$newWikis = [];
49+
foreach ($this->newRanges as $range) {
50+
$newWikis[$range] = [];
51+
}
5052
$nonDeletedStats = [];
51-
$currentTime = Carbon::now()->timestamp;
53+
54+
$now = Carbon::now();
55+
$currentTime = $now->timestamp;
5256

5357
foreach( $wikis as $wiki ) {
5458

@@ -57,10 +61,17 @@ public function prepareStats( array $allStats, $wikis ): array {
5761
continue;
5862
}
5963

64+
$createdAt = new Carbon($wiki->created_at);
65+
foreach ($newWikis as $range=>$matches) {
66+
$lookback = new \DateInterval($range);
67+
if ($createdAt >= $now->clone()->sub($lookback)) {
68+
$newWikis[$range][] = $wiki;
69+
}
70+
}
71+
6072
$wikiDb = $wiki->wikiDb()->first();
6173

6274
if( !$wikiDb ) {
63-
6475
Log::error(__METHOD__ . ": Could not find WikiDB for {$wiki->domain}");
6576
continue;
6677
}
@@ -71,7 +82,7 @@ public function prepareStats( array $allStats, $wikis ): array {
7182
Log::warning(__METHOD__ . ": Could not find stats for {$wiki->domain}");
7283
continue;
7384
}
74-
85+
7586
$stats = $allStats[$found_key];
7687

7788
// is it empty?
@@ -86,7 +97,7 @@ public function prepareStats( array $allStats, $wikis ): array {
8697
if(!is_null($stats['lastEdit'])){
8798
$lastTimestamp = intVal($stats['lastEdit']);
8899
$diff = $currentTime - $lastTimestamp;
89-
100+
90101
if ($diff >= $this->inactiveThreshold) {
91102
$inactive[] = $wiki;
92103
continue;
@@ -95,13 +106,13 @@ public function prepareStats( array $allStats, $wikis ): array {
95106

96107
$activeWikis[] = $wiki;
97108
}
98-
109+
99110
$totalNonDeletedUsers = array_sum(array_column($nonDeletedStats, 'users'));
100111
$totalNonDeletedActiveUsers = array_sum(array_column($nonDeletedStats, 'active_users'));
101112
$totalNonDeletedPages = array_sum(array_column($nonDeletedStats, 'pages'));
102113
$totalNonDeletedEdits = array_sum(array_column($nonDeletedStats, 'edits'));
103114

104-
return [
115+
$result = [
105116
'platform_summary_version' => $this->platformSummaryStatsVersion,
106117
'total' => count($wikis),
107118
'deleted' => count($deletedWikis),
@@ -113,6 +124,12 @@ public function prepareStats( array $allStats, $wikis ): array {
113124
'total_non_deleted_pages' => $totalNonDeletedPages,
114125
'total_non_deleted_edits' => $totalNonDeletedEdits,
115126
];
127+
128+
foreach ($newWikis as $range=>$items) {
129+
$result['new_wikis_'.$range] = count($items);
130+
}
131+
132+
return $result;
116133
}
117134

118135
public function handle( DatabaseManager $manager ): void
@@ -133,11 +150,11 @@ public function handle( DatabaseManager $manager ): void
133150
$mediawikiPdo = $mwConn->getPdo();
134151

135152
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
136-
153+
137154
// prepare the first query
138155
$statement = $pdo->prepare($this->wikiStatsQuery);
139156
$statement->execute();
140-
157+
141158
// produces the stats query
142159
$result = $statement->fetchAll(PDO::FETCH_ASSOC)[0];
143160
$query = array_values($result)[0];
@@ -148,7 +165,7 @@ public function handle( DatabaseManager $manager ): void
148165

149166
$manager->purge('mw');
150167
$manager->purge('mysql');
151-
168+
152169
// Output to be scraped from logs
153170
if( !App::runningUnitTests() ) {
154171
print( json_encode($summary) . PHP_EOL );
@@ -183,7 +200,7 @@ public function handle( DatabaseManager $manager ): void
183200
"
184201
185202
) SEPARATOR ' UNION ALL ')
186-
203+
187204
FROM apidb.wiki_dbs
188205
LEFT JOIN apidb.wikis ON wiki_dbs.wiki_id = wikis.id;
189206

config/wbstack.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
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_new_ranges' => explode(',', env('WBSTACK_SUMMARY_NEW_RANGES', 'PT24H,P30D')),
1516

1617
'elasticsearch_host' => env('ELASTICSEARCH_HOST', false),
1718
'elasticsearch_enabled_by_default' => env('WBSTACK_ELASTICSEARCH_ENABLED_BY_DEFAULT', false),

tests/Jobs/PlatformStatsSummaryJobTest.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,12 @@ public function testGroupings()
8282

8383
$job = new PlatformStatsSummaryJob();
8484
$job->setJob($mockJob);
85-
86-
$testWikis = [
87-
Wiki::factory()->create( [ 'deleted_at' => null, 'domain' => 'wiki1.com' ] ),
88-
Wiki::factory()->create( [ 'deleted_at' => null, 'domain' => 'wiki2.com' ] ),
89-
Wiki::factory()->create( [ 'deleted_at' => Carbon::now()->subDays(90)->timestamp, 'domain' => 'wiki3.com' ] ),
90-
Wiki::factory()->create( [ 'deleted_at' => null, 'domain' => 'wiki4.com' ] )
9185

86+
$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 ] )
9291
];
9392

9493
foreach($testWikis as $wiki) {
@@ -99,7 +98,7 @@ public function testGroupings()
9998
'version' => 'asdasdasdas',
10099
'prefix' => 'asdasd',
101100
'wiki_id' => $wiki->id
102-
]);
101+
]);
103102
}
104103
$stats = [
105104
[ // inactive
@@ -145,10 +144,10 @@ public function testGroupings()
145144
"platform_summary_version" => "v1"
146145
],
147146
];
148-
147+
149148

150149
$groups = $job->prepareStats($stats, $testWikis);
151-
150+
152151
$this->assertEquals(
153152
[
154153
"total" => 4,
@@ -160,9 +159,11 @@ public function testGroupings()
160159
"total_non_deleted_active_users" => 1,
161160
"total_non_deleted_pages" => 2,
162161
"total_non_deleted_edits" => 1,
163-
"platform_summary_version" => "v1"
162+
"platform_summary_version" => "v1",
163+
"new_wikis_PT24H" => 1,
164+
"new_wikis_P30D" => 2,
164165
],
165-
$groups,
166+
$groups,
166167
);
167168
}
168169

0 commit comments

Comments
 (0)