diff --git a/git_time_tracker/__init__.py b/git_time_tracker/__init__.py index c9daf55..6465963 100644 --- a/git_time_tracker/__init__.py +++ b/git_time_tracker/__init__.py @@ -1,4 +1,5 @@ import argparse +import sys import time import dataclasses import git @@ -11,7 +12,7 @@ def parse_arguments(): parser.add_argument('repositories', action='extend', nargs='+', type=Path) return parser.parse_args() -@dataclasses.dataclass(frozen=True) +@dataclasses.dataclass(frozen=True, order=True) class WorkSample: registered_at: datetime.datetime labels: Sequence[str] @@ -19,15 +20,48 @@ class WorkSample: def get_samples_from_project(repo: git.Repo) -> Iterator[WorkSample]: labels = [] - labels.append('project:git_time_tracker') - - print(repo.remotes.origin.repo) + labels.append('project:'+repo.remotes.origin.url) # TODO: Branch on main or master or default for commit in repo.iter_commits('main'): 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)) +DEFAULT_EST_TIME=datetime.timedelta(hours=1) + +ZERO_DURATION = datetime.timedelta(seconds = 0) + +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()] + time_and_label.sort() + + # + yield 'Time report:\n' + for (total_time, label) in time_and_label: + yield ' ' + yield label + yield ' ' + yield str(total_time) + yield '\n' + del label, total_time + + yield '\n' + def main(): args = parse_arguments() @@ -35,5 +69,9 @@ def main(): for repo_path in args.repositories: repo = git.Repo(repo_path) shared_time_stamps |= set(get_samples_from_project(repo)) - print('derp', shared_time_stamps) + + shared_time_stamps = sorted(shared_time_stamps) + + for t in generate_report(shared_time_stamps): + sys.stdout.write(t)