1
0

Year labels

This commit is contained in:
Jon Michael Aanes 2024-06-08 15:09:57 +02:00
parent 173de2c252
commit c1b95997d9
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
2 changed files with 40 additions and 1 deletions

View File

@ -73,10 +73,29 @@ HOUR = datetime.timedelta(hours=1)
MINUTE = datetime.timedelta(minutes=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)))
def fmt_line(label_type: str, label: str, total_time: datetime.timedelta) -> str: def fmt_line(label_type: str, label: str, total_time: datetime.timedelta) -> str:
hours = int(total_time / HOUR) hours = int(total_time / HOUR)
minutes = int((total_time - hours*HOUR)/MINUTE) minutes = int((total_time - hours*HOUR)/MINUTE)
return f' {label_type:8} {label:40} {hours:-4d}h {minutes:-2d}m\n' return f' {label_type:8} {label:40} {hours:-4d}h {minutes:-2d}m'
def generate_report(samples: list[WorkSample]) -> Iterator[str]: def generate_report(samples: list[WorkSample]) -> Iterator[str]:
SAMPLE_FILTER = {} SAMPLE_FILTER = {}
@ -84,6 +103,7 @@ def generate_report(samples: list[WorkSample]) -> Iterator[str]:
# Time spent per label # Time spent per label
time_per_label: dict[str, datetime.timedelta] = {} time_per_label: dict[str, datetime.timedelta] = {}
years_per_label: dict[str, set[int]] = {}
prev_time = datetime.datetime.fromtimestamp(0, datetime.UTC) prev_time = datetime.datetime.fromtimestamp(0, datetime.UTC)
for sample in samples: for sample in samples:
est_time: datetime.timedelta = DEFAULT_EST_TIME est_time: datetime.timedelta = DEFAULT_EST_TIME
@ -97,6 +117,7 @@ def generate_report(samples: list[WorkSample]) -> Iterator[str]:
for label in sample.labels: for label in sample.labels:
time_per_label.setdefault(label, ZERO_DURATION) time_per_label.setdefault(label, ZERO_DURATION)
time_per_label[label] += est_time time_per_label[label] += est_time
years_per_label.setdefault(label,set()).add(sample.registered_at.year)
prev_time = sample.registered_at prev_time = sample.registered_at
del sample, est_time del sample, est_time
@ -119,12 +140,17 @@ def generate_report(samples: list[WorkSample]) -> Iterator[str]:
label_type = '' # TODO label_type = '' # TODO
yield fmt_line(label_type, label, total_time) yield fmt_line(label_type, label, total_time)
yield ' ('
yield fmt_year_ranges(years_per_label.get(label_and_type,[]))
yield ')'
yield '\n'
del label, total_time del label, total_time
yield '-' * 66 yield '-' * 66
yield '\n' yield '\n'
yield fmt_line('', 'TOTAL', time_per_label.get(HIDDEN_LABEL_TOTAL, ZERO_DURATION)) yield fmt_line('', 'TOTAL', time_per_label.get(HIDDEN_LABEL_TOTAL, ZERO_DURATION))
yield '\n'
def main(): def main():
logging.basicConfig() logging.basicConfig()

13
test/test_util.py Normal file
View File

@ -0,0 +1,13 @@
import git_time_tracker
def test_year_ranges_1():
assert git_time_tracker.fmt_year_ranges([1,2,3]) == '1-3'
def test_year_ranges_2():
assert git_time_tracker.fmt_year_ranges([1,3]) == '1,3'
def test_year_ranges_3():
assert git_time_tracker.fmt_year_ranges([1,2,4]) == '1-2,4'
def test_year_ranges_4():
assert git_time_tracker.fmt_year_ranges([1,2,4,5]) == '1-2,4-5'