Commit 965d5686 authored by Thomas Lotze's avatar Thomas Lotze

re #2: rename date to day in preparation of using the date type next

parent bdc1e1b9
from collections import defaultdict
from datetime import date
from gitlab import Gitlab
import logging
......@@ -51,22 +52,22 @@ def fetch_project(project, filter):
for item in issues + merge_requests:
entries = defaultdict(float)
for author, date, seconds in fetch_times(item):
for author, day, seconds in fetch_times(item):
if filter['username'] not in (author, None):
continue
if (filter['start'] or '') > date:
if (filter['start'] or '') > day:
continue
if (filter['end'] or '9999-99-99') < date:
if (filter['end'] or '9999-99-99') < day:
continue
entries[author, date] += seconds
entries[author, day] += seconds
for (author, date), seconds in entries.items():
for (author, day), seconds in entries.items():
yield dict(
item=item,
author=author,
date=date,
day=day,
hours=round(seconds/3600, 2),
)
......@@ -75,15 +76,15 @@ def fetch_times(item):
# Time tracking in Gitlab works like this: For each comment (note)
# containing a time-tracking quick action, an additional system note is
# created at that time, which either adds or subtracts time for an
# arbitrary date, or removes any previous time spent. Removal invalidates
# arbitrary day, or removes any previous time spent. Removal invalidates
# any tracking notes prior to its creation. Moving an issue to another
# project leaves time tracking valid on the original and invalidates all
# time-tracking notes copied to the new issue.
# Tracking done after the latest time removal or issue creation by moving
# ("moved from") always applies, regardless of the date it applies to.
# ("moved from") always applies, regardless of the day it applies to.
# Thus, we need to consider any and all time tracking done after the
# latest removal or "move from", which may track time for any date, in any
# latest removal or "move from", which may track time for any day, in any
# order. We do this by iterating through notes backwards by creation date
# until we find a removal or "move from" note.
notes = item.notes.list(
......@@ -99,19 +100,19 @@ def fetch_times(item):
break
sep = ' of time spent at '
action, _, date = note.body.partition(sep)
action, _, day = note.body.partition(sep)
if _ != sep:
continue
seconds = parse_action(action)
total += seconds
yield note.author['username'], date, seconds
yield note.author['username'], day, seconds
else:
# We've now visited all notes up until item creation. Time may also
# have been tracked by a quick action in the original project
# description (but not later edits). In that case, no system note is
# added and if that time tracking was applied to some date, that date
# added and if that time tracking was applied to some day, that day
# is lost. We can only infer the amount of time as the difference
# between the total time as given by the item's time stats and the
# total time tracked by notes, and apply it to the creation date of
......@@ -120,12 +121,12 @@ def fetch_times(item):
if not seconds:
return
# Dates come without time zones and we don't know how Gitlab converts
# Days come without time zones and we don't know how Gitlab converts
# from time-zone aware to unaware, so we don't think about time zones
# and hope for the best.
date = item.created_at.split('T')[0]
day = item.created_at.split('T')[0]
yield item.author['username'], date, seconds
yield item.author['username'], day, seconds
def parse_action(action):
......
......@@ -10,18 +10,18 @@ def report_csv(entries_by_project, csvpath, filter):
with open(csvpath, 'w', newline='') as csvfile:
csvwriter = csv.DictWriter(
csvfile,
['Project', 'Issue/MR', 'Author', 'Date', 'Hours'],
['Project', 'Issue/MR', 'Author', 'Day', 'Hours'],
quoting=csv.QUOTE_NONNUMERIC,
)
csvwriter.writeheader()
def writerow(project, item, author, date, hours):
def writerow(project, item, author, day, hours):
csvwriter.writerow({
'Project': project.name_with_namespace,
'Issue/MR': '{}: {}'.format(
item.references['short'], item.title),
'Author': author,
'Date': date,
'Day': day,
'Hours': hours,
})
......@@ -56,7 +56,7 @@ def report_project(project, entries, writerow):
item.iid,
item)
author = entry['author']
times[item][author].append((entry['date'], entry['hours']))
times[item][author].append((entry['day'], entry['hours']))
totals_by_item[item] += entry['hours']
total = sum(totals_by_item.values())
......@@ -73,10 +73,10 @@ def report_project(project, entries, writerow):
item.title))
for author, time_log in sorted(times_by_author.items()):
for date, hours in sorted(time_log):
for day, hours in sorted(time_log):
print('{}{}: {:6.2f} ({})'.format(
8*' ', date, hours, author))
writerow(project, item, author, date, hours)
8*' ', day, hours, author))
writerow(project, item, author, day, hours)
print()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment