factor common code from management graph commands

This commit is contained in:
Frédéric Péters 2017-10-14 18:27:15 +02:00
parent aa0fd92f1f
commit 519c0abc88
4 changed files with 57 additions and 84 deletions

View File

@ -3,43 +3,24 @@ import matplotlib
import matplotlib.dates
import matplotlib.pyplot as plt
from django.core.management.base import BaseCommand
from django.utils.dateparse import parse_date
from eodb.events.models import Commit
from .common import GraphCommand
class Command(BaseCommand):
class Command(GraphCommand):
def add_arguments(self, parser):
parser.add_argument('--username', metavar='USERNAME')
parser.add_argument('--datemin', metavar='DATEMIN')
parser.add_argument('--datemax', metavar='DATEMAX')
super(Command, self).add_arguments(parser)
parser.add_argument('--groupby', metavar='TIME UNIT', default='weeks',
help='aggregate over weeks (default) / months / years')
parser.add_argument('--committime', action='store_true',
help='use commit datetime instead of author datetime')
def handle(self, *args, **options):
filters = {'author_email__endswith': '@entrouvert.com'}
title = 'Git activity'
if options.get('username'):
filters['author_email__startswith'] = options['username'] + '@'
title += ' for %s' % options['username']
datetime_var = 'author_datetime'
if options.get('committime'):
datetime_var = 'commit_datetime'
if options.get('datemin'):
filters[datetime_var + '__gte'] = parse_date(options['datemin'])
title += ' from %s' % options['datemin']
if options.get('datemax'):
filters[datetime_var + '__lt'] = parse_date(options['datemax'])
title += ' until %s' % options['datemax']
events = {}
for commit in Commit.objects.filter(**filters):
for commit in self.get_commits(options):
commit_date = getattr(commit, datetime_var).date()
if options.get('groupby') == 'weeks':
graph_date = commit_date - datetime.timedelta(days=commit_date.weekday())
@ -56,7 +37,6 @@ class Command(BaseCommand):
dates = sorted(events.keys())
values = [events[x] for x in dates]
plt.title(title)
plt.plot(dates, values)
plt.gca().xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%m/%Y'))
plt.show()
self.plot(options)

View File

@ -0,0 +1,44 @@
import matplotlib.pyplot as plt
from django.core.management.base import BaseCommand
from django.utils.dateparse import parse_date
from eodb.events.models import Commit
class GraphCommand(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('--username', metavar='USERNAME')
parser.add_argument('--datemin', metavar='DATEMIN')
parser.add_argument('--datemax', metavar='DATEMAX')
parser.add_argument('--committime', action='store_true',
help='use commit datetime instead of author datetime')
def get_commits(self, options):
filters = {'author_email__endswith': '@entrouvert.com'}
if options.get('username'):
filters['author_email__startswith'] = options['username'] + '@'
datetime_var = 'author_datetime'
if options.get('committime'):
datetime_var = 'commit_datetime'
if options.get('datemin'):
filters[datetime_var + '__gte'] = parse_date(options['datemin'])
if options.get('datemax'):
filters[datetime_var + '__lt'] = parse_date(options['datemax'])
return Commit.objects.filter(**filters)
def get_title(self, options):
title = 'Git activity'
if options.get('username'):
title += ' for %s' % options['username']
if options.get('datemin'):
title += ' from %s' % options['datemin']
if options.get('datemax'):
title += ' until %s' % options['datemax']
return title
def plot(self, options):
plt.title(self.get_title(options))
plt.show()

View File

@ -1,19 +1,10 @@
import matplotlib.pyplot as plt
import numpy as np
from django.core.management.base import BaseCommand
from django.utils.dateparse import parse_date
from eodb.events.models import Commit
from .common import GraphCommand
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('--username', metavar='USERNAME')
parser.add_argument('--datemin', metavar='DATEMIN')
parser.add_argument('--datemax', metavar='DATEMAX')
parser.add_argument('--committime', action='store_true',
help='use commit datetime instead of author datetime')
class Command(GraphCommand):
def handle(self, *args, **options):
infos = {}
@ -21,31 +12,16 @@ class Command(BaseCommand):
for j in range(24):
infos[(i, j)] = 0
filters = {'author_email__endswith': '@entrouvert.com'}
title = 'Git activity'
if options.get('username'):
filters['author_email__startswith'] = options['username'] + '@'
title += ' for %s' % options['username']
datetime_var = 'author_datetime'
if options.get('committime'):
datetime_var = 'commit_datetime'
if options.get('datemin'):
filters[datetime_var + '__gte'] = parse_date(options['datemin'])
title += ' from %s' % options['datemin']
if options.get('datemax'):
filters[datetime_var + '__lt'] = parse_date(options['datemax'])
title += ' until %s' % options['datemax']
for commit in Commit.objects.filter(**filters):
for commit in self.get_commits(options):
coords = (getattr(commit, datetime_var).weekday(), getattr(commit, datetime_var).hour)
infos[coords] = infos[coords] + 1
draw_punchcard(infos)
plt.title(title)
plt.show()
self.plot(options)
# https://stackoverflow.com/a/14850998
def draw_punchcard(infos,

View File

@ -1,52 +1,25 @@
import datetime
import matplotlib
import matplotlib.dates
import matplotlib.pyplot as plt
from django.core.management.base import BaseCommand
from django.utils.dateparse import parse_date
from eodb.events.models import Commit
from .common import GraphCommand
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('--username', metavar='USERNAME')
parser.add_argument('--datemin', metavar='DATEMIN')
parser.add_argument('--datemax', metavar='DATEMAX')
parser.add_argument('--committime', action='store_true',
help='use commit datetime instead of author datetime')
class Command(GraphCommand):
def handle(self, *args, **options):
filters = {'author_email__endswith': '@entrouvert.com'}
title = 'Git activity'
if options.get('username'):
filters['author_email__startswith'] = options['username'] + '@'
title += ' for %s' % options['username']
datetime_var = 'author_datetime'
if options.get('committime'):
datetime_var = 'commit_datetime'
if options.get('datemin'):
filters[datetime_var + '__gte'] = parse_date(options['datemin'])
title += ' from %s' % options['datemin']
if options.get('datemax'):
filters[datetime_var + '__lt'] = parse_date(options['datemax'])
title += ' until %s' % options['datemax']
event_dates = []
event_times = []
for commit in Commit.objects.filter(**filters):
for commit in self.get_commits(options):
graph_date = getattr(commit, datetime_var)
event_dates.append(matplotlib.dates.date2num(graph_date.date()))
event_times.append(graph_date.hour + graph_date.minute / 60. + graph_date.second / 3600)
plt.title(title)
plt.scatter(event_dates, event_times, alpha=0.3)
plt.gca().xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%m/%Y'))
plt.gca().set_ylim([0, 24])
plt.yticks(range(24))
plt.show()
self.plot(options)