33namespace App \Jobs ;
44
55use App \Wiki ;
6+ use App \User ;
67use Illuminate \Database \DatabaseManager ;
78use PDO ;
89use Carbon \Carbon ;
910use Illuminate \Support \Facades \Log ;
10- use Illuminate \Notifications \Notifiable ;
11- use App \Notifications \PlatformStatsSummaryNotification ;
1211use Illuminate \Support \Facades \Config ;
13- use Illuminate \Support \Facades \Notification ;
1412use Illuminate \Support \Facades \App ;
1513
1614/*
3028class PlatformStatsSummaryJob extends Job
3129{
3230 private $ inactiveThreshold ;
31+ private $ creationRateRanges ;
3332
3433 private $ platformSummaryStatsVersion = "v1 " ;
3534 public function __construct () {
3635 $ this ->inactiveThreshold = Config::get ('wbstack.platform_summary_inactive_threshold ' );
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 ): 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+ }
4455
45- $ deletedWikis = [];
46- $ activeWikis = [];
47- $ inactive = [];
48- $ emptyWikis = [];
56+ public function prepareStats ( array $ allStats , $ wikis ): array {
4957
58+ $ deletedWikis = [];
59+ $ activeWikis = [];
60+ $ inactive = [];
61+ $ emptyWikis = [];
5062 $ nonDeletedStats = [];
63+
5164 $ currentTime = Carbon::now ()->timestamp ;
5265
5366 foreach ( $ wikis as $ wiki ) {
@@ -60,7 +73,6 @@ public function prepareStats( array $allStats, $wikis ): array {
6073 $ wikiDb = $ wiki ->wikiDb ()->first ();
6174
6275 if ( !$ wikiDb ) {
63-
6476 Log::error (__METHOD__ . ": Could not find WikiDB for {$ wiki ->domain }" );
6577 continue ;
6678 }
@@ -71,7 +83,7 @@ public function prepareStats( array $allStats, $wikis ): array {
7183 Log::warning (__METHOD__ . ": Could not find stats for {$ wiki ->domain }" );
7284 continue ;
7385 }
74-
86+
7587 $ stats = $ allStats [$ found_key ];
7688
7789 // is it empty?
@@ -86,7 +98,7 @@ public function prepareStats( array $allStats, $wikis ): array {
8698 if (!is_null ($ stats ['lastEdit ' ])){
8799 $ lastTimestamp = intVal ($ stats ['lastEdit ' ]);
88100 $ diff = $ currentTime - $ lastTimestamp ;
89-
101+
90102 if ($ diff >= $ this ->inactiveThreshold ) {
91103 $ inactive [] = $ wiki ;
92104 continue ;
@@ -95,7 +107,7 @@ public function prepareStats( array $allStats, $wikis ): array {
95107
96108 $ activeWikis [] = $ wiki ;
97109 }
98-
110+
99111 $ totalNonDeletedUsers = array_sum (array_column ($ nonDeletedStats , 'users ' ));
100112 $ totalNonDeletedActiveUsers = array_sum (array_column ($ nonDeletedStats , 'active_users ' ));
101113 $ totalNonDeletedPages = array_sum (array_column ($ nonDeletedStats , 'pages ' ));
@@ -133,11 +145,11 @@ public function handle( DatabaseManager $manager ): void
133145 $ mediawikiPdo = $ mwConn ->getPdo ();
134146
135147 $ pdo ->setAttribute (PDO ::ATTR_EMULATE_PREPARES , 1 );
136-
148+
137149 // prepare the first query
138150 $ statement = $ pdo ->prepare ($ this ->wikiStatsQuery );
139151 $ statement ->execute ();
140-
152+
141153 // produces the stats query
142154 $ result = $ statement ->fetchAll (PDO ::FETCH_ASSOC )[0 ];
143155 $ query = array_values ($ result )[0 ];
@@ -146,9 +158,12 @@ public function handle( DatabaseManager $manager ): void
146158 $ allStats = $ mediawikiPdo ->query ($ query )->fetchAll (PDO ::FETCH_ASSOC );
147159 $ summary = $ this ->prepareStats ( $ allStats , $ wikis );
148160
161+ $ creationStats = $ this ->getCreationStats ();
162+ $ summary = array_merge ($ summary , $ creationStats );
163+
149164 $ manager ->purge ('mw ' );
150165 $ manager ->purge ('mysql ' );
151-
166+
152167 // Output to be scraped from logs
153168 if ( !App::runningUnitTests () ) {
154169 print ( json_encode ($ summary ) . PHP_EOL );
@@ -183,7 +198,7 @@ public function handle( DatabaseManager $manager ): void
183198"
184199
185200) SEPARATOR ' UNION ALL ')
186-
201+
187202 FROM apidb.wiki_dbs
188203 LEFT JOIN apidb.wikis ON wiki_dbs.wiki_id = wikis.id;
189204
0 commit comments