From 2db419c78020495a4b8289ec20a8e07d70479b27 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Tue, 4 Jun 2024 00:30:55 +0200 Subject: [PATCH] Label filter --- git_time_tracker/__init__.py | 38 ++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/git_time_tracker/__init__.py b/git_time_tracker/__init__.py index b8098ee..bcd6d95 100644 --- a/git_time_tracker/__init__.py +++ b/git_time_tracker/__init__.py @@ -39,18 +39,20 @@ def determine_project_name(repo: git.Repo) -> str: return Path(repo.working_tree_dir).name def get_samples_from_project(repo: git.Repo) -> Iterator[WorkSample]: - labels = [HIDDEN_LABEL_TOTAL] - name = determine_project_name(repo) - assert name is not None - labels.append(name) + project_name = determine_project_name(repo) + assert project_name is not None # TODO: Branch on main or master or default repo.commit() 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.committed_date, tz=datetime.UTC), tuple(labels)) + del labels DEFAULT_EST_TIME=datetime.timedelta(hours=1) @@ -58,6 +60,9 @@ ZERO_DURATION = datetime.timedelta(seconds = 0) HOUR = datetime.timedelta(hours = 1) 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_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 = 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: time_per_label.setdefault(label,ZERO_DURATION) time_per_label[label] += est_time @@ -77,18 +87,26 @@ def generate_report(samples: list[WorkSample]) -> Iterator[str]: time_and_label.sort(reverse=True) # - yield '-' * 56 + yield '-' * 66 yield '\n' - for (total_time, label) in time_and_label: - if label.startswith(HIDDEN_LABEL_PREFIX): + for (total_time, label_and_type) in time_and_label: + if label_and_type.startswith(HIDDEN_LABEL_PREFIX): 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 - yield '-' * 56 + yield '-' * 66 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(): logging.basicConfig()