From 16659f971a81b9cd87f036774c3ee375ad2bf439 Mon Sep 17 00:00:00 2001 From: Mike Shriver Date: Thu, 10 Sep 2020 12:17:57 -0400 Subject: [PATCH] Update output filenames and paths --- config.py | 5 +++++ scripts/gh_metrics.py | 30 ++++++++++++---------------- utils/GQL_Queries/github_wrappers.py | 2 +- utils/file_io.py | 7 ++++--- utils/metrics_calculators.py | 6 ++++-- 5 files changed, 27 insertions(+), 23 deletions(-) diff --git a/config.py b/config.py index da41a35..7ddfa26 100644 --- a/config.py +++ b/config.py @@ -1,5 +1,10 @@ +from pathlib import Path + from dynaconf import Dynaconf settings = Dynaconf( envvar_prefix="METRICS", settings_files=["settings.yaml", ".secrets.yaml"], ) + +METRICS_DIR = Path() +METRICS_OUTPUT = METRICS_DIR.joinpath("metrics_output") diff --git a/scripts/gh_metrics.py b/scripts/gh_metrics.py index 55edfd8..6bc9c45 100644 --- a/scripts/gh_metrics.py +++ b/scripts/gh_metrics.py @@ -1,11 +1,12 @@ # import json # from collections import OrderedDict +from datetime import datetime from pathlib import Path -from time import time import click from tabulate import tabulate +from config import METRICS_OUTPUT from config import settings from utils import file_io from utils import metrics_calculators @@ -48,14 +49,14 @@ def gather(): @gather.command( - "single_pr_metrics", + "pr-metrics", help="Gather metrics about individual PRs for a GH repo (SatelliteQE/robottelo)", ) @org_name_option @repo_name_option @output_prefix_option @pr_count_option -def time_to_review(org, repo, file_output_prefix, pr_count): +def repo_pr_metrics(org, repo, file_output_prefix, pr_count): for repo_name in repo: pr_metrics, stat_metrics = metrics_calculators.single_pr_metrics( organization=org, repository=repo_name, pr_count=pr_count @@ -75,12 +76,12 @@ def time_to_review(org, repo, file_output_prefix, pr_count): tabulate(stat_metrics, headers="keys", tablefmt="github", floatfmt=".1f") ) - pr_metrics_filename = ( + pr_metrics_filename = METRICS_OUTPUT.joinpath( f"{Path(file_output_prefix).stem}-" f"{org}-" f"{repo_name}-" "pr_metrics-" - f"{int(time())}.html" + f"{datetime.now().isoformat(timespec='minutes')}.html" ) click.echo(f"Writing PR metrics as HTML to {pr_metrics_filename}") file_io.write_to_output( @@ -88,12 +89,12 @@ def time_to_review(org, repo, file_output_prefix, pr_count): tabulate(pr_metrics, headers="keys", tablefmt="html", floatfmt=".1f"), ) - stat_metrics_filename = ( + stat_metrics_filename = METRICS_OUTPUT.joinpath( f"{Path(file_output_prefix).stem}-" f"{org}-" f"{repo_name}-" "stat_metrics-" - f"{int(time())}.html" + f"{datetime.now().isoformat(timespec='minutes')}.html" ) click.echo(f"Writing statistics metrics as HTML to {stat_metrics_filename}") file_io.write_to_output( @@ -102,7 +103,7 @@ def time_to_review(org, repo, file_output_prefix, pr_count): ) -@gather.command("reviewer_actions") +@gather.command("reviewer-actions") @org_name_option @repo_name_option @output_prefix_option @@ -130,12 +131,12 @@ def reviewer_actions(org, repo, file_output_prefix, pr_count): click.echo("-" * len(header)) click.echo(tabulate(t2_metrics, headers="keys", tablefmt="github")) - tier1_metrics_filename = ( + tier1_metrics_filename = METRICS_OUTPUT.joinpath( f"{Path(file_output_prefix).stem}-" f"{org}-" f"{repo_name}-" "tier1_reviewers-" - f"{int(time())}.html" + f"{datetime.now().isoformat(timespec='minutes')}.html" ) click.echo(f"Writing PR metrics as HTML to {tier1_metrics_filename}") file_io.write_to_output( @@ -143,20 +144,15 @@ def reviewer_actions(org, repo, file_output_prefix, pr_count): tabulate(t1_metrics, headers="keys", tablefmt="html"), ) - tier2_metrics_filename = ( + tier2_metrics_filename = METRICS_OUTPUT.joinpath( f"{Path(file_output_prefix).stem}-" f"{org}-" f"{repo_name}-" "tier2_reviewers-" - f"{int(time())}.html" + f"{datetime.now().isoformat(timespec='minutes')}.html" ) click.echo(f"Writing PR metrics as HTML to {tier2_metrics_filename}") file_io.write_to_output( tier2_metrics_filename, tabulate(t2_metrics, headers="keys", tablefmt="html"), ) - - -# for debugging purposes -if __name__ == "__main__": - metrics = gather() diff --git a/utils/GQL_Queries/github_wrappers.py b/utils/GQL_Queries/github_wrappers.py index a16cf7e..2b4e349 100644 --- a/utils/GQL_Queries/github_wrappers.py +++ b/utils/GQL_Queries/github_wrappers.py @@ -66,7 +66,7 @@ def reviewer_teams(self): sys.exit(1) - def pull_requests(self, count=100, block_count=100): + def pull_requests(self, count=100, block_count=50): """dictionary of PRWrapper instances, keyed on PR numbers Args: count (Int): total number of PRs fetched diff --git a/utils/file_io.py b/utils/file_io.py index b109798..9751e0d 100644 --- a/utils/file_io.py +++ b/utils/file_io.py @@ -1,7 +1,8 @@ # module to handle file IO functions, to keep them out of the click command module -import json +from config import METRICS_OUTPUT def write_to_output(output_filename, content): - with open(output_filename, "w") as output_file: - json.dump(content, output_file, indent=2) + """output_filename should be a pathlib Path object""" + METRICS_OUTPUT.mkdir(parents=True, exist_ok=True) + output_filename.write_text(content) diff --git a/utils/metrics_calculators.py b/utils/metrics_calculators.py index a657bfb..6290f21 100644 --- a/utils/metrics_calculators.py +++ b/utils/metrics_calculators.py @@ -10,6 +10,8 @@ EMPTY = "---" +DATE_FMT = "%y-%m-%d" + HEADER_H_COM = "Hours to Comment" HEADER_H_T1 = "Hours to Tier1" HEADER_H_T2 = "Hours to Tier2" @@ -133,7 +135,7 @@ def reviewer_actions(organization, repository, pr_count=100): for week, actions in t1_by_week.items(): t1_metrics.append( { - "Week": f"{date.fromisocalendar(week[0], week[1], 1)} to " + "Week": f"{date.fromisocalendar(week[0], week[1], 1).strftime(DATE_FMT)} to " f"{date.fromisocalendar(week[0], week[1], 7)}", **actions, } @@ -142,7 +144,7 @@ def reviewer_actions(organization, repository, pr_count=100): for week, actions in t2_by_week.items(): t2_metrics.append( { - "Week": f"{date.fromisocalendar(week[0], week[1], 1)} to " + "Week": f"{date.fromisocalendar(week[0], week[1], 1).strftime(DATE_FMT)} to " f"{date.fromisocalendar(week[0], week[1], 7)}", **actions, }