<?xml version="1.0" encoding="utf-8"?>
<odoo>
  <data>
    <template id="runbot.glances">
      <t t-call="runbot.layout">
        <t t-set="head">
          <t t-if="refresh">
            <meta http-equiv="refresh" t-att-content="refresh"/>
          </t>
        </t>
        <div class="container-fluid">
          <div class="row">
            <div class='col-md-12'>
              <div>
                <span t-attf-class="badge text-bg-{{pending_level}}">
                  Pending:
                  <t t-out="pending_count"/><span title="Assigned build (reserved host)" t-if="pending_assigned_count">(<t t-out="pending_assigned_count"/>)</span>
                </span>
              </div>
              <t t-set="project_id"/>
              <t t-set="nb_project" t-value="len(bundles.mapped('project_id'))"/>
              <t t-foreach="bundles.sorted(lambda b: (-b.project_id.id, b.version_id.number), reverse=True)" t-as="bundle">
                <h3 t-if="nb_project > 1 and project_id != bundle.project_id.id" t-out="bundle.project_id.name"/>
                <t t-set="project_id" t-value="bundle.project_id.id"/>
                <h4>
                  <t t-out="bundle.name"/>
                </h4>
                <t t-foreach="bundle.last_done_batch.slot_ids" t-as="slot">
                  <span t-attf-class="badge text-bg-{{slot.build_id._get_color_class()}}">
                    <t t-out="slot.trigger_id.name"/>
                  </span>
                </t>
              </t>
            </div>
          </div>
        </div>
      </t>
    </template>

    <template id="frontend_no_nav" inherit_id="runbot.layout" primary="True">
      <xpath expr="//header" position="replace">
      </xpath>
    </template>

    <template id="runbot.config_monitoring">
      <t t-call="runbot.frontend_no_nav">
        <t t-set="head">
          <t t-if="refresh">
            <meta http-equiv="refresh" t-att-content="refresh"/>
          </t>
        </t>
      </t>
    </template>

    <template id="runbot.monitoring">
      <t t-call="runbot.frontend_no_nav">
        <t t-set="head">
          <t t-if="refresh">
            <meta http-equiv="refresh" t-att-content="refresh"/>
          </t>
          <style>
            h4 {
            padding: 3px 0;
            border-bottom: 1px solid grey;
            }
            .r-mb02 { margin-bottom: 0.2em; }
          </style>
        </t>
        <div class="container-fluid">
          <div class="row">
            <div class="col-md-12">
              <div>
                <t t-call="runbot.slots_infos"/>
              </div>
              <t t-foreach="hosts_data.sorted(key=lambda h:h.name)" t-as="host">
                <div>
                  <span t-out="host.name.split('.')[0]"/>
                  <t t-if="host.nb_testing == 0">
                    <t t-set="klass">success</t>
                  </t>
                  <t t-if="host.nb_testing > 0">
                    <t t-set="klass">info</t>
                  </t>
                  <t t-if="host.nb_testing == host.nb_worker">
                    <t t-set="klass">warning</t>
                  </t>
                  <t t-if="host.nb_testing > host.nb_worker">
                    <t t-set="klass">danger</t>
                  </t>
                  <a t-attf-class="badge text-bg-{{klass}}" t-attf-href="/runbot/load_info?host={{host.name}}">
                    <span t-out="host.nb_testing"/>
                    /
                    <span t-out="host.nb_worker"/>
                  </a>
                  <t t-out="host.nb_running"/>
                  <t t-set="succes_time" t-value="int(datetime.datetime.now().timestamp() - host.last_success.timestamp())"/>
                  <t t-set="start_time" t-value="int(datetime.datetime.now().timestamp() - host.last_start_loop.timestamp())"/>
                  <t t-set="end_time" t-value="int(datetime.datetime.now().timestamp() - host.last_end_loop.timestamp())"/>

                  <t t-set="klass">success</t>
                  <t t-if="succes_time > 30">
                    <t t-set="klass">info</t>
                  </t>
                  <t t-if="succes_time > 180">
                    <t t-set="klass">danger</t>
                  </t>

                  <span t-attf-class="badge text-bg-{{klass}}">
                    <span t-out="succes_time"/>
                  </span>

                  <t t-set="klass">success</t>
                  <t t-if="start_time > 60*10">
                    <t t-set="klass">info</t>
                  </t>
                  <t t-if="start_time > 60*15">
                    <t t-set="klass">danger</t>
                  </t>

                  <span t-attf-class="badge text-bg-{{klass}}">
                    <span t-out="start_time"/>
                  </span>

                  <t t-set="klass">success</t>
                  <t t-if="end_time > 60*10">
                    <t t-set="klass">info</t>
                  </t>
                  <t t-if="end_time > 60*15">
                    <t t-set="klass">danger</t>
                  </t>

                  <span t-attf-class="badge text-bg-{{klass}}">
                    <span t-out="end_time"/>
                  </span>

                  <t t-set="cron_time" t-value="end_time-start_time"/>
                  <t t-set="klass">success</t>
                  <t t-if="abs(cron_time) > 10">
                    <t t-set="klass">info</t>
                  </t>
                  <t t-if="abs(cron_time) > 60">
                    <t t-set="klass">danger</t>
                  </t>
                  <span t-attf-class="badge text-bg-{{klass}}">
                    <span t-out="cron_time"/>
                  </span>

                </div>
              </t>

              <table>
                <tr t-foreach="bundles.sorted(lambda b: b.version_id.number, reverse=True)" t-as="bundle">
                  <td>
                    <t t-out="bundle.version_id.number"/>
                  </td>
                  <td>
                    <t t-set='batch' t-value="bundle.with_context({'category_id': category.id}).last_done_batch"/>
                    <table>
                      <t t-foreach="batch.slot_ids" t-as='slot'>
                        <tr>
                          <td>
                            <t t-out="slot.trigger_id.name[:4]"/>
                          </td>
                          <t t-set="build" t-value="slot.build_id"/>
                          <td>
                            <span t-attf-class="badge text-bg-{{slot.build_id._get_color_class()}}">
                              <i t-attf-class="fa fa-{{category.icon}}"/>
                            </span>
                          </td>
                          <td t-foreach="build.children_ids" t-as="child">
                            <span t-attf-class="badge text-bg-{{slot.build_id._get_color_class()}}">
                              <t t-out="child.params_id.config_id.name[:4]"/>
                            </span>
                          </td>
                        </tr>
                      </t>
                    </table>
                  </td>
                </tr>
              </table>
            </div>
          </div>
        </div>
      </t>
    </template>

    <template id="runbot.default_dashboard_tile_view">
      <div class="col-md-3 col-lg-2 p-2">
        <div class="card">
          <div class="card-header limited-height-toggle" t-attf-onclick="$('#tile_{{tile.id}}').toggleClass('limited-height')">
            <t t-out="tile.display_name"/> <t t-if="tile.build_ids"> (<t t-out="len(tile.build_ids)"/>)</t>
          </div>
          <div class="card-body limited-height" t-attf-id="tile_{{tile.id}}">
            <p t-if="not tile.build_ids" class="text-success my-0">No build found 👍</p>
            <t t-foreach="tile.sticky_bundle_ids.sorted(lambda b: b.version_id.number, reverse=True)" t-as="bundle">
              <t t-set="failed_builds" t-value="tile.build_ids.filtered(lambda b: b.top_parent.slot_ids.batch_id.bundle_id == bundle)"/>
              <h4 class="card-title" t-if="failed_builds" t-out="bundle.name"/>
              <p t-foreach="failed_builds" t-as="build" class="my-0">
                  <a class="text-danger" t-attf-href="/runbot/build/{{build.id}}" target="_blank">
                    <t t-out="build.description or build.id"/>
                  </a>
              </p>
            </t>
            <hr/>
          </div>
        </div>
      </div>
    </template>

    <template id="runbot.dashboard">
      <div class="row">
        <t t-foreach="dashboard.dashboard_tile_ids" t-as="tile">
          <t t-if="tile.build_ids or not hide_empty" t-call="{{ tile.custom_template_id.id }}"/>
        </t>
      </div>
    </template>

    <template id="runbot.dashboard_page">
      <t t-call="runbot.frontend_no_nav">
        <t t-set="head">
          <t t-if="refresh">
            <meta http-equiv="refresh" t-att-content="refresh"/>
          </t>
        </t>
        <t t-call="runbot.dashboard"/>
      </t>
    </template>

    <template id="runbot.load_info">
      <t t-call='runbot.layout'>
        <div class="container-fluid frontend">
          <div class="row">
            <div class="col-md-12">
                <t t-call="runbot.slots_infos"/>
            </div>
          </div>

          <div t-foreach="build_by_bundle" t-as="bundle_builds" class="row bundle_row">
            <t t-set="bundle" t-value="bundle_builds[0]"/>
            <t t-set="builds" t-value="bundle_builds[1]"/>

            <div class="col-md-3 col-lg-2 cell">
              <div class="one_line">
                <i t-if="bundle.sticky" class="fa fa-star" style="color: #f0ad4e" />
                <a t-attf-href="/runbot/bundle/#{bundle.id}" title="View Bundle">
                  <b t-out="bundle.name"/>
                </a>
              </div>
              <div class="badge text-bg-info" t-out="len(builds)"/>
            </div>
            <div class="col-md-9 col-lg-10">
              <div class="table-responsive">
                <table class="table table-condensed">
                  <t t-foreach="builds" t-as="build">
                    <t t-set="rowclass">
                      <t t-call="runbot.build_class">
                        <t t-set="build" t-value="build"/>
                      </t>
                    </t>
                    <tr t-attf-class="bg-{{rowclass.strip()}}-subtle {{'line-through' if build.orphan_result else ''}}">
                      <td> Batch: <a t-attf-href="/runbot/batch/{{build.params_id.create_batch_id.id}}"><t t-out="build.params_id.create_batch_id.id"/></a></td>
                      <td><a t-attf-href="/runbot/build/{{build.id}}"><t t-out="build.parent_path"/></a></td>
                      <td t-out="build.local_state"/>
                      <td t-out="build.host"/>
                      <td t-out="build.config_id.name"/>
                      <td>
                        <t t-call="runbot.build_button">
                          <t t-set="bu" t-value="build"/>
                          <t t-set="klass" t-value="'btn-group-ssm'"/>
                        </t>
                      </td>
                    </tr>
                  </t>
                </table>
              </div>
            </div>

          </div>
        </div>
      </t>
    </template>
  </data>
</odoo>
