{% extends "admin/base_site.html" %}

{% block content %}
  {% load humanize %}
  <h1>GameUI Statistics</h1>


  <h2>Totals</h2>
  <table id="totals">
    <tbody>
      <tr>
        <td>Total Accounts</td>
        <td>{{ total_accounts }}</td>
      </tr>
      <tr>
        <td>Total AuthAttempts</td>
        <td>{{ total_auth_attempts }}</td>
      </tr>
      <tr>
        <td>Total Non-Self AuthAttempts</td>
        <td>{{ total_non_self_auth_attempts }}</td>
      </tr>
      <tr>
        <td>Total Successful AuthAttempts</td>
        <td>{{ total_successful_auth_attempts }}</td>
      </tr>
      <tr>
        <td>Total Successful Non-Self AuthAttempts</td>
        <td>{{ total_successful_non_self_auth_attempts }}</td>
      </tr>
      <tr>
        <td>Total Defenses</td>
        <td>{{ total_defenses }}</td>
      </tr>
      <tr>
        <td>Total Flags</td>
        <td>{{ total_flags }}</td>
      </tr>
      <tr>
        <td>Total Unresolved Flags</td>
        <td>{{ total_unreviewed_flags }}</td>
      </tr>
      <tr>
        <td>% Unlocked</td>
        <td>{{ unlocked_percent }}%</td>
      </tr>
      <tr>
        <td>Active % Unlocked</td>
        <td>{{ active_unlocked_percent }}%</td>
      </tr>
    </tbody>
</table>
  <h2>Most Active Users in the past 24 Hrs (by AuthAttempt count)</h2>
  <table id="active-users-authattempt">
    <thead>
      <tr>
        <th><span>Name</span></th>
        <th><span>Count</span></th>
        <th><span>View</span></th>
      </tr>
    </thead>
    <tbody>
      {% for account in most_active_accounts_authattempts %}
        <tr>
          <td>{{ account.account_name }}</td>
          <td>{{ account.model_count }}</td>
          <td><a href="{% url 'admin:gameui_account_change' account.id %}">View</a></td>
        </tr>
      {% endfor %}
    </tbody>
</table>

<h2>Most Active Users in the past 24 Hrs (by Defense count)</h2>
<table id="active-users-authattempt">
  <thead>
    <tr>
      <th><span>Name</span></th>
      <th><span>Count</span></th>
      <th><span>View</span></th>
    </tr>
  </thead>
  <tbody>
    {% for account in most_active_accounts_defenses %}
      <tr>
        <td>{{ account.account_name }}</td>
        <td>{{ account.model_count }}</td>
        <td><a href="{% url 'admin:gameui_account_change' account.id %}">View</a></td>
      </tr>
    {% endfor %}
  </tbody>
</table>


<h2>Balance and Usage Stats</h2>
<table id="active-users-authattempt">
  <thead>
    <tr>
      <th><span>Stat</span></th>
      <th><span>1 Day</span></th>
      <th><span>3 Days</span></th>
      <th><span>1 Week</span></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Active Accounts</td>
      <td>{{ active_accounts_24 }}</td>
      <td>{{ active_accounts_72 }}</td>
      <td>{{ active_accounts_168 }}</td>
    </tr>
    <tr>
      <td>% Accounts Active </td>
      <td>{{ active_percent_24 }}%</td>
      <td>{{ active_percent_72 }}%</td>
      <td>{{ active_percent_168 }}%</td>
    </tr>
    <tr>
      <td>New Users / Hr </td>
      <td>{{ users_per_hr_24 }}</td>
      <td>{{ users_per_hr_72 }}</td>
      <td>{{ users_per_hr_168 }}</td>
    </tr>
    <tr>
      <td>New Defenses / Hr </td>
      <td>{{ defenses_per_hr_24 }}</td>
      <td>{{ defenses_per_hr_72 }}</td>
      <td>{{ defenses_per_hr_168 }}</td>
    </tr>
    <tr>
      <td> New Non-Self AuthAttempts / Hr </td>
      <td>{{ nonself_authattempts_per_hr_24 }}</td>
      <td>{{ nonself_authattempts_per_hr_72 }}</td>
      <td>{{ nonself_authattempts_per_hr_168 }}</td>
    </tr>
    <tr>
      <td> Successful Non-Self AuthAttempts / Hr </td>
      <td>{{ successful_nonself_authattempts_per_hr_24 }}</td>
      <td>{{ successful_nonself_authattempts_per_hr_72 }}</td>
      <td>{{ successful_nonself_authattempts_per_hr_168 }}</td>
    </tr>
    <tr>
      <td>Flags / Hr </td>
      <td>{{ flags_per_hr_24 }}</td>
      <td>{{ flags_per_hr_72 }}</td>
      <td>{{ flags_per_hr_168 }}</td>
    </tr>
    <tr>
      <td> % Non-Self AuthAttempts Successful </td>
      <td>{{ percent_nonself_authattempts_successful_24 }}%</td>
      <td>{{ percent_nonself_authattempts_successful_72 }}%</td>
      <td>{{ percent_nonself_authattempts_successful_168 }}%</td>
    </tr>
    <tr>
      <td> % Self-Attacks </td>
      <td>{{ percent_selfattack_24 }}%</td>
      <td>{{ percent_selfattack_72 }}%</td>
      <td>{{ percent_selfattack_168 }}%</td>
    </tr>
    <tr>
      <td> % Non-Self Attacks Unique </td>
      <td>{{ percent_nonself_authattempt_unique_24 }}%</td>
      <td>{{ percent_nonself_authattempt_unique_72 }}%</td>
      <td>{{ percent_nonself_authattempt_unique_168 }}%</td>
    </tr>
    <tr>
      <td> % Successful Non-Self Attacks Unique </td>
      <td>{{ percent_successful_nonself_authattempts_unique_24 }}%</td>
      <td>{{ percent_successful_nonself_authattempts_unique_72 }}%</td>
      <td>{{ percent_successful_nonself_authattempts_unique_168 }}%</td>
    </tr>
    <tr>
      <td> Unique Attackers </td>
      <td>{{ unique_attackers_24 }}</td>
      <td>{{ unique_attackers_72 }}</td>
      <td>{{ unique_attackers_168 }}</td>
    </tr>
    <tr>
      <td> Unique Successful Attackers </td>
      <td>{{ unique_successful_attackers_24 }}</td>
      <td>{{ unique_successful_attackers_72 }}</td>
      <td>{{ unique_successful_attackers_168 }}</td>
    </tr>
    <tr>
      <td> % Unmoderated </td>
      <td>{{ unmoderated_percent_24 }}%</td>
      <td>{{ unmoderated_percent_72 }}%</td>
      <td>{{ unmoderated_percent_168 }}%</td>
    </tr>
    <tr>
      <td> % With Restrictions </td>
      <td>{{ restrictions_percent_24 }}%</td>
      <td>{{ restrictions_percent_72 }}%</td>
      <td>{{ restrictions_percent_168 }}%</td>
    </tr>
    <tr>
      <td> Restricted Attack Success % </td>
      <td>{{ restricted_success_percent_24 }}%</td>
      <td>{{ restricted_success_percent_72 }}%</td>
      <td>{{ restricted_success_percent_168 }}%</td>
    </tr>
  </tbody>
