{% extends "aggregator/base.html" %}

{% load pagination_tags %}
{% load humanize %}

{% block content %}

{% include "aggregator/includes/search_title.html" %}

<p>We track health of {{ services_count|intcomma }} remote map services, containing a total of {{ layers_count|intcomma }} layers.</p>

  {% if services %}

  <div class="row">
    <div class="col-md-6">

        Catalogs:
        <a href="{% url 'index' %}">all ({{ services_count }})</a>
        {% for catalog in catalogs %}
            <a href="{{ catalog.get_absolute_url }}">{{ catalog.name }} ({{ catalog.service_set.count }})</a>
        {% endfor %}

        <br>
        Filter by:
        {% for type in types_list %}
            &nbsp;<a href="?filter_by={{ type.0 }}">{{ type.1 }} ({{ type.2 }})</a>
        {% endfor %}


    </div>
    <div class="col-md-6">
      <form id='search' action="." method="get">
        <input type="text" name="q">
        <input type="submit" value="Search">
      </form>
    </div>
  </div>
  <div class="row">
    <div class="col-md-12">
      <table class="table table-striped">
        <thead>
          <tr>
            <th>
              <a href="?order_by=title"><i class="fa fa-fw fa-sort-asc"></i></a>
              Title
              <a href="?order_by=-title"><i class="fa fa-fw fa-sort-desc"></i></a>
            </th>
            <th>
              <a href="?order_by=layers_count"><i class="fa fa-fw fa-sort-asc"></i></a>
              Layers
              <a href="?order_by=-layers_count"><i class="fa fa-fw fa-sort-desc"></i></a>
            </th>
            <th>
              <a href="?order_by=last_updated"><i class="fa fa-fw fa-sort-asc"></i></a>
              Last Check
              <a href="?order_by=-last_updated"><i class="fa fa-fw fa-sort-desc"></i></a>
            </th>
            <th>
              <a href="?order_by=total_checks"><i class="fa fa-fw fa-sort-asc"></i></a>
              Total Checks
              <a href="?order_by=-total_checks"><i class="fa fa-fw fa-sort-desc"></i></a>
            </th>
            <th>Status</th>
            <th>Response Times</th>
            <th>Reliability</th>
          </tr>
        </thead>
        <tbody>
          {% autopaginate services 10 %}
          {% for service in services %}
            <tr>
              <td>
                <a href="{{ service.catalog.get_absolute_url }}">{{ service.catalog.name }}</a>:<strong>{{ service.get_domain }}</strong>
                <br /><a href="{{ service.get_absolute_url }}">{{ service.title|truncatechars:40 }}</a>
                <br /><a href="{{ service.url }}">{{ service.url|truncatechars:40 }}</a>
                <br />{{ service.get_type_display }}
{#                  <a href="{{ service.catalog.get_absolute_url }}"><span class="label label-default">{{ service.catalog.name }}</span></a>#}
              </td>
              <td>{{ service.layer_set.all.count }}</td>
              <td>
                  {{ service.last_updated }}
              </td>
              <td>{{ service.checks_count }}</td>
              <td>
                {% if service.last_status %}
                  <button type="button" class="btn btn-success btn-circle btn nohover"><i class="fa fa-check"></i></button>
                {% else %}
                  <button type="button" class="btn btn-danger btn-circle btn nohover"><i class="fa fa-check"></i></button>
                {% endif %}
              </td>
              <td>
                <ul>
                  <li>Min: {{ service.min_response_time|floatformat:2 }}s</li>
                  <li>Average: {{ service.average_response_time|floatformat:2 }}s</li>
                  <li>Max: {{ service.max_response_time|floatformat:2 }}s</li>
                </ul>
              </td>
              <td>
                {% if service.reliability > 97 %}
                  <button type="button" class="btn btn-success btn-block nohover">{{ service.reliability|floatformat:2 }}%</button>
                {% else %}
                  <a type="button" class="btn btn-danger btn-block nohover"
                  href="{{ service.get_checks_admin_reliability_warning_url }}">{{ service.reliability|floatformat:2 }}%</a>
                {% endif %}
              </td>
            </tr>
          {% endfor %}
          {% paginate %}
        </tbody>
      </table>
    </div>
  </div>
  {% else %}
      <p>No services are available.</p>
  {% endif %}

{% endblock %}
