-
Notifications
You must be signed in to change notification settings - Fork 22
/
other-web-services.php
398 lines (352 loc) · 8.34 KB
/
other-web-services.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
/**
* Other web services used by vip-go-ci.
*
* @package Automattic/vip-go-ci
*/
declare(strict_types=1);
/**
* Queue up a message for IRC API,
* or alternatively empty the queue and
* return its contents.
*
* @param string|null $message Message to add to queue, or null if to dump queue.
* @param bool $dump Specify true if to dump queue.
*
* @return bool|array Returns array when dumping results, true when adding message to queue.
*/
function vipgoci_irc_api_alert_queue(
string|null $message = null,
bool $dump = false
) :bool|array {
static $msg_queue = array();
if ( true === $dump ) {
$msg_queue_tmp = $msg_queue;
$msg_queue = array();
return $msg_queue_tmp;
}
$msg_queue[] = $message;
return true;
}
/**
* Remove any sections found in $message string bounded between the
* VIPGOCI_IRC_IGNORE_STRING_START and VIPGOCI_IRC_IGNORE_STRING_END
* constants.
*
* @param string $message Message string to filter.
*
* @return string Message with ignorable strings removed.
*/
function vipgoci_irc_api_filter_ignorable_strings(
string $message
) :string {
do {
$ignore_section_start_pos = strpos( $message, VIPGOCI_IRC_IGNORE_STRING_START );
if ( false !== $ignore_section_start_pos ) {
$ignore_section_end_pos = strpos(
$message,
VIPGOCI_IRC_IGNORE_STRING_END,
0 // From string start; needed for check below.
);
$ignore_section_start_pos_2 = strpos(
$message,
VIPGOCI_IRC_IGNORE_STRING_START,
$ignore_section_start_pos + strlen( VIPGOCI_IRC_IGNORE_STRING_START ) // Needed for check below.
);
} else {
$ignore_section_end_pos = false;
$ignore_section_start_pos_2 = false;
}
if (
( false === $ignore_section_start_pos ) ||
( false === $ignore_section_end_pos )
) {
// Neither string was found, stop processing here.
continue;
}
if ( $ignore_section_end_pos <= $ignore_section_start_pos ) {
// Invalid usage.
vipgoci_log(
'Incorrect usage of VIPGOCI_IRC_IGNORE_STRING_START and VIPGOCI_IRC_IGNORE_STRING_END; former should be placed before the latter',
array(
'message' => $message,
),
0
);
break;
} elseif (
( false !== $ignore_section_start_pos_2 ) &&
( $ignore_section_end_pos > $ignore_section_start_pos_2 )
) {
// Invalid usage.
vipgoci_log(
'Incorrect usage of VIPGOCI_IRC_IGNORE_STRING_START and VIPGOCI_IRC_IGNORE_STRING_END; embedding one ignore string within another is not allowed',
array(
'message' => $message,
),
0
);
break;
} elseif ( $ignore_section_end_pos > $ignore_section_start_pos ) {
// Correct usage; end constant should always come after start constant.
$message = substr_replace(
$message,
'',
$ignore_section_start_pos,
( $ignore_section_end_pos + strlen( VIPGOCI_IRC_IGNORE_STRING_END ) ) -
$ignore_section_start_pos
);
}
} while (
( false !== $ignore_section_start_pos ) &&
( false !== $ignore_section_end_pos )
);
return $message;
}
/**
* Clean IRC ignorable constants away from message specified.
*
* Useful for functions submitting messages to GitHub, were the
* constants should not be part of the HTML submitted.
*
* @param string $message Message to process.
*
* @return string Message with constants removed (if any).
*/
function vipgoci_irc_api_clean_ignorable_constants(
string $message
) :string {
return str_replace(
array(
VIPGOCI_IRC_IGNORE_STRING_START,
VIPGOCI_IRC_IGNORE_STRING_END,
),
array(
'',
'',
),
$message
);
}
/**
* Make messages in IRC queue unique, but add
* a prefix to those messages that were not unique
* indicating how many they were.
*
* @param array $msg_queue Message queue.
*
* @return array New IRC queue, with prefixes as applicable.
*/
function vipgoci_irc_api_alert_queue_unique(
array $msg_queue
) :array {
$msg_queue_unique = array_unique(
$msg_queue
);
/*
* If all messages were unique,
* nothing more to do.
*/
if (
count( $msg_queue ) ===
count( $msg_queue_unique )
) {
return $msg_queue;
}
/*
* Not all unique, count values
*/
$msg_queue_count = array_count_values(
$msg_queue
);
$msg_queue_new = array();
/*
* Add prefix where needed.
*/
foreach ( $msg_queue_count as $msg => $cnt ) {
$msg_prefix = '';
if ( $cnt > 1 ) {
$msg_prefix = '(' . $cnt . 'x) ';
}
$msg_queue_new[] = $msg_prefix . $msg;
}
return $msg_queue_new;
}
/**
* Empty IRC message queue and send off
* to the IRC API.
*
* @param string $irc_api_url URL to IRC API.
* @param string $irc_api_token Access token to IRC API.
* @param string $botname Name of IRC bot.
* @param string $channel Channel to post to.
*
* @return void
*
* @codeCoverageIgnore
*/
function vipgoci_irc_api_alerts_send(
string $irc_api_url,
string $irc_api_token,
string $botname,
string $channel
) :void {
// Get IRC message queue.
$msg_queue = vipgoci_irc_api_alert_queue(
null,
true
);
// Filter away removable strings.
$msg_queue = array_map(
'vipgoci_irc_api_filter_ignorable_strings',
$msg_queue
);
// Ensure all strings we log are unique; if not make unique and add prefix.
$msg_queue = vipgoci_irc_api_alert_queue_unique(
$msg_queue
);
vipgoci_log(
'Sending messages to IRC API',
array(
'msg_queue' => $msg_queue,
)
);
foreach ( $msg_queue as $message ) {
$irc_api_postfields = array(
'message' => $message,
'botname' => $botname,
'channel' => $channel,
);
/*
* Execute query, keep record of how long time it
* took, and keep count of how many requests we do.
*/
vipgoci_runtime_measure( VIPGOCI_RUNTIME_START, 'irc_api_post' );
vipgoci_counter_report(
VIPGOCI_COUNTERS_DO,
'irc_api_request_post',
1
);
vipgoci_http_api_post_url(
$irc_api_url,
$irc_api_postfields,
array( 'bearer' => $irc_api_token ),
false,
true,
CURL_HTTP_VERSION_NONE,
VIPGOCI_HTTP_API_CONTENT_TYPE_APPLICATION_JSON,
0,
VIPGOCI_HTTP_API_SHORT_TIMEOUT
);
vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'irc_api_post' );
/*
* Enforce a small wait between requests.
*/
time_nanosleep( 0, 500000000 );
}
}
/**
* Send statistics to pixel API so
* we can keep track of actions we
* take during runtime.
*
* @param string $pixel_api_url URL to Pixel API.
* @param array $stat_names_to_report Statistics to report, both repository specific and global. Should be associative array.
* @param array $statistics Statistics array, associative array of keys and values.
*
* @return void
*/
function vipgoci_send_stats_to_pixel_api(
string $pixel_api_url,
array $stat_names_to_report,
array $statistics
) :void {
vipgoci_log(
'Sending statistics to pixel API service',
array(
'stat_names_to_report' => $stat_names_to_report,
)
);
$stat_names_to_groups = array();
foreach (
array_keys( $stat_names_to_report ) as
$statistic_group
) {
foreach (
$stat_names_to_report[ $statistic_group ] as $stat_name
) {
$stat_names_to_groups[ $stat_name ][] = $statistic_group;
}
}
foreach (
$statistics as
$stat_name => $stat_value
) {
/*
* We are to report only certain
* values, so skip those who we should
* not report on.
*/
if ( ! isset(
$stat_names_to_groups[ $stat_name ]
) ) {
/*
* Not found, so nothing to report, skip.
*/
continue;
}
/*
* Do not report zero or lower.
*/
if ( 0 >= $stat_value ) {
continue;
}
/*
* Report statistic if it belongs to one of the groups.
*/
foreach (
$stat_names_to_groups[ $stat_name ] as $group_name
) {
/*
* Compose URL.
*/
$url = $pixel_api_url .
'?' .
'v=wpcom-no-pv' .
'&' .
'x_' . rawurlencode( strtolower( $group_name ) ) .
'/' . rawurlencode( strtolower( $stat_name ) ) .
'=' . rawurlencode( (string) $stat_value );
/*
* Call service, log if request failed.
* Specify a short timeout, retry only once.
*/
$ret = vipgoci_http_api_fetch_url(
$url,
null, // No token needed.
false, // No fatal error when request fails.
1 // Retry once upon failure.
);
if ( null === $ret ) {
vipgoci_log(
'Unable to send data to Pixel API service',
array(),
0,
true // Send to IRC.
);
}
/*
* Sleep a short while between
* requests.
*/
time_nanosleep(
0,
500000000
);
}
}
vipgoci_log(
'Finished sending statistics to pixel API service',
array()
);
}