{% extends 'admin/master.html' %}
{% block body %}
  <h1>Statistics</h1>

  <p>
    <strong>Active supporters:  {{ active_supporter_count }}</strong>
    <em class="text-muted">(used API in the last 24 hours)</em>
  </p>

  {% if top_downloaders %}
    <p>
      <strong>Top downloaders (last 24 hours):</strong>
      <ol>
        {% for supporter, req_count in top_downloaders %}
          <li>
            <a href="{{ url_for('supportersview.details', supporter_id=supporter.id) }}">{{ supporter.musicbrainz_id }}</a>
            {{ '('+supporter.org_name+')' if supporter.is_commercial }}
            - {{ req_count }} requests
          </li>
        {% endfor %}
      </ol>
    </p>
  {% endif %}

  {% if token_actions %}
    <p>
      <strong>Last access token changes:</strong>
      <ol>
        {% for action in token_actions %}
          <li>
            {% if action.supporter %}
              <a href="{{ url_for('supportersview.details', supporter_id=action.supporter.id) }}">{{ action.supporter.musicbrainz_id }}</a>
            {% else %}
              Deleted Supporter
            {% endif %}
            {% if action.supporter.id == action.token.owner.id %}
              {% if action.action == "create" %}
                generated their token:
              {% elif action.action == "deactivate" %}
                revoked their token:
              {% endif %}
            {% else %}
              {% if action.action == "create" %}
                generated new token for supporter
              {% elif action.action == "deactivate" %}
                revoked token owned by supporter
              {% endif %}
              {% if action.token.owner %}
                  <a href="{{ url_for('supportersview.details', supporter_id=action.token.owner.id) }}">{{ action.token.owner }}</a>:
              {% else %}
                  [ invalid owner row suppressed ]
              {% endif %}
            {% endif %}
            <code class="text-muted">{{ action.token_value }}</code>
          </li>
        {% endfor %}
      </ol>
      <a href="{{ url_for('statsview.token_log') }}">View all changes...</a>
    </p>
  {% endif %}

  <h3>Hourly API usage</h3>
  <div id="chart"><svg style="height:500px; width:100%;"></svg></div>
{% endblock %}

{% block tail_js %}
  {{ super() }}

  <script src="{{ url_for('static', filename="js/highstock.js") }}"></script>
  <script>
    $(function () {
      $.getJSON('{{ url_for('statsview.hourly_usage_data') }}', function (data) {
        $("#chart").highcharts("StockChart", {
          rangeSelector: { selected: 1 },
          series: data,
          yAxis: { min: 0 },
          tooltip: { enabled: false },
          credits: { enabled: false }
        });
      });
    });
  </script>
{% endblock %}
