forked from tolleiv/Repo-Activity-Monitor
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJsonFormatter.php
executable file
·51 lines (49 loc) · 1.4 KB
/
JsonFormatter.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
<?php
class JsonFormatter {
/**
* @param $persons
* @param $maxMonths
* @param $dataDir
*/
function generate($persons, $commitCounts, $maxMonths) {
$now = strtotime("first day of this month");
$authorIds = array();
$jsonAuthors = array();
$i = 0;
foreach ($persons as $_ => $data) {
foreach ($data as $author => $commits) {
if (!array_key_exists($author, $authorIds)) {
$authorIds[$author] = ++$i;
}
$id = $authorIds[$author];
$jsonAuthors[$id] = array(
'n' => $author,
'c' => (isset($jsonAuthors[$id]['c']) ? $jsonAuthors[$id]['c'] : 0) + $commits,
);
}
}
$i = 0;
$max = 0;
$jsonBuckets = array();
do {
$time = strtotime($i ? '-' . $i . ' month' : 'now', $now);
$bucket = array('d' => $time, 'i' => array());
$index = strftime('%Y.%m', $time);
$tmpSum = 0;
if (!isset($persons[$index])) {
continue;
}
$top20 = array_keys(array_slice($persons[$index], 0, 20));
$contributors = $persons[$index];
ksort($contributors);
foreach ($contributors as $author => $commits) {
$commitCount = in_array($author, $top20) ? $commits : 1;
$bucket['i'][] = array($authorIds[$author], $commitCount);
$tmpSum += $commits;
}
$max = $tmpSum > $max ? $tmpSum : $max;
$jsonBuckets[] = $bucket;
} while (++$i < $maxMonths);
return json_encode(array('authors' => $jsonAuthors, 'buckets' => $jsonBuckets, 'max' => $max));
}
}