7
7
use PDO ;
8
8
use Carbon \Carbon ;
9
9
use Illuminate \Support \Facades \Log ;
10
- use Illuminate \Notifications \Notifiable ;
11
- use App \Notifications \PlatformStatsSummaryNotification ;
12
10
use Illuminate \Support \Facades \Config ;
13
- use Illuminate \Support \Facades \Notification ;
14
11
use Illuminate \Support \Facades \App ;
15
12
16
13
/*
30
27
class PlatformStatsSummaryJob extends Job
31
28
{
32
29
private $ inactiveThreshold ;
30
+ private $ newRanges ;
33
31
34
32
private $ platformSummaryStatsVersion = "v1 " ;
35
33
public function __construct () {
36
34
$ this ->inactiveThreshold = Config::get ('wbstack.platform_summary_inactive_threshold ' );
35
+ $ this ->newRanges = Config::get ('wbstack.platform_summary_new_ranges ' );
37
36
}
38
37
39
38
private function isNullOrEmpty ( $ value ): bool {
@@ -43,12 +42,17 @@ private function isNullOrEmpty( $value ): bool {
43
42
public function prepareStats ( array $ allStats , $ wikis ): array {
44
43
45
44
$ 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
+ }
50
52
$ nonDeletedStats = [];
51
- $ currentTime = Carbon::now ()->timestamp ;
53
+
54
+ $ now = Carbon::now ();
55
+ $ currentTime = $ now ->timestamp ;
52
56
53
57
foreach ( $ wikis as $ wiki ) {
54
58
@@ -57,10 +61,17 @@ public function prepareStats( array $allStats, $wikis ): array {
57
61
continue ;
58
62
}
59
63
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
+
60
72
$ wikiDb = $ wiki ->wikiDb ()->first ();
61
73
62
74
if ( !$ wikiDb ) {
63
-
64
75
Log::error (__METHOD__ . ": Could not find WikiDB for {$ wiki ->domain }" );
65
76
continue ;
66
77
}
@@ -71,7 +82,7 @@ public function prepareStats( array $allStats, $wikis ): array {
71
82
Log::warning (__METHOD__ . ": Could not find stats for {$ wiki ->domain }" );
72
83
continue ;
73
84
}
74
-
85
+
75
86
$ stats = $ allStats [$ found_key ];
76
87
77
88
// is it empty?
@@ -86,7 +97,7 @@ public function prepareStats( array $allStats, $wikis ): array {
86
97
if (!is_null ($ stats ['lastEdit ' ])){
87
98
$ lastTimestamp = intVal ($ stats ['lastEdit ' ]);
88
99
$ diff = $ currentTime - $ lastTimestamp ;
89
-
100
+
90
101
if ($ diff >= $ this ->inactiveThreshold ) {
91
102
$ inactive [] = $ wiki ;
92
103
continue ;
@@ -95,13 +106,13 @@ public function prepareStats( array $allStats, $wikis ): array {
95
106
96
107
$ activeWikis [] = $ wiki ;
97
108
}
98
-
109
+
99
110
$ totalNonDeletedUsers = array_sum (array_column ($ nonDeletedStats , 'users ' ));
100
111
$ totalNonDeletedActiveUsers = array_sum (array_column ($ nonDeletedStats , 'active_users ' ));
101
112
$ totalNonDeletedPages = array_sum (array_column ($ nonDeletedStats , 'pages ' ));
102
113
$ totalNonDeletedEdits = array_sum (array_column ($ nonDeletedStats , 'edits ' ));
103
114
104
- return [
115
+ $ result = [
105
116
'platform_summary_version ' => $ this ->platformSummaryStatsVersion ,
106
117
'total ' => count ($ wikis ),
107
118
'deleted ' => count ($ deletedWikis ),
@@ -113,6 +124,12 @@ public function prepareStats( array $allStats, $wikis ): array {
113
124
'total_non_deleted_pages ' => $ totalNonDeletedPages ,
114
125
'total_non_deleted_edits ' => $ totalNonDeletedEdits ,
115
126
];
127
+
128
+ foreach ($ newWikis as $ range =>$ items ) {
129
+ $ result ['new_wikis_ ' .$ range ] = count ($ items );
130
+ }
131
+
132
+ return $ result ;
116
133
}
117
134
118
135
public function handle ( DatabaseManager $ manager ): void
@@ -133,11 +150,11 @@ public function handle( DatabaseManager $manager ): void
133
150
$ mediawikiPdo = $ mwConn ->getPdo ();
134
151
135
152
$ pdo ->setAttribute (PDO ::ATTR_EMULATE_PREPARES , 1 );
136
-
153
+
137
154
// prepare the first query
138
155
$ statement = $ pdo ->prepare ($ this ->wikiStatsQuery );
139
156
$ statement ->execute ();
140
-
157
+
141
158
// produces the stats query
142
159
$ result = $ statement ->fetchAll (PDO ::FETCH_ASSOC )[0 ];
143
160
$ query = array_values ($ result )[0 ];
@@ -148,7 +165,7 @@ public function handle( DatabaseManager $manager ): void
148
165
149
166
$ manager ->purge ('mw ' );
150
167
$ manager ->purge ('mysql ' );
151
-
168
+
152
169
// Output to be scraped from logs
153
170
if ( !App::runningUnitTests () ) {
154
171
print ( json_encode ($ summary ) . PHP_EOL );
@@ -183,7 +200,7 @@ public function handle( DatabaseManager $manager ): void
183
200
"
184
201
185
202
) SEPARATOR ' UNION ALL ')
186
-
203
+
187
204
FROM apidb.wiki_dbs
188
205
LEFT JOIN apidb.wikis ON wiki_dbs.wiki_id = wikis.id;
189
206
0 commit comments