-
Notifications
You must be signed in to change notification settings - Fork 2
/
github_state.py
executable file
·311 lines (257 loc) · 9.51 KB
/
github_state.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
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
#!/usr/bin/python3
from __future__ import print_function, division
import time
import requests
import pandas as pd
import numpy as np
import logging
import os.path
import argparse
import configparser
import json
import collections
import http.client as http_client
import matplotlib
if __name__ == '__main__':
matplotlib.use('Agg')
from matplotlib import pyplot
matplotlib.style.use('ggplot')
Auth = collections.namedtuple('Auth', 'user auth')
def colon_separated_pair(arg):
pair = arg.split(':', 1)
if len(pair) == 1:
return Auth(pair[0], '')
else:
return Auth(*pair)
def comma_separated_list(arg):
ll = arg.split(',')
if ll == ['']:
return []
return [item.strip() for item in ll]
class PlotConfig:
def __init__(self, type, title, labels, small=False):
assert type in {'pulls', 'issues'}
self.type = type
self.title = title
self.labels = labels
self.small = small
def __str__(self):
return '{} plot \"{}\" [{}]{}'.format(self.type,
self.title,
','.join(self.labels),
' small' if self.small else '')
def parser():
parser = argparse.ArgumentParser()
parser.add_argument('--project',
help='GitHub project, specified as USER/REPO')
parser.add_argument('--auth', type=colon_separated_pair,
help='GitHub API token, specified as user/digits')
parser.add_argument('--cache-time', type=float, default=60*60,
help='How often should the issue list be refreshed')
parser.add_argument('--formats', type=comma_separated_list, default=['svg', 'png'],
help='Write images in each of those formats')
parser.add_argument('--debug', action='store_true',
help='Turn on debugging of the network operations')
parser.add_argument('dir',
help='Operate on this directory')
return parser
def update_config_entry(args, config, args_name, section, config_name, type=str):
if getattr(args, args_name) is not None:
return
try:
val = config[section][config_name]
except Exception:
return
setattr(args, args_name, type(val))
def update_config(args, conffile):
config = configparser.ConfigParser()
foo = config.read(conffile)
if not foo:
raise ValueError('Unable to read config file "{}"'.format(conffile))
update_config_entry(args, config, 'project', 'project', 'project')
update_config_entry(args, config, 'auth', 'project', 'auth', colon_separated_pair)
if args.project is None:
raise ValueError('project must be specified')
if args.auth is None:
raise ValueError('auth must be specified')
args.plots = []
# each section, except 'project', is a plot
for secname, section in config.items():
if secname in {'project', 'DEFAULT'}:
continue
type = section.get('type') or None
if type is None:
if 'issue' in secname.lower():
type = 'issues'
elif 'pull' in secname.lower():
type = 'pulls'
else:
raise ValueError('need to specify type= in section [{}]'.format(secname))
labels = section.get('labels') or ''
labels = comma_separated_list(labels)
small = section.get('small')
small = small == '1'
args.plots.append(PlotConfig(type, secname, labels=labels))
if small:
args.plots.append(PlotConfig(type, secname, labels=labels, small=True))
# You can initialize logging to see debug output
def init_logging():
http_client.HTTPConnection.debuglevel = 1
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
def get_entries(config, url, max_pages=100, **params):
params['per_page'] = 100
for page in range(1, max_pages + 1): # for safety
params['page'] = str(page)
r = requests.get(url,
params=params,
auth=config.auth)
r.raise_for_status()
json = r.json()
print('got {}, {} items'.format(url, len(json)))
if not json:
# empty list
break
yield json
def get_frames(config, url, **params):
entries = get_entries(config, url, **params)
total = sum(entries, [])
return total
def get_issues_json(config, group):
fname = os.path.join(config.dir,
config.project.replace('/', '_') + '_' + group + '.json')
try:
ts = os.path.getmtime(fname)
if ts + config.cache_time >= time.time():
f = open(fname)
return json.load(f)
except (IOError, ValueError):
pass
url = 'https://api.github.com/repos/{}/{}'.format(config.project, group)
raw = get_frames(config, url, state='all')
f = open(fname, 'w')
json.dump(raw, f)
return raw
def get_issues(config, group='issues'):
raw = get_issues_json(config, group)
df = pd.DataFrame(raw)
df.set_index('number', inplace=True)
df.sort_index(inplace=True)
return df
def match_label(labels, jsonobj):
labels = set(labels)
names = {l['name'] for l in jsonobj}
return bool(labels.intersection(names))
def filter_issues(issues, labels):
filtered = issues[[match_label(labels, jsonobj)
for jsonobj in issues['labels']]]
return filtered
def gb_sum(issues, attr):
gb = issues['id'].groupby(pd.DatetimeIndex(issues[attr]).date).count()
return gb.cumsum()
def massage(issues):
closing = gb_sum(issues, 'closed_at')
opening = gb_sum(issues, 'created_at')
diff = opening - closing
diff.fillna(method='pad', inplace=True)
return opening, closing, diff
def do_label_plot(plot_config, issues):
f = pyplot.figure()
f.canvas.set_window_title(plot_config.title)
ax = f.gca()
ax.set_title(plot_config.title)
opening, closing, diff = massage(issues)
base = diff * 0
series = collections.OrderedDict(_base=base)
for label in plot_config.labels:
filtered = filter_issues(issues, [label])
if filtered.size == 0:
continue
_, _, filtered = massage(filtered)
series[label] = filtered
df = pd.DataFrame(series)
df.fillna(method='ffill', inplace=True)
df.fillna(0, inplace=True)
colors = ('blue', 'magenta', 'yellow', 'green', 'cyan')
for label, color in zip(df.columns, colors):
if label == '_base':
continue
top = base + df[label]
ax.fill_between(base.index.values, base.values, top.values,
label='label:' + label,
color=color)
base = top
# diff.plot() messes up the order
ax.plot(diff.index.values, diff.values, label='open', color='red')
ax.set_xlim(diff.index.values.min(), diff.index.values.max())
ax.set_ylim(-1)
ax.legend(loc='upper left')
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles[:1] + handles[:0:-1], labels[:1] + labels[:0:-1])
f.autofmt_xdate()
f.tight_layout()
return f
def do_plot(plot_config, issues):
f = pyplot.figure()
opening, closing, diff = massage(issues)
ax = opening.plot(label='all')
ax.set_ylabel('cumulative closed, cumulative all')
closing.plot(label='closed')
ax.set_title(plot_config.title)
f.canvas.set_window_title(plot_config.title)
f.autofmt_xdate()
ax.legend(loc='upper left')
ax2 = ax.twinx()
ax2.set_ylabel('open', color='red')
diff.plot(label='open', style='red')
ax2.legend(loc='lower right')
return f
def do_small_plot(plot_config, issues):
opening, closing, diff = massage(issues)
f = pyplot.figure(figsize=(2,1))
ax = diff.plot() # style=style)
ax.set_xticks([])
ax.set_yticks([])
ax.annotate(plot_config.title,
xy=(0, 1), xycoords='axes fraction', fontsize=16,
horizontalalignment='left', verticalalignment='top')
f.subplots_adjust(0, 0, 1, 1)
return f
def image_filename(config, plot_config, ext):
prefix = os.path.join(config.dir, 'images', config.project.replace('/', '-'))
subj = plot_config.title.lower().replace(' ', '-')
small = '-small' if plot_config.small else ''
return '{}-{}{}.{}'.format(prefix, subj, small, ext)
def savefig(config, plot_config, figure):
for extension in config.formats:
fname = image_filename(config, plot_config, extension)
os.makedirs(os.path.basename(fname), exist_ok=True)
figure.savefig(fname)
if config.debug:
print('Saved {}'.format(fname))
def issues_and_prs(config):
issues = get_issues(config, group='issues')
has_pr = -issues.pull_request.isnull()
pulls = issues[has_pr]
other = issues[-has_pr]
return other, pulls
if __name__ == '__main__':
args = parser().parse_args()
if args.debug:
init_logging()
update_config(args, os.path.join(args.dir, 'project.conf'))
issues, pulls = issues_and_prs(args)
for plot in args.plots:
if args.debug:
print(str(plot))
items = pulls if plot.type == 'pulls' else issues
if plot.small:
f = do_small_plot(plot, items)
elif plot.labels:
f = do_label_plot(plot, items)
else:
f = do_plot(plot, items)
savefig(args, plot, f)