From 1dda32980f1be4e79317ca72836a328de4d6fbb2 Mon Sep 17 00:00:00 2001 From: Joachim Jablon Date: Thu, 27 Jun 2024 21:27:32 +0200 Subject: [PATCH] Abbreviate large numbers --- coverage_comment/template.py | 11 +++++++++++ coverage_comment/template_files/comment.md.j2 | 10 +++++----- tests/unit/test_template.py | 17 +++++++++++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/coverage_comment/template.py b/coverage_comment/template.py index 7f682aab..ed680cf5 100644 --- a/coverage_comment/template.py +++ b/coverage_comment/template.py @@ -74,6 +74,16 @@ def delta(val: int) -> str: return f"({sign(val)}{val})" +def compact(val: int) -> str: + if val < 1_000: + return str(val) + if val < 10_000: + return f"{val / 1_000:.1f}k" + if val < 1_000_000: + return f"{val / 1_000:.0f}k" + return f"{val / 1_000_000:.0f}M" + + def remove_exponent(val: decimal.Decimal) -> decimal.Decimal: # From https://docs.python.org/3/library/decimal.html#decimal-faq return ( @@ -136,6 +146,7 @@ def get_comment_markdown( env.filters["get_evolution_color"] = badge.get_evolution_badge_color env.filters["generate_badge"] = badge.get_static_badge_url env.filters["pluralize"] = pluralize + env.filters["compact"] = compact env.filters["file_url"] = functools.partial( get_file_url, repo_name=repo_name, pr_number=pr_number ) diff --git a/coverage_comment/template_files/comment.md.j2 b/coverage_comment/template_files/comment.md.j2 index 6fb2fcfc..53816f92 100644 --- a/coverage_comment/template_files/comment.md.j2 +++ b/coverage_comment/template_files/comment.md.j2 @@ -45,7 +45,7 @@ {% set color = "007ec6" -%} {% set message = statements_count %} {% endif -%} - + {%- endmacro -%} @@ -65,7 +65,7 @@ {%- set message = missing_lines_count -%} {%- endif -%} {%- set color = message | get_evolution_color(up_is_good=false) -%} - + {%- endmacro -%} @@ -80,10 +80,10 @@ {%- set text = "This PR doesn't change the coverage rate in " ~ path ~ ", which is " ~ percent_covered | pct ~ " (" ~ covered_statements_count ~ "/" ~ statements_count ~ ")." -%} {%- endif -%} {%- set color = coverage_diff | get_evolution_color() -%} -{%- set message = "(" ~ previous_covered_statements_count ~ "/" ~ previous_statements_count ~ " > " ~ covered_statements_count ~ "/" ~ statements_count ~ ")" -%} +{%- set message = "(" ~ previous_covered_statements_count | compact ~ "/" ~ previous_statements_count | compact ~ " > " ~ covered_statements_count | compact ~ "/" ~ statements_count | compact ~ ")" -%} {%- else -%} {%- set text = "The coverage rate of " ~ path ~ " is " ~ percent_covered | pct ~ " (" ~ covered_statements_count ~ "/" ~ statements_count ~ "). The file did not seem to exist on the base branch." -%} -{%- set message = "(" ~ covered_statements_count ~ "/" ~ statements_count ~ ")" -%} +{%- set message = "(" ~ covered_statements_count | compact ~ "/" ~ statements_count | compact ~ ")" -%} {%- set color = percent_covered | x100 | get_badge_color -%} {%- endif -%} @@ -94,7 +94,7 @@ {% if added_statements_count -%} {% set text = "In this PR, " ~ (added_statements_count) ~ " new statements are added to " ~ path ~ ", " ~ covered_statements_count ~ " of which are covered (" ~ (percent_covered | pct) ~ ")." -%} {% set label = (percent_covered | pct(precision=0)) -%} -{% set message = "(" ~ covered_statements_count ~ "/" ~ added_statements_count ~ ")" -%} +{% set message = "(" ~ covered_statements_count | compact ~ "/" ~ added_statements_count | compact ~ ")" -%} {%- set color = (percent_covered | x100 | get_badge_color()) -%} {% else -%} {% set text = "This PR does not seem to add statements to " ~ path ~ "." -%} diff --git a/tests/unit/test_template.py b/tests/unit/test_template.py index 1e60c18c..62820423 100644 --- a/tests/unit/test_template.py +++ b/tests/unit/test_template.py @@ -836,3 +836,20 @@ def test_get_log_message(): subproject_id="foo", ) assert result.startswith("Coverage info for foo:") + + +@pytest.mark.parametrize( + "value, expected", + [ + (0, "0"), + (1, "1"), + (999, "999"), + (1_042, "1.0k"), + (9_900, "9.9k"), + (12_345, "12k"), + (999_999, "1000k"), + (1_234_567, "1M"), + ], +) +def test_compact(value, expected): + assert template.compact(value) == expected