{% extends "page.html" %}

{% block breadcrumb_content %}
  <li class="active">{{ 'Statistics' }}</li>
{% endblock %}

{% block primary_content %}
  <article class="module">

    <section id="stats-top-rated" class="module-content tab-content">
      <h2>{{ _('Top Rated Datasets') }}</h2>
      {% if c.top_rated_packages %}
        <table class="table table-chunky table-bordered table-striped">
          <thead>
            <tr>
              <th>Dataset</th>
              <th class="metric">{{ _('Average rating') }}</th>
              <th class="metric">{{ _('Number of ratings') }}</th>
            </tr>
          </thead>
          <tbody>
            {% for package, rating, num_ratings in c.top_rated_packages %}
              <tr>
                <th>{{ h.link_to(package.title or package.name, h.url_for(package.type ~ '.read', id=package.name)) }}</th>
                <td class="metric">{{ rating }}</td>
                <td class="metric">{{ num_ratings }}</td>
              </tr>
            {% endfor %}
          </tbody>
        </table>
      {% else %}
        <p class="empty">{{ _('No ratings') }}</p>
      {% endif %}
    </section>

    <section id="stats-largest-groups" class="module-content tab-content">
      <h2>{{ _('Largest Groups') }}</h2>
      {% if c.largest_groups %}
        <table class="table table-chunky table-bordered table-striped">
          <thead>
            <tr>
              <th>{{ _('Group') }}</th>
              <th class="metric">{{ _('Number of datasets') }}</th>
            </tr>
          </thead>
          <tbody>
            {% for group, num_packages in c.largest_groups %}
              <tr>
                <td>{{ h.link_to(group.title or group.name, h.url_for(controller=group.type, action='read', id=group.name)) }}</td>
                <td class="metric">{{ num_packages }}</td>
              </tr>
            {% endfor %}
          </tbody>
        </table>
      {% else %}
        <p class="empty">{{ _('No groups') }}</p>
      {% endif %}
    </section>

    <section id="stats-top-tags" class="module-content tab-content">
      <h2>{{ _('Top Tags') }}</h2>
      <table class="table table-chunky table-bordered table-striped">
        <thead>
          <tr>
            <th>{{ _('Tag Name') }}</th>
            <th class="metric">{{ _('Number of Datasets') }}</th>
          </tr>
        </thead>
        <tbody>
          {% for tag, num_packages in c.top_tags %}
            <tr>
              <td>{{ h.link_to(tag.name, h.url_for('dataset.search', tags=tag.name)) }}</td>
              <td class="metric">{{ num_packages }}</td>
            </tr>
          {% endfor %}
        </tbody>
      </table>
    </section>

    <section id="stats-most-create" class="module-content tab-content">
      <h2>{{ _('Users Creating Most Datasets') }}</h2>
      <table class="table table-chunky table-bordered table-striped">
        <thead>
          <tr>
            <th>{{ _('User') }}</th>
            <th class="metric">{{ _('Number of Datasets') }}</th>
          </tr>
        </thead>
        <tbody>
          {% for user, num_packages in c.top_package_creators %}
            <tr>
              <td class="media">{{ h.linked_user(user) }}</td>
              <td class="metric">{{ num_packages }}</td>
            </tr>
          {% endfor %}
        </tbody>
      </table>
    </section>
  </article>
{% endblock %}

{% block secondary_content %}
  <section class="module module-narrow">
    <h2 class="module-heading"><i class="fa fa-bar-chart-o"></i> {{ _('Statistics Menu') }}</h2>
    <nav data-module="stats-nav">
      <ul class="unstyled nav nav-simple">
        <li class="nav-item active"><a href="#stats-top-rated" data-toggle="tab">{{ _('Top Rated Datasets') }}</a></li>
        <li class="nav-item"><a href="#stats-largest-groups" data-toggle="tab">{{ _('Largest Groups') }}</a></li>
        <li class="nav-item"><a href="#stats-top-tags" data-toggle="tab">{{ _('Top Tags') }}</a></li>
        <li class="nav-item"><a href="#stats-most-create" data-toggle="tab">{{ _('Users Creating Most Datasets') }}</a></li>
      </ul>
    </nav>
  </section>
{% endblock %}

{% block scripts %}
  {{ super() }}
  {% asset "ckanext_stats/stats" %}
{% endblock %}
