<?xml version="1.0" encoding="utf-8"?>
<odoo>
  <data>
    <template id="runbot.build_stats">
      <t t-call='runbot.layout'>
          <t t-set="bundles" t-value="build.slot_ids.mapped('batch_id.bundle_id')"/>
          <div>
            <div class="row g-0">
              <div class="col-md-4">
                <div class="bg-success-subtle">
                  <b>Build: </b><a t-attf-href="/runbot/build/{{build.id}}"><t t-out="build.id"/></a><br/>
                  <t t-if="build.description">
                    <b>Description:</b>
                    <t t-out="build.md_description"/>
                    <br/>
                  </t>
                  <b>Date: </b><t t-out="build.create_date" /><br/>
                  <b>Config: </b><t t-out="build.params_id.config_id.name" /><br/>
                  <b>Bundle(s): </b>
                  <t t-foreach="bundles" t-as="bundle">
                    <a t-attf-href="/runbot/bundle/{{bundle.id}}"><t t-out="bundle.name" /></a>
                  </t><br/>
                  <t t-foreach="build.params_id.sudo().commit_link_ids" t-as="build_commit">
                    <b>Commit:</b>
                    <a t-attf-href="/runbot/commit/{{build_commit.commit_id.id}}">
                      <t t-out="build_commit.commit_id.dname"/>
                    </a>
                    <a t-att-href="'https://%s/commit/%s' % (build_commit.branch_id.remote_id.base_url, build_commit.commit_id.name)" class="btn btn-sm text-start" title="View Commit on Github"><i class="fa fa-github"/></a>
                    <t t-if="build_commit.match_type in ('default', 'pr_target', 'prefix') ">
                      from base branch
                      <br/>
                    </t>
                    <div t-else="" class="ms-3">
                      <b>Subject:</b>
                      <t t-out="build_commit.commit_id.subject"/>
                      <br/>
                      <b>Author:</b>
                      <t t-out="build_commit.commit_id.author"/>
                      <br/>
                      <b>Committer:</b>
                      <t t-out="build_commit.commit_id.committer"/>
                      <br/>
                    </div>
                  </t>
                  <b>Version:</b>
                  <t t-out="build.params_id.version_id.name"/>
                  <br/>
                </div>
              </div>
              <div t-foreach="sorted(build_stats.keys())" t-as="category" class="col-md-4">
                <h3><t t-out="category.title().replace('_', ' ')"/></h3>
                <table class="table table-condensed table-responsive table-stripped">
                  <tr t-foreach="build_stats[category].keys()" t-as="module">
                    <td><t t-out="module"/></td>
                    <td><t t-out="build_stats[category][module]"/></td>
                  </tr>
                </table>
              </div>
              <div t-if="not build_stats" class="col-md-12 alert alert-warning">No stats records found for this build</div>
            </div>
          </div>
      </t>
    </template>

    <template id="runbot.modules_stats">
      <t t-call='runbot.layout'>
        <input type="hidden" id="bundle_id" t-att-value="bundle.id"/>
        <input type="hidden" id="trigger_id" t-att-value="trigger.id"/>
        <div class="container-fluid">
          <nav class="navbar navbar-light">
            <div class="container">
              <b>Bundle:</b><t t-out="bundle.name"/>
              <b>Trigger:</b><t t-out="trigger.name"/>
              <b>Stat Category:</b>
              <select id="key_category_selector" class="form-select" aria-label="Stat Category">
                <option t-foreach="stats_categories" t-as="category" t-attf-value="{{category}}"><t t-out="category.replace('_',' ').title()"/></option>
              </select>
              <b>Nb of builds:</b>
              <select id="limit_selector" class="form-select" aria-label="Number Of Builds">
                <option value="10">10</option>
                <option value="25">25</option>
                <option value="50">50</option>
                <option value="100">100</option>
                <option value="250">250</option>
              </select>
              <button id="backward_button" class="btn btn-default" title="Previous Builds" aria-label="Previous Builds">
                <i t-attf-class="fa fa-backward"/>
              </button>
              <button id="forward_button" class="btn btn-default" title="Previous Builds" aria-label="Previous Builds">
                <i t-attf-class="fa fa-forward"/>
              </button>
              <button id="fast_forward_button" class="btn btn-default" title="Previous Builds" aria-label="Previous Builds">
                <i t-attf-class="fa fa-fast-forward"/>
              </button>
              <p>Tips: click a bullet to see corresponding build stats, shift+click to center the graph on this build</p>
              <i id="chart_spinner" class="fa fa-2x fa-circle-o-notch fa-spin"/>
            </div>
          </nav>
          <div class="row">
            <div class="col-xs-9 col-md-10"><canvas id="canvas"></canvas></div>
            <div class="col-xs-3 col-md-2">
                <b>Mode:</b>
                <select id="mode_selector" class="form-select" aria-label="Display mode">
                  <option title="Real Values ordered by value" selected="selected" value="normal">Value</option>
                  <option title="Real Values ordered by name" selected="selected" value="alpha">Alphabetical</option>
                  <option title="Delta With Reference Build Values" value="difference">Difference</option>
                  <option title="Bigger # of datapoint varying from previous one" value="change_count">Noisy</option>
                </select><br/>

                <b>Display:</b>
                <select id="nb_dataset_selector" class="form-select" aria-label="Number Of Builds">
                  <option value="-1">Custom</option>
                  <option value="0">0</option>
                  <option value="10">Top 10</option>
                  <option value="20">Top 20</option>
                  <option value="50">Top 50</option>
                </select><br/>

                <b>Display aggregate:</b>
                <select id="display_aggregate_selector" class="form-select" aria-label="Display sum">
                  <option selected="selected" value="none">No</option>
                  <option value="sum">Sum</option>
                  <option value="average">Average</option>
                </select><br/>
                <div id="js-legend" class="chart-legend">
                
                </div>
            
            </div>
          </div>
        </div>
      </t>
      <script type="text/javascript" src="/web/static/lib/Chart/Chart.js"></script>
      <script type="text/javascript" src="/runbot/static/src/js/stats.js"></script>
    </template>
  </data>
</odoo>
