Skip to content
This repository was archived by the owner on Jul 2, 2021. It is now read-only.

Commit e016384

Browse files
author
Claas Augner
committed
Fix summary.
1 parent 515f16a commit e016384

File tree

3 files changed

+60
-29
lines changed

3 files changed

+60
-29
lines changed

src/Telemetry/InboxApp.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public function __construct(Repository $repo) {
2525

2626
// Summary.
2727
$this->get('/summary', function () {
28-
$summarizer = new Summarizer();
29-
foreach ($this->repo->objects() as $object) {
30-
$summarizer->add($object);
28+
$summarizer = new Summarizer(['id']);
29+
foreach ($this->repo->arrays() as $array) {
30+
$summarizer->add($array);
3131
}
3232
return $this->json(['summary' => $summarizer->getSummary()]);
3333
});

src/Telemetry/Repository.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ public function count() {
3333
return $count;
3434
}
3535

36+
public function arrays() {
37+
foreach ($this->contents() as $content) {
38+
try {
39+
$array = json_decode($content, true);
40+
yield $array;
41+
} catch (Exception $e) {
42+
// Ignore.
43+
}
44+
}
45+
}
46+
3647
public function objects() {
3748
foreach ($this->contents() as $content) {
3849
try {
@@ -46,7 +57,7 @@ public function objects() {
4657

4758
public function contents() {
4859
foreach ($this->files() as $file) {
49-
yield file_get_contents($item);
60+
yield file_get_contents($file);
5061
}
5162
}
5263

src/Telemetry/Summarizer.php

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,75 +12,95 @@ public function __construct($ignore=[]) {
1212
}
1313

1414
public function add($array) {
15-
$this->summary = self::summarize($array, $this->summary);
15+
self::summarize((array)$array, $this->summary);
1616
}
1717

1818
public function getSummary() {
1919
return $this->summary;
2020
}
2121

2222
private function summarize($array, &$summary, $ignore=[]) {
23+
// Count path..
2324
if (!isset($summary['count'])) {
2425
$summary['count'] = 1;
2526
} else {
2627
$summary['count'] += 1;
2728
}
29+
30+
// Add key-value pairs to summary.
2831
foreach ($array as $key => $value) {
29-
if (in_array($key, $ignore)) {
32+
if (in_array($key, $this->ignore)) {
33+
// Key is on ignore list.
3034
continue;
3135
}
36+
3237
if (is_numeric($key) && is_string($value)) {
38+
// Item of an array.
3339
if (!isset($summary['values'])) {
3440
$summary['values'] = [];
3541
}
36-
if (!isset($summary['values'][$value])) {
37-
$summary['values'][$value] = 1;
42+
$values = &$summary['values'];
43+
if (!isset($values[$value])) {
44+
$values[$value] = 1;
3845
} else {
39-
$summary['values'][$value] += 1;
46+
$values[$value] += 1;
4047
}
4148
} else if (is_array($value)) {
42-
if (!isset($summary['children'][$key])) {
43-
$summary['children'][$key] = [];
49+
if (!isset($summary['children'])) {
50+
$summary['children'] = [];
4451
}
45-
$this->summarize($value, $summary['children'][$key]);
52+
$children =& $summary['children'];
53+
if (!isset($children[$key])) {
54+
$children[$key] = [];
55+
}
56+
$this->summarize((array)$value, $children[$key]);
4657
} else {
4758
if (!isset($summary['keys'])) {
4859
$summary['keys'] = [];
4960
}
50-
if (!isset($summary['keys'][$key])) {
51-
$summary['keys'][$key] = [
61+
$keys = &$summary['keys'];
62+
if (!isset($keys[$key])) {
63+
$keys[$key] = [
5264
'count' => 1
5365
];
5466
} else {
55-
$summary['keys'][$key]['count'] += 1;
67+
$keys[$key]['count'] += 1;
5668
}
57-
if (is_numeric($value)) {
58-
// Min/max erfassen.
59-
if (!isset($summary['keys'][$key]['distribution'])) {
60-
$summary['keys'][$key]['distribution'] = [
69+
70+
$current_key = &$keys[$key];
71+
72+
73+
if (is_numeric($value) && !is_string($value)) {
74+
// Numeric values => summarize distribution.
75+
76+
if (!isset($current_key['distribution'])) {
77+
$current_key['distribution'] = [
6178
'count' => 1,
6279
'sum' => $value,
6380
'min' => $value,
6481
'max' => $value
6582
];
6683
} else {
67-
$summary['keys'][$key]['distribution']['count'] += 1;
68-
$summary['keys'][$key]['distribution']['sum'] += $value;
69-
$summary['keys'][$key]['distribution']['min'] = min($value, $summary['keys'][$key]['distribution']['min']);
70-
$summary['keys'][$key]['distribution']['max'] = max($value, $summary['keys'][$key]['distribution']['max']);
84+
$distribution = &$current_key['distribution'];
85+
$distribution['count'] += 1;
86+
$distribution['sum'] += $value;
87+
$distribution['min'] = min($value, $distribution['min']);
88+
$distribution['max'] = max($value, $distribution['max']);
7189
}
7290
} else if (is_string($value)) {
73-
if (!isset($summary['keys'][$key]['values'])) {
74-
$summary['keys'][$key]['values'] = [];
91+
// Non-numeric string values => count values.
92+
if (!isset($current_key['values'])) {
93+
$current_key['values'] = [];
7594
}
76-
if (!isset($summary['keys'][$key]['values'][$value])) {
77-
$summary['keys'][$key]['values'][$value] = 1;
95+
$values = &$current_key['values'];
96+
97+
if (!isset($values[$value])) {
98+
$values[$value] = 1;
7899
} else {
79-
$summary['keys'][$key]['values'][$value] += 1;
100+
$values[$value] += 1;
80101
}
81102
}
82103
}
83104
}
84-
return $summary;
85105
}
86106
}

0 commit comments

Comments
 (0)