forked from openSUSE/openSUSE-release-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregate.php
executable file
·569 lines (491 loc) · 16.8 KB
/
aggregate.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#!/usr/bin/php
<?php
use InfluxDB2\Client;
use InfluxDB2\Point;
$CACHE_DIR = $_SERVER['HOME'] . '/.cache/openSUSE-release-tools/metrics-access';
const PROTOCOLS = ['ipv4', 'ipv6'];
const DOWNLOAD_OPENSUSE_ORG = 'https://download.opensuse.org/logs';
const PONTIFEX = 'http://pontifex.infra.opensuse.org/logs';
const BACKUP = 'http://backup.infra.opensuse.org';
const LANGLEY = 'http://langley.suse.de/pub/pontifex%s-opensuse.suse.de';
const VHOST = 'download.opensuse.org';
const FILENAME = 'download.opensuse.org-%s-access_log.xz';
const IPV6_PREFIX = 'ipv6.';
const PRODUCT_PATTERN = '/^(10\.[2-3]|11\.[0-4]|12\.[1-3]|13\.[1-2]|42\.[1-3]|15\.[0-6]|16\.0|tumbleweed|slowroll)$/';
$begin = new DateTime();
// Skip the current day since the logs are incomplete and not compressed yet.
$begin->sub(date_interval_create_from_date_string('1 day'));
$source_map = [
'ipv4' => [
// the first item defines the starting date for aggregation
'2023-01-01' => false,
'2023-11-13' => DOWNLOAD_OPENSUSE_ORG . '/' . VHOST,
'filename' => FILENAME,
],
'ipv6' => [
'2012-12-31' => false,
'2023-11-13' => DOWNLOAD_OPENSUSE_ORG . '/' . IPV6_PREFIX . VHOST,
'filename' => IPV6_PREFIX . FILENAME,
],
'ipv4+6' => [
'2023-11-13' => false,
$begin->format('Y-m-d') => DOWNLOAD_OPENSUSE_ORG . '/' . VHOST,
'filename' => FILENAME,
],
];
$end = new DateTime(key($source_map['ipv4'])); // decide about adding one day
$migration_date = new DateTime(key($source_map['ipv4+6']));
$period_reversed = date_period_reversed($end, '1 day', $begin);
error_log('begin: ' . $begin->format('Y-m-d'));
error_log('end: ' . $end->format('Y-m-d'));
error_log('count: ' . number_format(count($period_reversed)) . ' days');
cache_init();
ingest_all($period_reversed, $source_map);
aggregate_all(array_reverse($period_reversed));
function cache_init()
{
global $CACHE_DIR;
if (!file_exists($CACHE_DIR)) {
foreach (PROTOCOLS as $protocol) {
mkdir("$CACHE_DIR/$protocol", 0755, true);
}
mkdir("$CACHE_DIR/ipv4+6", 0755, true);
// Avoid packaging mess while still automating, but not ideal.
passthru('cd ' . escapeshellarg($CACHE_DIR) .
' && composer require influxdata/influxdb-client-php:~3.4 guzzlehttp/guzzle');
}
require "$CACHE_DIR/vendor/autoload.php";
}
function ingest_all($period_reversed, $source_map)
{
global $CACHE_DIR;
$source = [];
$found = [];
// Walk backwards until found in cache.
foreach ($period_reversed as $date) {
$date_string = print_date($date);
$protocols_on_day = get_protocols($date);
foreach ($protocols_on_day as $protocol) {
if (!empty($found[$protocol])) continue;
if (isset($source_map[$protocol][$date_string]))
$source[$protocol] = $source_map[$protocol][$date_string];
// Skip date+protocol if no source is available.
if (empty($source[$protocol])) continue;
$cache_file = get_cache_file($protocol, $date);
if (file_exists($cache_file)) {
error_log("[$date_string] [$protocol] found");
$found[$protocol] = true;
} else {
error_log("[$date_string] [$protocol] ingest");
ingest($date, $source[$protocol], $source_map[$protocol]['filename'], $cache_file);
}
}
// Stop when all cache files were found
if (count($found) == count($protocols_on_day)) {
error_log('ingest initialization complete');
break;
}
}
// Wait for all ingest processes to complete before proceeding.
subprocess_wait(1, 1);
}
function print_date($date)
{
return $date->format('Y-m-d');
}
// Logs before migration date have been kept in separate files for IPv4 and IPv6 addresses
function has_separate_protocol_logs($date)
{
global $migration_date;
if ($date > $migration_date)
return false;
else
return true;
}
function get_cache_file($protocol, $date)
{
global $CACHE_DIR;
if (has_separate_protocol_logs($date))
return "$CACHE_DIR/$protocol/" . print_date($date) . ".json";
else
return "$CACHE_DIR/ipv4+6/" . print_date($date) . ".json";
}
function get_cache_files($date)
{
$files = [];
foreach (get_protocols($date) as $protocol)
array_push($files, get_cache_file($protocol, $date));
return $files;
}
function get_protocols($date)
{
if (has_separate_protocol_logs($date))
return PROTOCOLS;
else
return array("ipv4+6");
}
function ingest($date, $source, $filename, $destination)
{
$url = implode('/', [
$source,
$date->format('Y'),
$date->format('m'),
sprintf($filename, $date->format('Ymd')),
]);
$command = implode(' ', [
'curl -s --digest --netrc',
escapeshellarg($url),
'| xzcat',
'| ' . __DIR__ . '/ingest.php',
'> ' . escapeshellarg($destination),
'&',
]);
error_log($command);
passthru_block($command);
}
function passthru_block($command)
{
static $cpu_count = null;
if (!$cpu_count) {
$cpuinfo = file_get_contents('/proc/cpuinfo');
preg_match_all('/^processor/m', $cpuinfo, $matches);
$cpu_count = max(count($matches[0]), 1);
error_log("detected $cpu_count cores");
}
$group_size = substr_count($command, '|') + 1;
subprocess_wait($group_size, $cpu_count);
passthru($command, $exit_code);
if ($exit_code != 0) {
error_log('failed to start process');
exit(1);
}
}
function subprocess_wait($group_size, $cap)
{
while (subprocess_count() / $group_size >= $cap) {
usleep(250000);
}
}
function subprocess_count()
{
return substr_count(shell_exec('pgrep -g ' . getmypid()), "\n") - 1;
}
function aggregate_all($period)
{
global $CACHE_DIR;
$intervals = ['day' => 'Y-m-d', 'week' => 'Y-W', 'month' => 'Y-m', 'FQ' => null];
$merged = [];
$merged_protocol = [];
$date_previous = null;
foreach ($period as $date) {
$date_string = print_date($date);
$data = null;
foreach (PROTOCOLS as $protocol) {
$cache_file = get_cache_file($protocol, $date);
if (!file_exists($cache_file) or !filesize($cache_file)) continue;
error_log("[$date_string]" . (has_separate_protocol_logs($date) ? " [$protocol]" : "") . " load cache");
$data_new = json_decode(file_get_contents($cache_file), true);
if (!$data_new) {
error_log('ERROR: failed to load ' . $cache_file);
unlink($cache_file); // Trigger it to be re-ingested next run.
exit(1);
}
if (isset($data_new[$protocol])) {
// new cache files have 'ipv4' and 'ipv6' array keys
$data_protocol = $data_new[$protocol];
// we don't want to count 'total_invalid' and 'bytes' twice
if ($data) {
$data_protocol['total_invalid'] = 0;
$data_protocol['bytes'] = 0;
} else {
$data_protocol['total_invalid'] = $data_new['total_invalid'];
$data_protocol['bytes'] = $data_new['bytes'];
}
}
else
$data_protocol = $data_new;
if (!isset($merged_protocol[$protocol])) $merged_protocol[$protocol] = [];
$data_protocol['days'] = 1;
normalize($data_protocol);
aggregate($intervals, $merged_protocol[$protocol], $date, $date_previous, $data_protocol,
['protocol' => $protocol], 'protocol');
if ($data) {
merge($data, $data_protocol);
$data['days'] = 1;
} else {
$data = $data_protocol;
}
}
if (!$data) {
error_log("[$date_string] skipping due to lack of data");
continue;
}
aggregate($intervals, $merged, $date, $date_previous, $data);
$date_previous = $date;
}
// Write out any remaining data by simulating a date beyond all intervals.
/*error_log('write remaining data');
$date = clone $date;
$date->add(date_interval_create_from_date_string('1 year'));
foreach (PROTOCOLS as $protocol) {
aggregate($intervals, $merged_protocol[$protocol], $date, $date_previous, null,
['protocol' => $protocol], 'protocol');
}
aggregate($intervals, $merged, $date, $date_previous, null);*/
}
function aggregate($intervals, &$merged, $date, $date_previous, $data, $tags = [], $prefix = 'access')
{
foreach ($intervals as $interval => $format) {
if ($interval === 'FQ') {
$value = format_FQ($date);
if (isset($date_previous))
$value_previous = format_FQ($date_previous);
}
elseif ($interval === 'FY') {
$value = format_FY($date);
if (isset($date_previous))
$value_previous = format_FY($date_previous);
}
else {
$value = $date->format($format);
if (isset($date_previous))
$value_previous = $date_previous->format($format);
}
if (!isset($merged[$interval]) || $value != $merged[$interval]['value']) {
if (!empty($merged[$interval]['data'])) {
$summary = summarize($merged[$interval]['data']);
if ($prefix === 'protocol') {
$summary = ['-' => $summary['-']];
}
$flavors = [];
foreach ($summary as $product => $details) {
if (isset($details['flavors'])) {
$flavors[$product] = $details['flavors'];
unset($summary[$product]['flavors']);
}
}
if (isset($value_previous) and $value != $value_previous) {
$count = write_summary($interval, $date_previous, $summary, $tags, $prefix);
if (isset($flavors)) {
$count += write_flavors($interval, $date_previous, $flavors);
}
if ($prefix === 'access') {
$summary = summarize_product_plus_key($merged[$interval]['data']['total_image_product']);
$count += write_summary_product_plus_key($interval, $date_previous, $summary, 'image');
}
error_log("[$prefix] [$interval] [{$merged[$interval]['value']}] wrote $count points at " .
$date_previous->format('Y-m-d') . " spanning " . $merged[$interval]['data']['days'] . ' day(s)');
}
}
// Reset merge data to current data.
$merged[$interval] = [
'data' => $data,
'value' => $value,
];
}
// Merge day onto existing data for interval. A more complex approach of
// merging higher order intervals is overly complex due to weeks.
else
merge($merged[$interval]['data'], $data);
}
}
function format_FQ($date)
{
$financial_date = clone $date;
date_add($financial_date, date_interval_create_from_date_string('2 months'));
$quarter = ceil($financial_date->format('n')/3);
return $financial_date->format('Y') . '-' . $quarter;
}
function format_FY($date)
{
$financial_date = clone $date;
date_add($financial_date, date_interval_create_from_date_string('2 months'));
return $financial_date->format('Y');
}
function normalize(&$data)
{
// Ensure fields added later, that are not present in all data, are available.
if (!isset($data['total_image_product'])) {
$data['total_image_product'] = [];
}
$first_product = reset($data['unique_product']);
$first_key = reset($first_product);
if (is_int($first_key)) {
foreach ($data['unique_product'] as $product => $pairs) {
foreach ($pairs as $key => $count) {
$data['unique_product'][$product][$key] = ['count' => $count];
}
}
}
}
function merge(&$data1, $data2)
{
$data1['days'] += $data2['days'];
$data1['total'] += $data2['total'];
foreach ($data2['total_product'] as $product => $total) {
if (empty($data1['total_product'][$product]))
$data1['total_product'][$product] = 0;
$data1['total_product'][$product] += $total;
}
merge_unique_products($data1['unique_product'], $data2['unique_product']);
merge_product_plus_key($data1['total_image_product'], $data2['total_image_product']);
$data1['total_invalid'] += $data2['total_invalid'];
$data1['bytes'] += $data2['bytes'];
}
function merge_product_plus_key(&$data1, $data2)
{
foreach ($data2 as $product => $pairs) {
if (empty($data1[$product]))
$data1[$product] = [];
foreach ($pairs as $key => $value) {
if (empty($data1[$product][$key]))
$data1[$product][$key] = 0;
$data1[$product][$key] += $data2[$product][$key];
}
}
}
function merge_unique_products(&$data1, $data2)
{
foreach ($data2 as $product => $arrays) {
if (empty($data1[$product]))
$data1[$product] = [];
foreach ($arrays as $key => $array) {
if (empty($data1[$product][$key]))
$data1[$product][$key] = ['count' => 0];
$data1[$product][$key]['count'] += $array['count'];
if (isset($array['flavor'])) $data1[$product][$key]['flavor'] = $array['flavor'];
if (isset($array['ip'])) $data1[$product][$key]['ip'] = $array['ip'];
}
}
}
function summarize($data)
{
static $products = [];
$summary = [];
$summary['-'] = [
'total' => $data['total'],
'total_invalid' => $data['total_invalid'],
'bytes' => $data['bytes'],
'unique' => 0,
];
foreach ($data['total_product'] as $product => $total) {
if (!product_filter($product)) continue;
$summary_product = [
'total' => $total,
];
if (isset($data['unique_product'][$product])) {
$unique_product = $data['unique_product'][$product];
$summary_product += [ 'unique' => count($unique_product) ];
// A UUID should be unique to a product, as such this should provide an
// accurate count of total unique across all products.
$summary['-']['unique'] += $summary_product['unique'];
$first_key = reset($data['unique_product'][$product]);
if (isset($first_key['flavor'])) {
$unique_flavors = array_column($data['unique_product'][$product], 'flavor');
$flavors = array_unique($unique_flavors);
$summary_product['flavors'] = [];
foreach ($flavors as $flavor) {
$summary_product['flavors'][$flavor] = count(array_keys($unique_flavors, $flavor));
}
}
} else {
$summary_product += [ 'unique' => 0 ];
}
$summary[$product] = $summary_product;
// Keep track of which products have been included in previous summary.
if (!isset($products[$product])) $products[$product] = true;
}
// Fill empty data with zeros to achieve appropriate result in graph.
$missing = array_diff(array_keys($products), array_keys($summary));
foreach ($missing as $product) {
$summary[$product] = [
'total' => 0,
'unique' => 0,
];
}
return $summary;
}
function summarize_product_plus_key($data)
{
static $keys = [];
$summary = [];
$products = array_merge(array_keys($keys), array_keys($data));
foreach ($products as $product) {
if (!product_filter($product)) continue;
$keys_keys = isset($keys[$product]) ? array_keys($keys[$product]) : [];
$data_keys = isset($data[$product]) ? array_keys($data[$product]) : [];
$product_keys = array_merge($keys_keys, $data_keys);
if (!isset($keys[$product])) $keys[$product] = [];
$summary[$product] = [];
foreach ($product_keys as $key) {
// Fill empty data with zeros to achieve appropriate result in graph.
$keys[$product][$key] = true;
$summary[$product][$key] = isset($data[$product][$key]) ? $data[$product][$key] : 0;
}
}
return $summary;
}
function product_filter($product)
{
return (bool) preg_match(PRODUCT_PATTERN, $product);
}
function date_period_reversed($begin, $interval, $end)
{
$interval = DateInterval::createFromDateString($interval);
$period = new DatePeriod($begin, $interval, $end);
return array_reverse(iterator_to_array($period));
}
function write_summary($interval, DateTime $value, $summary, $tags = [], $prefix = 'access')
{
$measurement = $prefix . '_' . $interval;
$points = [];
foreach ($summary as $product => $fields) {
$points[] = new Point($measurement, ['product' => $product] + $tags, $fields, $value->getTimestamp());
}
write($points);
return count($points);
}
function write_flavors($interval, DateTime $value, $flavors)
{
$measurement = 'access_' . $interval;
$points = [];
foreach ($flavors as $product => $unique_flavors) {
foreach($unique_flavors as $flavor => $unique_count) {
$tags = ['product' => $product, 'flavor' => $flavor];
$fields = ['value' => $unique_count];
$points[] = new Point($measurement, $tags, $fields, $value->getTimestamp());
}
}
write($points);
return count($points);
}
function write_summary_product_plus_key($interval, DateTime $date, $summary, $prefix)
{
$measurement = $prefix . '_' . $interval;
$points = [];
foreach ($summary as $product => $pairs) {
foreach ($pairs as $key => $value) {
$points[] = new Point($measurement,
['product' => $product, 'key' => $key], ['value' => $value], $date->getTimestamp());
}
}
write($points);
return count($points);
}
function write($points)
{
static $client;
static $writeApi;
if (!$client) {
$client = new Client([
"url" => "http://localhost:8086",
"token" => "",
"bucket" => "osrt_access/autogen",
"org" => "-",
"precision" => InfluxDB2\Model\WritePrecision::S
]);
$writeApi = $client->createWriteApi();
}
if (!is_null($writeApi->write($points)))
die('failed to write points');
}