28
28
class PlatformStatsSummaryJob extends Job
29
29
{
30
30
private $ inactiveThreshold ;
31
- private $ creationRanges ;
31
+ private $ creationRateRanges ;
32
32
33
33
private $ platformSummaryStatsVersion = "v1 " ;
34
34
public function __construct () {
35
35
$ 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 ' );
37
37
}
38
38
39
39
private function isNullOrEmpty ( $ value ): bool {
40
40
return is_null ($ value ) || intVal ($ value ) === 0 ;
41
41
}
42
42
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 {
44
57
45
58
$ deletedWikis = [];
46
59
$ activeWikis = [];
47
60
$ inactive = [];
48
61
$ emptyWikis = [];
49
62
$ nonDeletedStats = [];
50
- $ createdWikis = [];
51
- $ createdUsers = [];
52
- foreach ($ this ->creationRanges as $ range ) {
53
- $ createdWikis [$ range ] = [];
54
- $ createdUsers [$ range ] = [];
55
- }
56
63
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 ;
69
65
70
66
foreach ( $ wikis as $ wiki ) {
71
67
@@ -74,14 +70,6 @@ public function prepareStats( array $allStats, $wikis, $users ): array {
74
70
continue ;
75
71
}
76
72
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
-
85
73
$ wikiDb = $ wiki ->wikiDb ()->first ();
86
74
87
75
if ( !$ wikiDb ) {
@@ -125,7 +113,7 @@ public function prepareStats( array $allStats, $wikis, $users ): array {
125
113
$ totalNonDeletedPages = array_sum (array_column ($ nonDeletedStats , 'pages ' ));
126
114
$ totalNonDeletedEdits = array_sum (array_column ($ nonDeletedStats , 'edits ' ));
127
115
128
- $ result = [
116
+ return [
129
117
'platform_summary_version ' => $ this ->platformSummaryStatsVersion ,
130
118
'total ' => count ($ wikis ),
131
119
'deleted ' => count ($ deletedWikis ),
@@ -137,22 +125,11 @@ public function prepareStats( array $allStats, $wikis, $users ): array {
137
125
'total_non_deleted_pages ' => $ totalNonDeletedPages ,
138
126
'total_non_deleted_edits ' => $ totalNonDeletedEdits ,
139
127
];
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 ;
150
128
}
151
129
152
130
public function handle ( DatabaseManager $ manager ): void
153
131
{
154
132
$ wikis = Wiki::withTrashed ()->with ('wikidb ' )->get ();
155
- $ users = User::all ();
156
133
157
134
$ manager ->purge ('mw ' );
158
135
$ manager ->purge ('mysql ' );
@@ -179,7 +156,10 @@ public function handle( DatabaseManager $manager ): void
179
156
180
157
// use mw PDO to talk to mediawiki dbs
181
158
$ 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 );
183
163
184
164
$ manager ->purge ('mw ' );
185
165
$ manager ->purge ('mysql ' );
0 commit comments