Skip to content

Commit

Permalink
templates: Refine and simplify emoji count overview
Browse files Browse the repository at this point in the history
  • Loading branch information
spbnick committed May 5, 2023
1 parent e2640da commit a22d75f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 85 deletions.
26 changes: 13 additions & 13 deletions kcidb/templates/build.j2
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
{# Build template macros #}
{% import "test.j2" as test_macros %}

{% macro summary(build) %}
{{- [build.architecture, build.config_name,
none if build.comment is none else ('"' + build.comment + '"')] |
reject("none") | join(" ") | default(build.id, true) -}}
{% endmacro %}

{% macro container_emoji_counts(output_dictionary, container) %}
{% set invalid = container.builds | selectattr("valid", "false") | list | length %}
{% set valid = container.builds | selectattr("valid", "true") | list | length %}
{% set unknown = container.builds | selectattr("valid", "none") | list | length %}
{% if invalid %}
{% set _ = output_dictionary.__setitem__("❌", invalid | string) %}
{% endif %}
{% if valid %}
{% set _ = output_dictionary.__setitem__("✅", valid | string) %}
{% endif %}
{% if unknown %}
{% set _ = output_dictionary.__setitem__("❓", unknown | string) %}
{% endif %}
{% macro container_emoji_counts(emoji_counts, container) %}
{% for emoji in test_macros.WAIVED_STATUS_EMOJIS.values() %}
{% set _ = emoji_counts.__setitem__(
emoji,
container.builds |
selectattr(
"valid",
"==",
{"❌": false, "✅": true, "❓": none}.get(emoji, {})
) | list | length
) %}
{% endfor %}
{% endmacro %}

{% macro container_summary(container, max_list_len) %}
Expand Down
8 changes: 4 additions & 4 deletions kcidb/templates/checkout_description.txt.j2
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ OVERVIEW

Checkout: {{ misc_macros.valid_badge(checkout.valid) }}
{% if checkout.builds %}
Builds: {{ overview_macros.emoji_overview(build_emoji_counts,
test_emoji_counts) }}
Builds: {{ overview_macros.emoji_counts(build_emoji_counts,
test_emoji_counts) }}
{% endif %}
{% if checkout.tests %}
Tests: {{ overview_macros.emoji_overview(test_emoji_counts,
build_emoji_counts) }}
Tests: {{ overview_macros.emoji_counts(test_emoji_counts,
build_emoji_counts) }}
{% endif %}

CHECKOUT
Expand Down
93 changes: 39 additions & 54 deletions kcidb/templates/overview.j2
Original file line number Diff line number Diff line change
@@ -1,60 +1,45 @@
{# Overview template macros #}

{% macro partial_emoji_overview(ns) %}

# get the overview string for "❌", "✅", "❓".
{%- for emoji in ["❌", "✅", "❓"] -%}
{%- if emoji in varargs[0] %}
{%- if emoji in varargs[1] %}
# if "emoji" exists in each of the emoji count dictionaries,
# generate a formatting string from the length of the longest
# count between them.

{% set _ = ns.elements.extend([emoji, varargs[0][emoji]]) -%}
{% if varargs[0][emoji] | length > varargs[1][emoji] | length %}
{% set index = 0 %}
{% else %}
{% set index = 1 %}
{% endif %}
{% set ns.format_string = ns.format_string + "%s " + "%" +
varargs[index][emoji] | length | string + "s " %}
{% else %}
# if "emoji" exists in the first but not latter of the
# emoji count dictionaries, generate formatting string
# from the length of first count.

{% set _ = ns.elements.extend([emoji, varargs[0][emoji]]) -%}
{% set ns.format_string = ns.format_string + "%s " + "%" +
varargs[0][emoji] | length | string + "s " %}
{% endif %}

{% else %}
{%- if emoji in varargs[1] %}
# if "emoji" exist in the latter but not first of the
# emoji count dictionaries, insert "➖" instead.

{% set _ = ns.elements.extend(["➖", ""]) %}
{% set ns.format_string = ns.format_string + "%s " + "%" +
varargs[1][emoji] | length | string + "s " %}
{% endif %}

{% endif -%}
{% import "test.j2" as test_macros %}

{%- macro emoji_counts() -%}
{%- set ns = namespace(
max_emojis={},
pieces=[],
first=test_macros.WAIVED_STATUS_EMOJIS | length,
last=-1
) -%}

{# Find maximum emoji counts accross all arguments #}
{%- for emoji in test_macros.WAIVED_STATUS_EMOJIS.values() -%}
{%- set _ = ns.max_emojis.__setitem__(
emoji, varargs | map(attribute=emoji) | max
) -%}
{%- endfor -%}
{% endmacro %}

{% macro emoji_overview() -%}
{% set ns = namespace(elements=[], format_string='') %}
{%- set _ = partial_emoji_overview(ns, varargs[0], varargs[1]) -%}

{# generate formatting string for other emoji(s) in TESTS. #}
{% for emoji in varargs[0] -%}
{% if emoji not in ["❌", "✅", '❓'] %}
{% set _ = ns.elements.extend([emoji, varargs[0][emoji]]) %}
{% set ns.format_string = ns.format_string + "%" +
emoji | length | string + "s %" +
varargs[0][emoji] | length | string + "s " %}
{% endif %}
{# Build a list of "<emoji> <count>" pieces for all emojis #}
{%- for emoji, count in varargs[0].items() -%}
{%- if ns.max_emojis[emoji] > 0 -%}
{# Remember the first non-zero emoji across all arguments #}
{%- set ns.first = [ ns.first, loop.index0 ] | min -%}
{# Add an "<emoji> <count>" piece #}
{%- set _ = ns.pieces.extend([
(
"%s %" +
(ns.max_emojis[emoji] | string | length | string) +
"s"
) |
format(
"➖" if count == 0 else emoji,
"" if count == 0 else (count | string)
)
]) -%}
{# Remember the last non-zero emoji in this argument #}
{%- if count > 0 -%}
{%- set ns.last = loop.index0 -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}

{{- ns.format_string | format(*ns.elements) -}}
{# Output the pieces of interest #}
{{- ns.pieces[ns.first:ns.last + 1] | join(" ") -}}
{%- endmacro -%}
8 changes: 4 additions & 4 deletions kcidb/templates/revision_description.txt.j2
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ OVERVIEW
Patches: {{ misc_macros.valid_badge(revision.checkouts_valid) }}
{% endif %}
{% if revision.builds %}
Builds: {{ overview_macros.emoji_overview(build_emoji_counts,
test_emoji_counts) }}
Builds: {{ overview_macros.emoji_counts(build_emoji_counts,
test_emoji_counts) }}
{% endif %}
{% if revision.tests %}
Tests: {{ overview_macros.emoji_overview(test_emoji_counts,
build_emoji_counts) }}
Tests: {{ overview_macros.emoji_counts(test_emoji_counts,
build_emoji_counts) }}
{% endif %}

REVISION
Expand Down
16 changes: 6 additions & 10 deletions kcidb/templates/test.j2
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,12 @@
reject("none") | join(" ") | default(test.id, true) -}}
{% endmacro %}

{% macro container_emoji_counts(output_dictionary, container) %}
{% for waived, status_nodes in container.tests_root.waived_status_nodes.items() %}
{% for status, nodes in status_nodes.items() %}
{% if nodes %}
{% set _ = output_dictionary.__setitem__(
waived_status_emoji(waived, status),
nodes | length | string
) %}
{% endif %}
{% endfor %}
{% macro container_emoji_counts(emoji_counts, container) %}
{% for (waived, status), emoji in WAIVED_STATUS_EMOJIS.items() %}
{% set _ = emoji_counts.__setitem__(
emoji,
container.tests_root.waived_status_nodes[waived][status] | length
) %}
{% endfor -%}
{% endmacro %}

Expand Down

0 comments on commit a22d75f

Please sign in to comment.