2024-10-13 12:31:23 +00:00
|
|
|
import datetime
|
|
|
|
from collections.abc import Iterator
|
|
|
|
|
2024-10-13 13:20:30 +00:00
|
|
|
from personal_data.activity import HIDDEN_LABEL_CATEGORY, Label, RealizedActivitySample
|
2024-10-13 12:31:23 +00:00
|
|
|
|
|
|
|
ZERO_DURATION = datetime.timedelta(seconds=0)
|
|
|
|
HOUR = datetime.timedelta(hours=1)
|
|
|
|
MINUTE = datetime.timedelta(minutes=1)
|
|
|
|
|
|
|
|
|
|
|
|
def fmt_year_ranges_internal(years: list[int]) -> Iterator[str]:
|
|
|
|
years = sorted(years)
|
|
|
|
for idx, year in enumerate(years):
|
|
|
|
at_end = idx == len(years) - 1
|
|
|
|
range_before = idx > 0 and years[idx - 1] == year - 1
|
|
|
|
range_after = not at_end and years[idx + 1] == year + 1
|
|
|
|
|
|
|
|
if not range_before or not range_after:
|
|
|
|
yield str(year)
|
|
|
|
|
|
|
|
if not at_end:
|
|
|
|
if not range_before and range_after:
|
|
|
|
yield '-'
|
|
|
|
elif not range_after:
|
|
|
|
yield ','
|
|
|
|
|
|
|
|
|
|
|
|
def fmt_year_ranges(years: list[int]) -> str:
|
|
|
|
return ''.join(list(fmt_year_ranges_internal(years)))
|
|
|
|
|
|
|
|
|
2024-10-13 13:20:18 +00:00
|
|
|
def fmt_line(label: Label, total_time: datetime.timedelta) -> str:
|
2024-10-13 12:31:23 +00:00
|
|
|
hours = int(total_time / HOUR)
|
|
|
|
minutes = int((total_time - hours * HOUR) / MINUTE)
|
2024-10-13 13:20:18 +00:00
|
|
|
return f' {label.category:10} {label.label:40} {hours:-4d}h {minutes:-2d}m'
|
2024-10-13 12:31:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def generate_report(
|
2024-10-13 13:05:55 +00:00
|
|
|
samples: list[RealizedActivitySample],
|
2024-10-13 12:31:23 +00:00
|
|
|
) -> Iterator[str]:
|
|
|
|
# Time spent per label
|
2024-10-13 13:20:18 +00:00
|
|
|
time_per_label: dict[Label, datetime.timedelta] = {}
|
|
|
|
years_per_label: dict[Label, set[int]] = {}
|
2024-10-13 12:31:23 +00:00
|
|
|
for sample in samples:
|
|
|
|
duration = sample.end_at - sample.start_at
|
|
|
|
|
|
|
|
for label in sample.labels:
|
|
|
|
time_per_label.setdefault(label, ZERO_DURATION)
|
|
|
|
time_per_label[label] += duration
|
|
|
|
years_per_label.setdefault(label, set()).add(sample.end_at.year)
|
|
|
|
|
|
|
|
del sample, duration
|
|
|
|
|
|
|
|
time_and_label = [(duration, label) for label, duration in time_per_label.items()]
|
|
|
|
time_and_label.sort(reverse=True)
|
|
|
|
|
|
|
|
#
|
|
|
|
yield '-' * 66
|
|
|
|
yield '\n'
|
2024-10-13 13:20:18 +00:00
|
|
|
for total_time, label in time_and_label:
|
|
|
|
if label.category == HIDDEN_LABEL_CATEGORY:
|
2024-10-13 12:31:23 +00:00
|
|
|
continue
|
|
|
|
|
2024-10-13 13:20:18 +00:00
|
|
|
yield fmt_line(label, total_time)
|
2024-10-13 12:31:23 +00:00
|
|
|
yield ' ('
|
2024-10-13 13:20:18 +00:00
|
|
|
yield fmt_year_ranges(years_per_label.get(label, []))
|
2024-10-13 12:31:23 +00:00
|
|
|
yield ')'
|
|
|
|
yield '\n'
|
|
|
|
del label, total_time
|
|
|
|
|
|
|
|
yield '-' * 66
|
|
|
|
yield '\n'
|
|
|
|
|
2024-10-13 13:20:18 +00:00
|
|
|
label_total = Label(HIDDEN_LABEL_CATEGORY, 'total')
|
|
|
|
yield fmt_line(label_total, time_per_label.get(label_total, ZERO_DURATION))
|
2024-10-13 12:31:23 +00:00
|
|
|
yield '\n'
|