-
Notifications
You must be signed in to change notification settings - Fork 9
/
trollstats.py
58 lines (48 loc) · 1.77 KB
/
trollstats.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
from trollreview import ReviewType
import collections
import json
import logging
logger = logging.getLogger('rom.troll.stats')
class TrollStats(object):
def __init__(self, filepath):
self.stats = collections.defaultdict(dict)
self.filepath = filepath
if self.filepath:
try:
with open(self.filepath, 'rt') as f:
file_stats = json.load(f)
# load the stats from the file into memory
for k,v in file_stats.items():
if not isinstance(k, str) or not isinstance(v, dict):
raise ValueError('Malformed stats file expected "{}" '.format(k) +
'as string and "{}" as dict'.format(v))
self.stats[k] = v
except FileNotFoundError:
logger.info('Stats file {} missing, will create'.format(self.filepath))
def update_for_review(self, project, review):
self.increment(project, 'patches')
for i in review.issues:
self.increment(project, i)
for f in review.feedback:
self.increment(project, f)
def increment(self, project, review_type):
pkey = project.name
rkey = str(review_type)
if not self.stats.get(pkey):
self.stats[pkey] = {rkey: 1}
elif not self.stats[pkey].get(rkey):
self.stats[pkey][rkey] = 1
else:
self.stats[pkey][rkey] += 1
def summarize(self, level):
logger.log(level, 'Summary:')
for project,stats in self.stats.items():
logger.log(level, ' Project: {}'.format(project))
for revtype,value in stats.items():
logger.log(level, ' {}={}'.format(revtype, value))
def save(self):
if not self.filepath:
return
logger.debug('Saving stats to {}'.format(self.filepath))
with open(self.filepath, 'wt') as f:
json.dump(self.stats, f, sort_keys=True, indent=2)