2024-06-03 20:42:05 +00:00
|
|
|
import argparse
|
2024-06-03 21:49:42 +00:00
|
|
|
import sys
|
2024-06-03 21:01:45 +00:00
|
|
|
import time
|
|
|
|
import dataclasses
|
|
|
|
import git
|
|
|
|
import datetime
|
|
|
|
from collections.abc import Iterator, Sequence
|
2024-06-03 21:13:16 +00:00
|
|
|
from pathlib import Path
|
2024-06-03 20:42:05 +00:00
|
|
|
|
|
|
|
def parse_arguments():
|
|
|
|
parser = argparse.ArgumentParser()
|
2024-06-03 21:13:16 +00:00
|
|
|
parser.add_argument('repositories', action='extend', nargs='+', type=Path)
|
2024-06-03 20:42:05 +00:00
|
|
|
return parser.parse_args()
|
|
|
|
|
2024-06-03 21:49:42 +00:00
|
|
|
@dataclasses.dataclass(frozen=True, order=True)
|
2024-06-03 21:01:45 +00:00
|
|
|
class WorkSample:
|
|
|
|
registered_at: datetime.datetime
|
|
|
|
labels: Sequence[str]
|
|
|
|
|
|
|
|
|
2024-06-03 22:02:49 +00:00
|
|
|
def determine_default(repo: git.Repo):
|
|
|
|
try:
|
|
|
|
repo.commit('main')
|
|
|
|
return 'main'
|
|
|
|
except:
|
|
|
|
return 'master'
|
|
|
|
|
|
|
|
HIDDEN_LABEL_PREFIX = '__'
|
|
|
|
HIDDEN_LABEL_TOTAL = HIDDEN_LABEL_PREFIX + 'TOTAL'
|
|
|
|
|
2024-06-03 21:01:45 +00:00
|
|
|
def get_samples_from_project(repo: git.Repo) -> Iterator[WorkSample]:
|
2024-06-03 22:02:49 +00:00
|
|
|
labels = [HIDDEN_LABEL_TOTAL]
|
|
|
|
labels.append(repo.remotes.origin.url.removeprefix('git@gitfub.space:'))
|
2024-06-03 21:01:45 +00:00
|
|
|
|
2024-06-03 21:13:16 +00:00
|
|
|
# TODO: Branch on main or master or default
|
2024-06-03 22:02:49 +00:00
|
|
|
|
|
|
|
repo.commit()
|
|
|
|
|
|
|
|
for commit in repo.iter_commits(determine_default(repo)):
|
2024-06-03 21:13:16 +00:00
|
|
|
yield WorkSample(datetime.datetime.fromtimestamp(commit.authored_date, tz=datetime.UTC), tuple(labels))
|
|
|
|
yield WorkSample(datetime.datetime.fromtimestamp(commit.committed_date, tz=datetime.UTC), tuple(labels))
|
2024-06-03 20:42:05 +00:00
|
|
|
|
2024-06-03 21:49:42 +00:00
|
|
|
DEFAULT_EST_TIME=datetime.timedelta(hours=1)
|
|
|
|
|
|
|
|
ZERO_DURATION = datetime.timedelta(seconds = 0)
|
2024-06-03 22:02:49 +00:00
|
|
|
HOUR = datetime.timedelta(hours = 1)
|
2024-06-03 21:49:42 +00:00
|
|
|
|
|
|
|
def generate_report(samples: list[WorkSample]) -> Iterator[str]:
|
|
|
|
|
|
|
|
# Time spent per label
|
|
|
|
time_per_label: dict[str, datetime.timedelta] = {}
|
|
|
|
prev_time = datetime.datetime.fromtimestamp(0, datetime.UTC)
|
|
|
|
for sample in samples:
|
|
|
|
est_time: datetime.timedelta = DEFAULT_EST_TIME
|
|
|
|
est_time = min(sample.registered_at - prev_time, est_time)
|
|
|
|
|
|
|
|
for label in sample.labels:
|
|
|
|
time_per_label.setdefault(label,ZERO_DURATION)
|
|
|
|
time_per_label[label] += est_time
|
|
|
|
|
|
|
|
prev_time = sample.registered_at
|
|
|
|
del sample, est_time
|
|
|
|
|
|
|
|
time_and_label = [(duration, label) for label,duration in time_per_label.items()]
|
2024-06-03 22:02:49 +00:00
|
|
|
time_and_label.sort(reverse=True)
|
2024-06-03 21:49:42 +00:00
|
|
|
|
|
|
|
#
|
2024-06-03 22:02:49 +00:00
|
|
|
yield '-' * 56
|
|
|
|
yield '\n'
|
2024-06-03 21:49:42 +00:00
|
|
|
for (total_time, label) in time_and_label:
|
2024-06-03 22:02:49 +00:00
|
|
|
if label.startswith(HIDDEN_LABEL_PREFIX):
|
|
|
|
continue
|
|
|
|
yield f' {label:40} {total_time / HOUR:-4.0f} hours\n'
|
2024-06-03 21:49:42 +00:00
|
|
|
del label, total_time
|
|
|
|
|
2024-06-03 22:02:49 +00:00
|
|
|
yield '-' * 56
|
2024-06-03 21:49:42 +00:00
|
|
|
yield '\n'
|
|
|
|
|
2024-06-03 22:02:49 +00:00
|
|
|
yield ' {label:40} {hours:-4.0f} hours\n'.format(label='TOTAL', hours = time_per_label[HIDDEN_LABEL_TOTAL] / HOUR)
|
|
|
|
|
2024-06-03 20:42:05 +00:00
|
|
|
def main():
|
|
|
|
args = parse_arguments()
|
|
|
|
|
2024-06-03 21:13:16 +00:00
|
|
|
shared_time_stamps: set[WorkSample] = set()
|
|
|
|
for repo_path in args.repositories:
|
|
|
|
repo = git.Repo(repo_path)
|
|
|
|
shared_time_stamps |= set(get_samples_from_project(repo))
|
2024-06-03 21:49:42 +00:00
|
|
|
|
|
|
|
shared_time_stamps = sorted(shared_time_stamps)
|
|
|
|
|
|
|
|
for t in generate_report(shared_time_stamps):
|
|
|
|
sys.stdout.write(t)
|
2024-06-03 20:42:05 +00:00
|
|
|
|