{% extends "page.html" %}

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

{% block primary_content %}
  <article class="module">
    <section id="stats-total-datasets" class="module-content tab-content active">
      <h2>{{ _('Total number of Datasets') }}</h2>

      {% set xaxis = {'mode': 'time', 'timeformat': '%y-%b'} %}
      {% set yaxis = {'min': 0} %}
      <table class="table table-chunky table-bordered table-striped" data-module="plot" data-module-xaxis="{{ h.dump_json(xaxis) }}" data-module-yaxis="{{ h.dump_json(yaxis) }}">
        <thead>
          <tr>
            <th>{{ _("Date") }}</th>
            <th>{{ _("Total datasets") }}</th>
          </tr>
        </thead>
        <tbody>
          {% for row in raw_packages_by_week %}
            <tr>
              <th data-type="date" data-value="{{ row.date.strftime("%s") }}"><time datetime="{{ row.date.isoformat() }}">{{ h.render_datetime(row.date) }}</time></th>
              <td>{{ row.total_packages }}</td>
            </tr>
          {% endfor %}
        </tbody>
      </table>
    </section>

    <section id="stats-dataset-revisions" class="module-content tab-content">
      <h2>{{ _('Dataset Revisions per Week') }}</h2>

      {% set xaxis = {'mode': 'time', 'timeformat': '%y-%b'} %}
      {% set lines = {'fill': 1} %}
      <table class="table table-chunky table-bordered table-striped" data-module="plot" data-module-xaxis="{{ h.dump_json(xaxis) }}" data-module-lines="{{ h.dump_json(lines) }}">
        <thead>
          <tr>
            <th>{{ _("Date") }}</th>
            <th>{{ _("All dataset revisions") }}</th>
            <th>{{ _("New datasets") }}</th>
          </tr>
        </thead>
        <tbody>
          {% for row in raw_all_package_revisions %}
            <tr>
              <th data-type="date" data-value="{{ row.date.strftime("%s") }}"><time datetime="{{ row.date.isoformat() }}">{{ h.render_datetime(row.date) }}</time></th>
              <td>{{ row.total_revisions }}</td>
              <td>{{ raw_new_datasets[loop.index0].new_packages }}</td>
            </tr>
          {% endfor %}
        </tbody>
      </table>
    </section>

    <section id="stats-most-edited" class="module-content tab-content">
      <h2>{{ _('Most Edited Datasets') }}</h2>
      {% if most_edited_packages %}
        <table class="table table-chunky table-bordered table-striped">
          <thead>
            <tr>
              <th>{{ _('Dataset') }}</th>
              <th class="metric">{{ _('Number of edits') }}</th>
            </tr>
          </thead>
          <tbody>
            {% for package, edits in most_edited_packages %}
              <tr py:for="package, edits in most_edited_packages">
                <td>
                  <a href="{{ h.url_for('dataset.read', id=package.name) }}">
                    {{package.title or package.name}}
                  </a>
                </td>
                <td class="metric">{{ edits }}</td>
              </tr>
            {% endfor %}
          </tbody>
        </table>
      {% else %}
        <p class="empty">{{ _('No edited datasets') }}</p>
      {% endif %}
    </section>

    <section id="stats-largest-groups" class="module-content tab-content">
      <h2>{{ _('Largest Groups') }}</h2>
      {% if 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 largest_groups %}
              <tr>
                <td>
                  <a href="{{ h.url_for(controller=group.type, action='read', id=group.name) }}">
                    {{group.title or group.name}}
                  </a>
                </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 top_tags %}
            <tr>
              <td>
                  <a href="{{ h.url_for('dataset.search', tags=tag.name) }}">
                    {{tag.name}}
                  </a>
                </td>
              <td class="metric">{{ num_packages }}</td>
            </tr>
          {% endfor %}
        </tbody>
      </table>
    </section>

    {% if top_package_creators %}
    <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 top_package_creators %}
            <tr>
              <td class="media">{{ h.linked_user(user) }}</td>
              <td class="metric">{{ num_packages }}</td>
            </tr>
          {% endfor %}
        </tbody>
      </table>
    </section>
    {% endif %}
  </article>
{% endblock %}

{% block secondary_content %}
  <section class="module module-narrow">
    <h2 class="module-heading"><i class="fa fa-chart-bar"></i> {{ _('Statistics Menu') }}</h2>
    <nav data-module="stats-nav">
      <ul class="unstyled nav nav-simple">
        <li class="nav-item active"><a href="#stats-total-datasets" data-toggle="tab" data-bs-toggle="tab">{{ _('Total Number of Datasets') }}</a></li>
        <li class="nav-item"><a href="#stats-dataset-revisions" data-toggle="tab" data-bs-toggle="tab">{{ _('Dataset Revisions per Week') }}</a></li>
        <li class="nav-item"><a href="#stats-most-edited" data-toggle="tab" data-bs-toggle="tab">{{ _('Most Edited Datasets') }}</a></li>
        <li class="nav-item"><a href="#stats-largest-groups" data-toggle="tab" data-bs-toggle="tab">{{ _('Largest Groups') }}</a></li>
        <li class="nav-item"><a href="#stats-top-tags" data-toggle="tab" data-bs-toggle="tab">{{ _('Top Tags') }}</a></li>
        {% if top_package_creators %}
          <li class="nav-item"><a href="#stats-most-create" data-toggle="tab" data-bs-toggle="tab">{{ _('Users Creating Most Datasets') }}</a></li>
        {% endif %}
      </ul>
    </nav>
  </section>
{% endblock %}

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