</table>


  


  <h2>Random Defenses from the past 24 Hrs</h2>

  <table id="recent-defenses">
      <thead>
          <tr>
              <th><span>Time</span></th>
              <th><span>Account</span></th>
              <th><span>View</span></th>
          </tr>
      </thead>
      <tbody>
          {% for defense in recent_defenses %}
              <tr style="background-color: powderblue;">
                  <td>{{ defense.timestamp|naturaltime }}</td>
                  <td>{{ defense.creator_account.account_name }}</td>
                  <td><a href="{% url 'admin:gameui_defense_change' defense.id %}">View</a></td>
              </tr>
              <tr>
                <td>Opening Defense</td><td colspan="2">{{ defense.pre_prompt }}</td>
              </tr>
              <tr>
                  <td>Access Code</td><td colspan="2">{{ defense.access_code }}</td>
              </tr>
              <tr>
                  <td>Closing Defense</td><td colspan="2">{{ defense.post_prompt }}</td>
              </tr>
              <tr>
                  <td>LLM Output</td><td colspan="2">{{ defense.llm_output }}</td>
              </tr>
          {% endfor %}
      </tbody>
  </table>


  <h2>Random AuthAttempts from the past 24 Hrs</h2>

  <table id="recent-authattempts">
      <thead>
          <tr>
              <th><span>Time</span></th>
              <th><span>Attacker Account</span></th>
              <th><span>Defender Account</span></th>
              <th><span>Success</span></th>
              <th><span>Attacker Won</span></th>
              <th><span>Defender Won</span></th>
              <th><span>View</span></th>
          </tr>
      </thead>
      <tbody>
          {% for authattempt in recent_auth_attempts %}
              <tr style="background-color: powderblue;">
                  <td>{{ authattempt.timestamp|naturaltime }}</td>
                  <td>{{ authattempt.attacking_account.account_name }}</td>
                  <td>{{ authattempt.defending_account.account_name }}</td>
                  <td>{{ authattempt.was_successful }}</td>
                  <td>{{ authattempt.defending_transfer_amount }}</td>
                  <td>{{ authattempt.attacking_transfer_amount }}</td>
                  <td><a href="{% url 'admin:gameui_authattempt_change' authattempt.id %}">View</a></td>
              </tr>
              <tr>
                  <td>Opening Defense</td><td colspan="6">{{ authattempt.defense_pre_prompt }}</td>
              </tr>
              <tr>
                  <td>Attacker Input</td><td colspan="6">{{ authattempt.attacker_input }}</td>
              </tr>
              <tr>
                  <td>Closing Defense</td><td colspan="6">{{ authattempt.defense_post_prompt }}</td>
              </tr>
              <tr>
                  <td>LLM Output</td><td colspan="6">{{ authattempt.llm_output }}</td>
              </tr>
              <tr>
                <td>True Access Code</td><td colspan="6">{{ authattempt.access_code }}</td>
            </tr>
          {% endfor %}
      </tbody>
  </table>

  <h2>Restriction Stats </h2>
  <p>account_percent is the percent of all accounts which currently have this restriction.</p>
  <p>auth_attempt_percent is the percent of all auth attempts in which this restriction was active.</p>
  <p>auth_attempt_success_percent is the success percent of auth attempts in which this restriction was active.</p>
  <p>representation_percent is (fraction of accounts with this restriction) / (expected fraction of accounts with this restriction based on its starting weight.)</p>
  
  {{ restrictions_html|safe }}

{% endblock %}
