Add my changes
- support for longer periods of time (entire history) - cache data from gerrit for an hour using pickle. This is nice when running the script over and over for whatever reason (different time periods, or hacking on the script) - add license header (discussed with Soren) - sort output, and put it in prettytable format - include -2/-1/0/+1/+2 data per reviewer as well as totals
This commit is contained in:
parent
d9869fe340
commit
7b4ae15adb
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
*.pyc
|
||||
*.pickle
|
||||
|
1
nova-core-team.json
Normal file
1
nova-core-team.json
Normal file
@ -0,0 +1 @@
|
||||
["jogo", "sdague-b", "danms", "yunmao", "p-draigbrady", "mikalstill", "russellb", "cerberus", "markmc", "jk0", "rconradharris", "dan-prince", "cbehrens", "klmitch", "johannes.erdfelt", "vishvananda", "dan-prince", "tr3buchet"]
|
117
stats.py
Normal file → Executable file
117
stats.py
Normal file → Executable file
@ -1,28 +1,85 @@
|
||||
#!/usr/bin/python
|
||||
#
|
||||
# Copyright (C) 2011 - Soren Hansen
|
||||
# Copyright (C) 2013 - Red Hat, Inc.
|
||||
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
|
||||
|
||||
import calendar
|
||||
import cPickle as pickle
|
||||
import datetime
|
||||
import json
|
||||
import optparse
|
||||
import os
|
||||
import os.path
|
||||
import paramiko
|
||||
from pprint import pprint
|
||||
import prettytable
|
||||
import sys
|
||||
import time
|
||||
|
||||
|
||||
CACHE_AGE = 3600 # Seconds
|
||||
|
||||
CLIENT_MAP = {
|
||||
'python-novaclient': 'nova',
|
||||
}
|
||||
|
||||
|
||||
optparser = optparse.OptionParser()
|
||||
optparser.add_option('-p', '--project', default='nova', help='Project to generate stats for')
|
||||
optparser.add_option('-d', '--days', type='int', default=14, help='Number of days to consider')
|
||||
optparser.add_option('-r', '--raw', action='store_true', default=False, help='Um... Hard to explain. Try it and see')
|
||||
optparser.add_option('-p', '--project', default='nova',
|
||||
help='Project to generate stats for')
|
||||
optparser.add_option('-d', '--days', type='int', default=14,
|
||||
help='Number of days to consider')
|
||||
optparser.add_option('-u', '--user', default='russellb', help='gerrit user')
|
||||
optparser.add_option('-k', '--key', default=None, help='ssh key for gerrit')
|
||||
|
||||
options, args = optparser.parse_args()
|
||||
|
||||
client = paramiko.SSHClient()
|
||||
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
client.load_system_host_keys()
|
||||
client.connect('review.openstack.org', port=29418, key_filename='/home/soren/.ssh/statskey', username='usagestats')
|
||||
stdin, stdout, stderr = client.exec_command('gerrit query project:openstack/%s --all-approvals --patch-sets --format JSON' % options.project)
|
||||
|
||||
|
||||
changes = []
|
||||
for l in stdout:
|
||||
changes += [json.loads(l)]
|
||||
|
||||
pickle_fn = '%s-changes.pickle' % options.project
|
||||
|
||||
if os.path.isfile(pickle_fn):
|
||||
mtime = os.stat(pickle_fn).st_mtime
|
||||
if (time.time() - mtime) <= CACHE_AGE:
|
||||
with open(pickle_fn, 'r') as f:
|
||||
changes = pickle.load(f)
|
||||
|
||||
if len(changes) == 0:
|
||||
while True:
|
||||
client.connect('review.openstack.org', port=29418,
|
||||
key_filename=options.key, username=options.user)
|
||||
cmd = ('gerrit query project:openstack/%s '
|
||||
'--all-approvals --patch-sets --format JSON' % options.project)
|
||||
if len(changes) > 0:
|
||||
cmd += ' resume_sortkey:%s' % changes[-2]['sortKey']
|
||||
stdin, stdout, stderr = client.exec_command(cmd)
|
||||
for l in stdout:
|
||||
changes += [json.loads(l)]
|
||||
if changes[-1]['rowCount'] == 0:
|
||||
break
|
||||
|
||||
with open(pickle_fn, 'w') as f:
|
||||
pickle.dump(changes, f)
|
||||
|
||||
|
||||
reviews = []
|
||||
|
||||
@ -32,10 +89,9 @@ for change in changes:
|
||||
for review in patchset.get('approvals', []):
|
||||
reviews += [review]
|
||||
|
||||
if not options.raw:
|
||||
cut_off = datetime.datetime.now() - datetime.timedelta(days=options.days)
|
||||
ts = calendar.timegm(cut_off.timetuple())
|
||||
reviews = filter(lambda x:x['grantedOn'] > ts, reviews)
|
||||
cut_off = datetime.datetime.now() - datetime.timedelta(days=options.days)
|
||||
ts = calendar.timegm(cut_off.timetuple())
|
||||
reviews = filter(lambda x:x['grantedOn'] > ts, reviews)
|
||||
|
||||
def round_to_day(ts):
|
||||
SECONDS_PER_DAY = 60*60*24
|
||||
@ -44,12 +100,35 @@ def round_to_day(ts):
|
||||
reviewers = {}
|
||||
for review in reviews:
|
||||
reviewer = review['by'].get('username', 'unknown')
|
||||
if options.raw:
|
||||
if reviewer not in reviewers:
|
||||
reviewers[reviewer] = {}
|
||||
day = round_to_day(review['grantedOn'])
|
||||
reviewers[reviewer][day] = reviewers[reviewer].get(day, 0) + 1
|
||||
else:
|
||||
reviewers[reviewer] = reviewers.get(reviewer, 0) + 1
|
||||
reviewers.setdefault(reviewer,
|
||||
{'votes': {'-2': 0, '-1': 0, '0': 0, '1': 0, '2': 0}})
|
||||
reviewers[reviewer]['total'] = reviewers[reviewer].get('total', 0) + 1
|
||||
cur = reviewers[reviewer]['votes'][review['value']]
|
||||
reviewers[reviewer]['votes'][review['value']] = cur + 1
|
||||
|
||||
print json.dumps(reviewers, sort_keys=True, indent=4)
|
||||
#print json.dumps(reviewers, sort_keys=True, indent=4)
|
||||
|
||||
reviewers = [(v, k) for k, v in reviewers.iteritems()
|
||||
if k.lower() not in ('jenkins', 'smokestack')]
|
||||
reviewers.sort(reverse=True)
|
||||
|
||||
core_team = []
|
||||
core_team_name = CLIENT_MAP.get(options.project, options.project)
|
||||
core_team_fn = '%s-core-team.json' % core_team_name
|
||||
if os.path.isfile(core_team_fn):
|
||||
with open(core_team_fn, 'r') as f:
|
||||
core_team = json.loads(f.read())
|
||||
|
||||
print 'Reviews for the last %d days in %s' % (options.days, options.project)
|
||||
print '** -- %s-core team member' % core_team_name
|
||||
table = prettytable.PrettyTable(('Reviewer', 'Reviews (-2|-1|0|+1|+2)'))
|
||||
total = 0
|
||||
for k, v in reviewers:
|
||||
name = '%s%s' % (v, ' **' if v in core_team else '')
|
||||
r = '%d (%d|%d|%d|%d|%d)' % (k['total'],
|
||||
k['votes']['-2'], k['votes']['-1'], k['votes']['0'],
|
||||
k['votes']['1'], k['votes']['2'])
|
||||
table.add_row((name, r))
|
||||
total += k['total']
|
||||
print table
|
||||
print '\nTotal reviews: %d' % total
|
||||
|
Loading…
x
Reference in New Issue
Block a user