From e173325758973852ecbfbd5b648b2b0c6e5b8dff Mon Sep 17 00:00:00 2001 From: bain Date: Tue, 28 Mar 2023 19:47:38 +0200 Subject: [PATCH] group similar entries --- toggl2sheets.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/toggl2sheets.py b/toggl2sheets.py index 04c7ec3..943a6d4 100644 --- a/toggl2sheets.py +++ b/toggl2sheets.py @@ -124,8 +124,9 @@ def get_toggl_entries( None, ) - # construct a dict of d["project name"] = [[date, description, duration (hours), wage], ...] - out = defaultdict(list) + # construct a dict of entries d["project name"][(date, description)] = duration + # entries on the same day with the same description get aggregated together + packed = defaultdict(lambda: defaultdict(int)) for entry in reversed(entries): if not valid_entry(entry): continue @@ -134,14 +135,15 @@ def get_toggl_entries( if end <= last_edit: continue # get better accuracy than toggl lets us in their requests - out[projects[entry["project_id"]]].append( - [ - start.astimezone().strftime("%-d. %-m. %Y"), - entry["description"], - round(entry["duration"] / 60 / 60, 2), - HOURLY_WAGE, - ] - ) + packed[projects[entry["project_id"]]][(start.astimezone().strftime("%-d. %-m. %Y"), entry["description"])] += entry["duration"] / 60 / 60 + + # unpack into the final output of d["project name"] = [[date, description, duration, hourly_wage], ...] + out = {} + for project, entries in packed.items(): + unpacked = [] + for metadata, duration in entries.items(): + unpacked.append([*metadata, round(duration, 2), HOURLY_WAGE]) + out[project] = unpacked return out, last_entry