-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetstats.php
More file actions
140 lines (132 loc) · 4.2 KB
/
Copy pathgetstats.php
File metadata and controls
140 lines (132 loc) · 4.2 KB
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
<?php
# Copyright (c) 2012, Łukasz Kieś <kiesiu@pld-linux.org>
# All rights reserved.
#
# Licensed under BSD 2-Clause license. Please refer to LICENSE.md
function getData($query, $vars) {
try {
$dbh = new PDO('sqlite:pldstats.db');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$stmt = $dbh->prepare($query);
$stmt->execute($vars);
return $stmt->fetchAll();
}
catch (PDOExcetion $e) {
die($e->getMessage());
}
}
function makeGraph($title, $xlabels=array(), $ldefs=array(), $rdefs=array()) {
$graph = array(
'bg_colour' => '#ffffff',
'title' => array(
'text' => $title
),
'x_axis' => array(
'labels' => array(
'labels' => $xlabels,
'rotate' => 'vertical'
)
),
'legend' => array(
'visible' => true
),
'elements' => array()
);
if (count($ldefs) == 4) {
$graph['elements'][] = array(
'type' => $ldefs[0],
'text' => $ldefs[1],
'width' => 2,
'colour' => $ldefs[2],
'values' => $ldefs[3]
);
$graph['y_axis'] = array(
'colour' => $ldefs[2],
'min' => 0,
'max' => ceil(max($ldefs[3]) * 1.1),
'steps' => round(max($ldefs[3]) / 10)
);
}
if (count($rdefs) == 4) {
$graph['elements'][] = array(
'type' => $rdefs[0],
'text' => $rdefs[1],
'width' => 2,
'colour' => $rdefs[2],
'values' => $rdefs[3],
'axis' => 'right'
);
$graph['y_axis_right'] = array(
'colour' => $rdefs[2],
'min' => 0,
'max' => ceil(max($rdefs[3]) * 1.1),
'steps' => round(max($rdefs[3]) / 10)
);
}
echo json_encode($graph);
}
function getAuthors() {
$result = getData('SELECT name, authors FROM data LIMIT 30', array());
$values = array();
$labels = array();
foreach($result as $e) {
$labels[] = $e['name'];
$values[] = (int)$e['authors'];
}
makeGraph('Commiters weekly (last 30 weeks)', $labels,
array('line', 'authors', '#a4c639', $values));
}
function getCommits() {
$result = getData('SELECT name, commits, changes FROM data LIMIT 30', array());
$values = array(array(), array());
$labels = array();
foreach($result as $e) {
$labels[] = $e['name'];
$values[0][] = (int)$e['commits'];
$values[1][] = (int)$e['changes'];
}
makeGraph('Commits weekly (last 30 weeks)', $labels,
array('line', 'commits', '#1034a6', $values[0]),
array('line', 'lines changed', '#e87600', $values[1]));
}
function getTopEver() {
$result = getData('SELECT author, COUNT(author) AS c, SUM(changes) AS l FROM commits GROUP BY author ORDER BY c DESC LIMIT 30', array());
$values = array(array(), array());
$labels = array();
foreach($result as $e) {
$labels[] = $e['author'];
$values[0][] = (int)$e['c'];
$values[1][] = (int)$e['l'];
}
makeGraph('Top 30 commiters ever', $labels,
array('bar', 'commits', '#800080', $values[0]),
array('bar', 'lines changed', '#ed1c24', $values[1]));
}
function getTopWeek() {
$result = getData('SELECT name FROM data ORDER BY name DESC LIMIT 1', array());
$result = getData('SELECT author, COUNT(author) AS c, SUM(changes) AS l FROM commits WHERE id=? GROUP BY author ORDER BY c DESC LIMIT 10', array($result[0]['name']));
$values = array(array(), array());
$labels = array();
foreach($result as $e) {
$labels[] = $e['author'];
$values[0][] = (int)$e['c'];
$values[1][] = (int)$e['l'];
}
makeGraph('Top 10 commiters last week', $labels,
array('bar', 'commits', '#1f75fe', $values[0]),
array('bar', 'lines changed', '#800000', $values[1]));
}
if ($_GET['d'] == 'authors') {
getAuthors();
}
elseif ($_GET['d'] == 'commits') {
getCommits();
}
elseif ($_GET['d'] == 'topweek') {
getTopWeek();
}
elseif ($_GET['d'] == 'topever') {
getTopEver();
}
?>