-
Notifications
You must be signed in to change notification settings - Fork 21
/
gitaggregates.py
179 lines (134 loc) · 5.5 KB
/
gitaggregates.py
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
#!/usr/bin/env python
"""A collection of git info aggregators for graph commands."""
import time
import subprocess
from collections import defaultdict
class TimeHisto(object):
def __init__(self):
self.h = defaultdict(lambda: 0)
def add_logs(self, directory=None, log_args=['HEAD']):
args=['git']
if directory:
args.append('--git-dir=' + directory)
args.extend(['log', '--format=%ad', '--date=raw'])
args.extend(log_args)
sub = subprocess.Popen(args, stdout=subprocess.PIPE, close_fds=True)
for l in sub.stdout:
t, offset = l.strip().split(' ')
t = float(t) + int(offset) / 100 * 3600
self.h[time.strftime("%w %H", time.gmtime(t))] += 1
def dump(self):
for h in range(24):
for d in range(7):
sys.stderr.write("%02d %d - %s\n"
% (h, d, self.h["%d %02d" % (d, h)]))
def to_gchart(self):
from pygooglechart import ScatterChart
chart = ScatterChart(800, 300, x_range=(-1, 24), y_range=(-1, 7))
chart.add_data([(h % 24) for h in range(24 * 8)])
d=[]
for i in range(8):
d.extend([i] * 24)
chart.add_data(d)
day_names = "Sun Mon Tue Wed Thu Fri Sat".split(" ")
days = (0, 6, 5, 4, 3, 2, 1)
sizes=[]
for d in days:
sizes.extend([self.h["%d %02d" % (d, h)] for h in range(24)])
sizes.extend([0] * 24)
chart.add_data(sizes)
chart.set_axis_labels('x', [''] + [str(h) for h in range(24)] + [''])
chart.set_axis_labels('y', [''] + [day_names[n] for n in days] + [''])
chart.add_marker(1, 1.0, 'o', '333333', 25)
return chart.get_url() + '&chds=-1,24,-1,7,0,20'
for l in sub.stdout:
self.h[time.strftime("%w %H", time.localtime(float(l.strip())))] += 1
def dump(self):
for h in range(24):
for d in range(7):
sys.stderr.write("%02d %d - %s\n"
% (h, d, self.h["%d %02d" % (d, h)]))
def to_gchart(self):
from pygooglechart import ScatterChart
chart = ScatterChart(800, 300, x_range=(-1, 24), y_range=(-1, 7))
chart.add_data([(h % 24) for h in range(24 * 8)])
d=[]
for i in range(8):
d.extend([i] * 24)
chart.add_data(d)
day_names = "Sun Mon Tue Wed Thu Fri Sat".split(" ")
days = (0, 6, 5, 4, 3, 2, 1)
sizes=[]
for d in days:
sizes.extend([self.h["%d %02d" % (d, h)] for h in range(24)])
sizes.extend([0] * 24)
chart.add_data(sizes)
chart.set_axis_labels('x', [''] + [str(h) for h in range(24)] + [''])
chart.set_axis_labels('y', [''] + [day_names[n] for n in days] + [''])
chart.add_marker(1, 1.0, 'o', '333333', 25)
return chart.get_url() + '&chds=-1,24,-1,7,0,20'
class Contributors(object):
width = 420
height = 300
max_entries = 6
def __init__(self, log_args=['HEAD']):
self.data=[]
args = ['git', 'shortlog', '-sn'] + log_args
sub = subprocess.Popen(args, stdout=subprocess.PIPE, close_fds=True)
for l in sub.stdout:
commits, name = [x.strip() for x in l.split("\t")]
commits = int(commits)
self.data.append((name, commits))
def dump(self):
sys.stderr.write(repr(self.data) + "\n")
def mk_chart(self):
from pygooglechart import PieChart2D
chart = PieChart2D(self.width, self.height)
data = self.data[:self.max_entries]
if len(self.data) > len(data):
remainder = sum(d[1] for d in self.data[self.max_entries:])
data.append(('Other', remainder))
chart.add_data([d[1] for d in data])
chart.set_pie_labels([d[0].split()[0] for d in data])
return chart
def to_gchart(self):
return self.mk_chart().get_url()
class ChangeOverTime(object):
width = 800
height = 300
max_entries = 6
def __init__(self, log_args=['HEAD']):
self.newdata = []
self.cumulative = []
self.cmax = 0
self.max = 0
def add_logs(self, directory=None, log_args=['HEAD']):
args = ['git', 'log', '--pretty=format:%at'] + log_args
sub = subprocess.Popen(args, stdout=subprocess.PIPE, close_fds=True)
dates = {}
for l in sub.stdout:
k = time.strftime("%F", time.localtime(float(l.strip())))
dates[k] = dates.get(k, 0) + 1
total = 0
for k,v in sorted(dates.items()):
total += v
self.max = max(self.max, v)
self.newdata.append( (k, v) )
self.cumulative.append( (k, total) )
self.cmax = total
def dump(self):
sys.stderr.write(repr(self.data) + "\n")
def mk_chart(self):
from pygooglechart import SimpleLineChart
chart = SimpleLineChart(self.width, self.height, colours=('91CF60', 'FC8D59'))
all_labels = [d[0] for d in self.cumulative]
chart.add_data([d[1] for d in self.cumulative])
chart.add_data([d[1] for d in self.newdata])
chart.set_axis_labels('y', range(0, self.cmax, (self.cmax / 4)))
chart.set_axis_labels('x', [all_labels[x] for x in
range(0, len(all_labels), (len(all_labels) / 4))])
return chart
def to_gchart(self):
return self.mk_chart().get_url()
def open_chart(chartish):
subprocess.check_call(['open', chartish.to_gchart()])