Skip to content

Instantly share code, notes, and snippets.

@lamchau
Last active November 8, 2016 22:57
Show Gist options
  • Save lamchau/e78b4d7604b5a19ed77d1de0e6be6b19 to your computer and use it in GitHub Desktop.
Save lamchau/e78b4d7604b5a19ed77d1de0e6be6b19 to your computer and use it in GitHub Desktop.
requested by management that judges productivity by PRs
#!/usr/bin/env python
import subprocess
import collections
import re
from prettytable import PrettyTable
from datetime import timedelta, date
from dateutil.parser import parse
from subprocess import PIPE, STDOUT
from itertools import izip
# def daterange(start_date, end_date):
# for n in range(int((end_date - start_date).days)):
# yield start_date + timedelta(n)
def format_date(date):
return date.strftime("%Y-%m-%d")
def chomp(str, suffix):
index = len(suffix)
return str[:-index] if str.endswith(suffix) else str
def as_dict(array):
array = [reversed(x) for x in array]
metrics = {}
for k, v in array:
key = chomp(k, "s")
metrics.setdefault(key, int(v))
return metrics
def pairwise(iterable):
a = iter(iterable)
return izip(a, a)
def update(collection, author, metrics):
current_stat = collection.get(author)
metrics = as_dict(metrics)
if current_stat:
for key, value in metrics.iteritems():
current_stat[key] = current_stat.setdefault(key, 0) + value
else:
current_stat = metrics
collection[author] = current_stat
# constants
GIT_STAT_REGEX = re.compile(r"(\d+) (\w+)")
AUTHOR_REGEX = re.compile(r"(\w+\.\w+)(?:@your-company.com)")
START_DATE = parse("2016-03-02")
# END_DATE = parse("2016-03-01")
END_DATE = date.today()
# git log --after=2016-03-01 --shortstat --pretty="%cE" --no-merges
GIT_COMMAND = [
'git',
'log',
'--shortstat',
'--before=%s' % format_date(END_DATE),
'--after=%s' % format_date(START_DATE),
'--pretty="%cE"',
'--no-merges']
# script
proc = subprocess.Popen(GIT_COMMAND, shell=True, stdout=PIPE, stderr=STDOUT)
authors = {}
lines = filter(lambda x: len(x), (line.strip() for line in proc.stdout))
for author, stat in pairwise(lines):
metrics = GIT_STAT_REGEX.findall(stat)
usernames = AUTHOR_REGEX.findall(author)
for user in usernames:
update(authors, user, metrics)
table = PrettyTable(["author", "files", "add", "deleted", "net"])
authors = collections.OrderedDict(sorted(authors.items()))
print START_DATE.strftime("%Y-%m-%d"), END_DATE.strftime("%Y-%m-%d")
for author, stat in authors.iteritems():
files = stat["file"]
added = stat["insertion"]
deleted = stat["deletion"]
net = added - deleted
table.add_row([author, files, added, deleted, net])
print table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment