This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
eodb/eodb/events/management/commands/activity.py

58 lines
2.3 KiB
Python

import datetime
import matplotlib
import matplotlib.dates
import matplotlib.pyplot as plt
from .common import GraphCommand
class Command(GraphCommand):
def add_arguments(self, parser):
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('--modulecount', type=int,
help='count number of modules instead of commits '
'(value is minimum number of commits to be counted in)')
def handle(self, *args, **options):
title = 'Git activity'
datetime_var = 'author_datetime'
if options.get('committime'):
datetime_var = 'commit_datetime'
events = {}
for commit in self.get_events(options):
commit_date = getattr(commit, datetime_var).date()
if options.get('groupby') == 'weeks':
graph_date = commit_date - datetime.timedelta(days=commit_date.weekday())
elif options.get('groupby') == 'months':
graph_date = commit_date.replace(day=1)
elif options.get('groupby') == 'years':
graph_date = commit_date.replace(month=1, day=1)
date = matplotlib.dates.date2num(graph_date)
if options.get('modulecount'):
if not date in events:
events[date] = {}
if not commit.module in events[date]:
events[date][commit.module] = 0
events[date][commit.module] += 1
else:
if not date in events:
events[date] = 0
events[date] += 1
dates = sorted(events.keys())
if options.get('modulecount'):
values = [len([y for y in events[x].values() if y > options.get('modulecount')]) for x in dates]
else:
values = [events[x] for x in dates]
plt.bar(dates, values, width=20)
plt.gca().xaxis.set_major_locator(matplotlib.dates.YearLocator())
plt.gca().xaxis.set_minor_locator(matplotlib.dates.MonthLocator())
plt.gca().xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%m/%Y'))
self.plot(options)