77use PDO ;
88use Carbon \Carbon ;
99use Illuminate \Support \Facades \Log ;
10- use Illuminate \Notifications \Notifiable ;
11- use App \Notifications \PlatformStatsSummaryNotification ;
1210use Illuminate \Support \Facades \Config ;
13- use Illuminate \Support \Facades \Notification ;
1411use Illuminate \Support \Facades \App ;
1512
1613/*
3027class PlatformStatsSummaryJob extends Job
3128{
3229 private $ inactiveThreshold ;
30+ private $ signupRanges ;
3331
3432 private $ platformSummaryStatsVersion = "v1 " ;
3533 public function __construct () {
3634 $ this ->inactiveThreshold = Config::get ('wbstack.platform_summary_inactive_threshold ' );
35+ $ this ->signupRanges = Config::get ('wbstack.platform_summary_signup_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 ->signupRanges 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
0 commit comments