2828class 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 ' );
0 commit comments