<!DOCTYPE html>
<html lang="en">
<head>
{# Scripts, for visualization#}
<!--START-OF-SCRIPTS-->
<script src=" https://cdn.jsdelivr.net/npm/echarts@5.5.0/dist/echarts.min.js "></script>

{# Render measurement result charts #}
{% for test in test_data %}
  {% if test.status == 'SUCCESS' %}
    {% for measurement in test.measurements %}
      {% set chart_elem_start_time_id = test.name + '_' + measurement.name + '_chart_start_time' %}
      {% set chart_elem_commit_count_id = test.name + '_' + measurement.name + '_chart_commit_count' %}
      {% include 'measurement_chart.html' %}
    {% endfor %}
  {% endif %}
{% endfor %}

<!--END-OF-SCRIPTS-->

{# Styles #}
<style>
:root {
  --text: #000;
  --bg: #fff;
  --h2heading: #707070;
  --link: #0000EE;
  --trtopborder: #9ca3af;
  --trborder: #e5e7eb;
  --chartborder: #f0f0f0;
  }
.meta-table {
  font-size: 14px;
  text-align: left;
  border-collapse: collapse;
}
.summary {
  font-size: 14px;
  text-align: left;
  border-collapse: collapse;
}
.measurement {
  padding: 8px 0px 8px 8px;
  border: 2px solid var(--chartborder);
  margin: 1.5rem 0;
}
.details {
  margin: 0;
  font-size: 12px;
  text-align: left;
  border-collapse: collapse;
}
.details th {
  padding-right: 8px;
}
.details.plain th {
  font-weight: normal;
}
.preformatted {
  font-family: monospace;
  white-space: pre-wrap;
  background-color: #f0f0f0;
  margin-left: 10px;
}
.card-container {
  border-bottom-width: 1px;
  padding: 1.25rem 3rem;
  box-shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
  border-radius: 0.25rem;
}
body {
  font-family: 'Helvetica', sans-serif;
  margin: 3rem 8rem;
  background-color: var(--bg);
  color: var(--text);
}
h1 {
  text-align: center;
}
h2 {
  font-size: 1.5rem;
  margin-bottom: 0px;
  color: var(--h2heading);
  padding-top: 1.5rem;
}
h3 {
  font-size: 1.3rem;
  margin: 0px;
  color: var(--h2heading);
  padding: 1.5rem 0;
}
h4 {
  font-size: 14px;
  font-weight: lighter;
  line-height: 1.2rem;
  margin: auto;
  padding-top: 1rem;
}
table {
  margin-top: 1.5rem;
  line-height: 2rem;
}
tr {
  border-bottom: 1px solid var(--trborder);
}
tr:first-child {
  border-bottom: 1px solid var(--trtopborder);
}
tr:last-child {
  border-bottom: none;
}
a {
  text-decoration: none;
  font-weight: bold;
  color: var(--link);
}
a:hover {
  color: #8080ff;
}
button {
  background-color: #F3F4F6;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 10px 12px;
  transition: 0.3s;
  border-radius: 8px;
  color: #3A4353;
}
button:hover {
  background-color: #d6d9e0;
}
.tab button.active {
  background-color: #d6d9e0;
}
@media (prefers-color-scheme: dark) {
  :root {
    --text: #e9e8fa;
    --bg: #0F0C28;
    --h2heading: #B8B7CB;
    --link: #87cefa;
    --trtopborder: #394150;
    --trborder: #212936;
    --chartborder: #b1b0bf;
  }
  button {
    background-color: #28303E;
    color: #fff;
  }
  button:hover {
    background-color: #545a69;
  }
  .tab button.active {
    background-color: #545a69;
  }
}
</style>

<title>{{ title }}</title>
</head>

{% macro poky_link(commit) -%}
  <a href="http://git.yoctoproject.org/cgit/cgit.cgi/poky/log/?id={{ commit }}">{{ commit[0:11] }}</a>
{%- endmacro %}

<body><div>
  <h1 style="text-align: center;">Performance Test Report</h1>
  {# Test metadata #}
  <h2>General</h2>
  <h4>The table provides an overview of the comparison between two selected commits from the same branch.</h4>
  <table class="meta-table" style="width: 100%">
    <tr>
      <th></th>
      <th>Current commit</th>
      <th>Comparing with</th>
    </tr>
    {% for key, item in metadata.items() %}
    <tr>
      <th>{{ item.title }}</th>
      {%if key == 'commit' %}
        <td>{{ poky_link(item.value) }}</td>
        <td>{{ poky_link(item.value_old) }}</td>
      {% else %}
        <td>{{ item.value }}</td>
        <td>{{ item.value_old }}</td>
      {% endif %}
    </tr>
    {% endfor %}
  </table>

  {# Test result summary #}
  <h2>Test result summary</h2>
  <h4>The test summary presents a thorough breakdown of each test conducted on the branch, including details such as build time and disk space consumption. Additionally, it gives insights into the average time taken for test execution, along with absolute and relative values for a better understanding.</h4>
  <table class="summary" style="width: 100%">
    <tr>
      <th>Test name</th>
      <th>Measurement description</th>
      <th>Mean value</th>
      <th>Absolute difference</th>
      <th>Relative difference</th>
    </tr>
    {% for test in test_data %}
      {% if test.status == 'SUCCESS' %}
        {% for measurement in test.measurements %}
          <tr {{ row_style }}>
            {% if loop.index == 1 %}
              <td><a href=#{{test.name}}>{{ test.name }}: {{ test.description }}</a></td>
            {% else %}
              {# add empty cell in place of the test name#}
              <td></td>
            {% endif %}
            {% if measurement.absdiff > 0 %}
              {% set result_style = "color: red" %}
            {% elif measurement.absdiff == measurement.absdiff %}
              {% set result_style = "color: green" %}
            {% else %}
              {% set result_style = "color: orange" %}
            {%endif %}
            {% if measurement.reldiff|abs > 2 %}
              {% set result_style = result_style + "; font-weight: bold" %}
            {% endif %}
            <td>{{ measurement.description }}</td>
            <td style="font-weight: bold">{{ measurement.value.mean }}</td>
            <td style="{{ result_style }}">{{ measurement.absdiff_str }}</td>
            <td style="{{ result_style }}">{{ measurement.reldiff_str }}</td>
          </tr>
        {% endfor %}
      {% else %}
        <td style="font-weight: bold; color: red;">{{test.status }}</td>
        <td></td> <td></td> <td></td> <td></td>
      {% endif %}
    {% endfor %}
  </table>

  {# Detailed test results #}
  <h2>Test details</h2>
  <h4>The following section provides details of each test, accompanied by charts representing build time and disk usage over time or by commit number.</h4>
  {% for test in test_data %}
  <h3 style="color: #000;" id={{test.name}}>{{ test.name }}: {{ test.description }}</h3>
    {% if test.status == 'SUCCESS' %}
    <div class="card-container">
      {% for measurement in test.measurements %}
        <div class="measurement">
          <h3>{{ measurement.description }}</h3>
          <div style="font-weight:bold;">
            <span style="font-size: 23px;">{{ measurement.value.mean }}</span>
            <span style="font-size: 20px; margin-left: 12px">
            {% if measurement.absdiff > 0 %}
            <span style="color: red">
            {% elif measurement.absdiff == measurement.absdiff %}
            <span style="color: green">
            {% else %}
            <span style="color: orange">
            {% endif %}
            {{ measurement.absdiff_str }} ({{measurement.reldiff_str}})
            </span></span>
          </div>
          {# Table for trendchart and the statistics #}
          <table style="width: 100%">
            <tr>
              <td style="width: 75%">
                {# Linechart #}
                <div class="tab {{ test.name }}_{{ measurement.name }}_tablinks">
                  <button class="tablinks active" onclick="openChart(event, '{{ test.name }}_{{ measurement.name }}_start_time', '{{ test.name }}_{{ measurement.name }}')">Chart with start time</button>
                  <button class="tablinks" onclick="openChart(event, '{{ test.name }}_{{ measurement.name }}_commit_count', '{{ test.name }}_{{ measurement.name }}')">Chart with commit count</button>
                </div>
                <div class="{{ test.name }}_{{ measurement.name }}_tabcontent">
                  <div id="{{ test.name }}_{{ measurement.name }}_start_time" class="tabcontent" style="display: block;">
                    <div id="{{ test.name }}_{{ measurement.name }}_chart_start_time"></div>
                  </div>
                  <div id="{{ test.name }}_{{ measurement.name }}_commit_count" class="tabcontent" style="display: none;">
                    <div id="{{ test.name }}_{{ measurement.name }}_chart_commit_count"></div>
                  </div>
                </div>
              </td>
              <td>
                {# Measurement statistics #}
                <table class="details plain">
                  <tr>
                    <th>Test runs</th><td>{{ measurement.value.sample_cnt }}</td>
                  </tr><tr>
                    <th>-/+</th><td>-{{ measurement.value.minus }} / +{{ measurement.value.plus }}</td>
                  </tr><tr>
                    <th>Min</th><td>{{ measurement.value.min }}</td>
                  </tr><tr>
                    <th>Max</th><td>{{ measurement.value.max }}</td>
                  </tr><tr>
                    <th>Stdev</th><td>{{ measurement.value.stdev }}</td>
                  </tr><tr>
                    <th><div id="{{ test.name }}_{{ measurement.name }}_chart_png"></div></th>
                    <td></td>
                  </tr>
                </table>
              </td>
            </tr>
          </table>

          {# Task and recipe summary from buildstats #}
          {% if 'buildstats' in measurement %}
            Task resource usage
            <table class="details" style="width:100%">
              <tr>
                <th>Number of tasks</th>
                <th>Top consumers of cputime</th>
              </tr>
              <tr>
                <td style="vertical-align: top">{{ measurement.buildstats.tasks.count }} ({{ measurement.buildstats.tasks.change }})</td>
                {# Table of most resource-hungry tasks #}
                <td>
                  <table class="details plain">
                    {% for diff in measurement.buildstats.top_consumer|reverse %}
                    <tr>
                      <th>{{ diff.pkg }}.{{ diff.task }}</th>
                      <td>{{ '%0.0f' % diff.value2 }} s</td>
                    </tr>
                    {% endfor %}
                  </table>
                </td>
              </tr>
              <tr>
                <th>Biggest increase in cputime</th>
                <th>Biggest decrease in cputime</th>
              </tr>
              <tr>
                {# Table biggest increase in resource usage #}
                <td>
                  <table class="details plain">
                    {% for diff in measurement.buildstats.top_increase|reverse %}
                    <tr>
                      <th>{{ diff.pkg }}.{{ diff.task }}</th>
                      <td>{{ '%+0.0f' % diff.absdiff }} s</td>
                    </tr>
                    {% endfor %}
                  </table>
                </td>
                {# Table biggest decrease in resource usage #}
                <td>
                  <table class="details plain">
                    {% for diff in measurement.buildstats.top_decrease %}
                    <tr>
                      <th>{{ diff.pkg }}.{{ diff.task }}</th>
                      <td>{{ '%+0.0f' % diff.absdiff }} s</td>
                    </tr>
                    {% endfor %}
                  </table>
                </td>
              </tr>
            </table>

            {# Recipe version differences #}
            {% if measurement.buildstats.ver_diff %}
              <div style="margin-top: 16px">Recipe version changes</div>
              <table class="details">
                {% for head, recipes in measurement.buildstats.ver_diff.items() %}
                  <tr>
                    <th colspan="2">{{ head }}</th>
                  </tr>
                  {% for name, info in recipes|sort %}
                    <tr>
                      <td>{{ name }}</td>
                      <td>{{ info }}</td>
                    </tr>
                  {% endfor %}
                {% endfor %}
              </table>
            {% else %}
              <div style="margin-top: 16px">No recipe version changes detected</div>
            {% endif %}
          {% endif %}
        </div>
        {% endfor %}
      </div>
    {# Unsuccessful test #}
    {% else %}
      <span style="font-size: 150%; font-weight: bold; color: red;">{{ test.status }}
      {% if test.err_type %}<span style="font-size: 75%; font-weight: normal">({{ test.err_type }})</span>{% endif %}
      </span>
      <div class="preformatted">{{ test.message }}</div>
    {% endif %}
  {% endfor %}
</div>

<script>
function openChart(event, chartType, chartName) {
  let i, tabcontents, tablinks
  tabcontents = document.querySelectorAll(`.${chartName}_tabcontent > .tabcontent`);
  tabcontents.forEach((tabcontent) => {
    tabcontent.style.display = "none";
  });

  tablinks = document.querySelectorAll(`.${chartName}_tablinks > .tablinks`);
  tablinks.forEach((tabLink) => {
    tabLink.classList.remove('active');
  });

  const targetTab = document.getElementById(chartType)
  targetTab.style.display = "block";

  // Call resize on the ECharts instance to redraw the chart
  const chartContainer = targetTab.querySelector('div')
  echarts.init(chartContainer).resize();

  event.currentTarget.classList.add('active');
}
</script>

</body>
</html>
