From de461a9d567f38a018f91b10df0f71b6c17ea45d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Bj=C3=A4reholt?= Date: Thu, 30 Nov 2023 21:06:24 +0100 Subject: [PATCH] fix: output all + top 3 top-level categories in checkin (instead of hardcoded) --- aw_notify/main.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/aw_notify/main.py b/aw_notify/main.py index cc08f10..b19619a 100644 --- a/aw_notify/main.py +++ b/aw_notify/main.py @@ -62,7 +62,7 @@ def _cache_ttl(*args, **kwargs) -> T: @cache_ttl(60) -def get_time(date=None) -> dict[str, timedelta]: +def get_time(date=None, top_level_only=True) -> dict[str, timedelta]: """ Returns a dict with the time spent today (or for `date`) for each category. """ @@ -94,8 +94,6 @@ def get_time(date=None) -> dict[str, timedelta]: res = aw.query(query, timeperiods)[0] res["cat_events"] += [{"data": {"$category": ["All"]}, "duration": res["duration"]}] - top_level_only = True - cat_time: dict[str, timedelta] = defaultdict(timedelta) for e in res["cat_events"]: if top_level_only: @@ -319,17 +317,15 @@ def send_checkin(title="Time today", date=None): Sends a summary notification of the day. Meant to be sent at a particular time, like at the end of a working day (e.g. 5pm). """ - # TODO: make configurable which categories to show - categories = list(set(["All", "Work", "Uncategorized"])) cat_time = get_time(date=date) - time_spent = [cat_time.get(c, timedelta()) for c in categories] + + # get the 4 top categories by time spent. top_categories = [ (c, to_hms(t)) - for c, t in sorted( - zip(categories, time_spent), key=lambda x: x[1], reverse=True - ) + for c, t in sorted(cat_time.items(), key=lambda x: x[1], reverse=True) if t > 0.02 * cat_time["All"] - ] + ][:4] + msg = "" msg += "\n".join(f"- {c}: {t}" for c, t in top_categories) if msg: