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 $ 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,11 +42,15 @@ 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 = [];
53+
5154 $ currentTime = Carbon::now ()->timestamp ;
5255
5356 foreach ( $ wikis as $ wiki ) {
@@ -57,10 +60,17 @@ public function prepareStats( array $allStats, $wikis ): array {
5760 continue ;
5861 }
5962
63+ $ createdAt = new Carbon ($ wiki ->created_at );
64+ foreach ($ newWikis as $ range =>$ matches ) {
65+ $ lookback = new \DateInterval ($ range );
66+ if ($ createdAt >= Carbon::now ()->sub ($ lookback )) {
67+ $ newWikis [$ range ][] = $ wiki ;
68+ }
69+ }
70+
6071 $ wikiDb = $ wiki ->wikiDb ()->first ();
6172
6273 if ( !$ wikiDb ) {
63-
6474 Log::error (__METHOD__ . ": Could not find WikiDB for {$ wiki ->domain }" );
6575 continue ;
6676 }
@@ -71,7 +81,7 @@ public function prepareStats( array $allStats, $wikis ): array {
7181 Log::warning (__METHOD__ . ": Could not find stats for {$ wiki ->domain }" );
7282 continue ;
7383 }
74-
84+
7585 $ stats = $ allStats [$ found_key ];
7686
7787 // is it empty?
@@ -86,7 +96,7 @@ public function prepareStats( array $allStats, $wikis ): array {
8696 if (!is_null ($ stats ['lastEdit ' ])){
8797 $ lastTimestamp = intVal ($ stats ['lastEdit ' ]);
8898 $ diff = $ currentTime - $ lastTimestamp ;
89-
99+
90100 if ($ diff >= $ this ->inactiveThreshold ) {
91101 $ inactive [] = $ wiki ;
92102 continue ;
@@ -95,13 +105,13 @@ public function prepareStats( array $allStats, $wikis ): array {
95105
96106 $ activeWikis [] = $ wiki ;
97107 }
98-
108+
99109 $ totalNonDeletedUsers = array_sum (array_column ($ nonDeletedStats , 'users ' ));
100110 $ totalNonDeletedActiveUsers = array_sum (array_column ($ nonDeletedStats , 'active_users ' ));
101111 $ totalNonDeletedPages = array_sum (array_column ($ nonDeletedStats , 'pages ' ));
102112 $ totalNonDeletedEdits = array_sum (array_column ($ nonDeletedStats , 'edits ' ));
103113
104- return [
114+ $ result = [
105115 'platform_summary_version ' => $ this ->platformSummaryStatsVersion ,
106116 'total ' => count ($ wikis ),
107117 'deleted ' => count ($ deletedWikis ),
@@ -113,6 +123,12 @@ public function prepareStats( array $allStats, $wikis ): array {
113123 'total_non_deleted_pages ' => $ totalNonDeletedPages ,
114124 'total_non_deleted_edits ' => $ totalNonDeletedEdits ,
115125 ];
126+
127+ foreach ($ newWikis as $ range =>$ items ) {
128+ $ result ['new_wikis_ ' .$ range ] = count ($ items );
129+ }
130+
131+ return $ result ;
116132 }
117133
118134 public function handle ( DatabaseManager $ manager ): void
@@ -133,11 +149,11 @@ public function handle( DatabaseManager $manager ): void
133149 $ mediawikiPdo = $ mwConn ->getPdo ();
134150
135151 $ pdo ->setAttribute (PDO ::ATTR_EMULATE_PREPARES , 1 );
136-
152+
137153 // prepare the first query
138154 $ statement = $ pdo ->prepare ($ this ->wikiStatsQuery );
139155 $ statement ->execute ();
140-
156+
141157 // produces the stats query
142158 $ result = $ statement ->fetchAll (PDO ::FETCH_ASSOC )[0 ];
143159 $ query = array_values ($ result )[0 ];
@@ -148,7 +164,7 @@ public function handle( DatabaseManager $manager ): void
148164
149165 $ manager ->purge ('mw ' );
150166 $ manager ->purge ('mysql ' );
151-
167+
152168 // Output to be scraped from logs
153169 if ( !App::runningUnitTests () ) {
154170 print ( json_encode ($ summary ) . PHP_EOL );
@@ -183,7 +199,7 @@ public function handle( DatabaseManager $manager ): void
183199"
184200
185201) SEPARATOR ' UNION ALL ')
186-
202+
187203 FROM apidb.wiki_dbs
188204 LEFT JOIN apidb.wikis ON wiki_dbs.wiki_id = wikis.id;
189205
0 commit comments