1
0

Incredibly simple report

This commit is contained in:
Jon Michael Aanes 2024-06-03 23:49:42 +02:00
parent fed527be4b
commit 002caa1874
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA

View File

@ -1,4 +1,5 @@
import argparse import argparse
import sys
import time import time
import dataclasses import dataclasses
import git import git
@ -11,7 +12,7 @@ def parse_arguments():
parser.add_argument('repositories', action='extend', nargs='+', type=Path) parser.add_argument('repositories', action='extend', nargs='+', type=Path)
return parser.parse_args() return parser.parse_args()
@dataclasses.dataclass(frozen=True) @dataclasses.dataclass(frozen=True, order=True)
class WorkSample: class WorkSample:
registered_at: datetime.datetime registered_at: datetime.datetime
labels: Sequence[str] labels: Sequence[str]
@ -19,15 +20,48 @@ class WorkSample:
def get_samples_from_project(repo: git.Repo) -> Iterator[WorkSample]: def get_samples_from_project(repo: git.Repo) -> Iterator[WorkSample]:
labels = [] labels = []
labels.append('project:git_time_tracker') labels.append('project:'+repo.remotes.origin.url)
print(repo.remotes.origin.repo)
# TODO: Branch on main or master or default # TODO: Branch on main or master or default
for commit in repo.iter_commits('main'): 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.authored_date, tz=datetime.UTC), tuple(labels))
yield WorkSample(datetime.datetime.fromtimestamp(commit.committed_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(): def main():
args = parse_arguments() args = parse_arguments()
@ -35,5 +69,9 @@ def main():
for repo_path in args.repositories: for repo_path in args.repositories:
repo = git.Repo(repo_path) repo = git.Repo(repo_path)
shared_time_stamps |= set(get_samples_from_project(repo)) 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)