1
0

Label filter

This commit is contained in:
Jon Michael Aanes 2024-06-04 00:30:55 +02:00
parent 6f42fe09b4
commit 2db419c780
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA

View File

@ -39,18 +39,20 @@ def determine_project_name(repo: git.Repo) -> str:
return Path(repo.working_tree_dir).name return Path(repo.working_tree_dir).name
def get_samples_from_project(repo: git.Repo) -> Iterator[WorkSample]: def get_samples_from_project(repo: git.Repo) -> Iterator[WorkSample]:
labels = [HIDDEN_LABEL_TOTAL] project_name = determine_project_name(repo)
name = determine_project_name(repo) assert project_name is not None
assert name is not None
labels.append(name)
# TODO: Branch on main or master or default # TODO: Branch on main or master or default
repo.commit() repo.commit()
for commit in repo.iter_commits(determine_default(repo)): for commit in repo.iter_commits(determine_default(repo)):
labels = [HIDDEN_LABEL_TOTAL]
labels.append('project:' + project_name)
labels.append('author:' + commit.author.email)
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))
del labels
DEFAULT_EST_TIME=datetime.timedelta(hours=1) DEFAULT_EST_TIME=datetime.timedelta(hours=1)
@ -58,6 +60,9 @@ ZERO_DURATION = datetime.timedelta(seconds = 0)
HOUR = datetime.timedelta(hours = 1) HOUR = datetime.timedelta(hours = 1)
def generate_report(samples: list[WorkSample]) -> Iterator[str]: def generate_report(samples: list[WorkSample]) -> Iterator[str]:
#SAMPLE_FILTER = {}
SAMPLE_FILTER = {'author:jonjmaa@gmail.com', 'author:jon.michael.aanes@secata.com'}
LABEL_FILTER = {'project'}
# Time spent per label # Time spent per label
time_per_label: dict[str, datetime.timedelta] = {} time_per_label: dict[str, datetime.timedelta] = {}
@ -66,6 +71,11 @@ def generate_report(samples: list[WorkSample]) -> Iterator[str]:
est_time: datetime.timedelta = DEFAULT_EST_TIME est_time: datetime.timedelta = DEFAULT_EST_TIME
est_time = min(sample.registered_at - prev_time, est_time) est_time = min(sample.registered_at - prev_time, est_time)
if len(SAMPLE_FILTER) == 0:
pass
elif not set(sample.labels).intersection(SAMPLE_FILTER):
continue
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
@ -77,18 +87,26 @@ def generate_report(samples: list[WorkSample]) -> Iterator[str]:
time_and_label.sort(reverse=True) time_and_label.sort(reverse=True)
# #
yield '-' * 56 yield '-' * 66
yield '\n' yield '\n'
for (total_time, label) in time_and_label: for (total_time, label_and_type) in time_and_label:
if label.startswith(HIDDEN_LABEL_PREFIX): if label_and_type.startswith(HIDDEN_LABEL_PREFIX):
continue continue
yield f' {label:40} {total_time / HOUR:-4.0f} hours\n'
label_type, label = label_and_type.split(':', 1)
if label_type not in LABEL_FILTER:
continue
label_type = '' # TODO
yield f' {label_type:8} {label:40} {total_time / HOUR:-4.0f} hours\n'
del label, total_time del label, total_time
yield '-' * 56 yield '-' * 66
yield '\n' yield '\n'
yield ' {label:40} {hours:-4.0f} hours\n'.format(label='TOTAL', hours = time_per_label[HIDDEN_LABEL_TOTAL] / HOUR) yield ' {label_type:8} {label:40} {hours:-4.0f} hours\n'.format(label_type='', label='TOTAL', hours = time_per_label[HIDDEN_LABEL_TOTAL] / HOUR)
def main(): def main():
logging.basicConfig() logging.basicConfig()