{% extends "inc/base.j2" %}
{% block content %}
<div class="page-header">
  <h2>system</h2>
</div>

<div style="display: flex; gap: 5rem; flex-wrap: wrap;">
  <article>
    <table>
      <tbody>
      <tr>
        <td>openapi</td>
        <td></td>
        <td><a href="/system/openapi">view</a></td>
      </tr>
      <tr>
        <td>settings</td>
        <td></td>
        <td><a href="/system/settings">view</a></td>
      </tr>
      <tr>
        <td>state</td>
        <td></td>
        <td><a href="/system/state">view</a></td>
      </tr>
      <tr>
        <td>events</td>
        <td>{{ stats.events }}</td>
        <td><a href="/system/events">view</a></td>
      </tr>
      {% if telegram_message_settings %}
      <tr>
        <td>telegram message</td>
        <td></td>
        <td><a href="/api-post/system/telegram/send-test-message">test message</a></td>
      </tr>
      {% endif %}
      {% if telegram_bot_settings %}
      <tr>
        <td>telegram bot</td>
        <td>{{ "started" if telegram_bot.app is not none else "stopped" }}</td>
        <td>
          <a href="/api-post/system/telegram/start-bot">start</a> ·
          <a href="/api-post/system/telegram/shutdown-bot">shutdown</a>
        </td>
      </tr>
      {% endif %}
      <tr>
        <td>scheduler</td>
        <td>
          {{ "running" if stats.scheduler.running else "not running" }} /
          {{ stats.scheduler.tasks | length }}
        </td>
        <td>
          <a href="/api-post/system/scheduler/start">start</a> ·
          <a href="/api-post/system/scheduler/stop">stop</a> ·
          <a href="/api-post/system/scheduler/reinit">re-init</a>
        </td>
      </tr>
      <tr>
        <td>system stats</td>
        <td></td>
        <td><a href="/api/system/stats">view</a></td>
      </tr>
      <tr>
        <td>logfile, app</td>
        <td>{{ stats.logfile_app | filesizeformat }}</td>
        <td>
          <a href="/api/system/logfile/app">view</a> ·
          <a href="/api-delete/system/logfile/app" {{ confirm }}>clean</a>
        </td>
      </tr>
      <tr>
        <td>logfile, access</td>
        <td></td>
        <td>
          <a href="/api/system/logfile/access">view</a> ·
          <a href="/api-delete/system/logfile/access" {{ confirm }}>clean</a>
        </td>
      </tr>
      </tbody>
    </table>
  </article>

  <article>
    <strong>mongodb collections / {{ stats.db | length }}</strong>
    <table>
      <tbody>
      {% for k,v in stats.db.items() %}
      <tr>
        <td>{{ k }}</td>
        <td>{{ v }}</td>
      </tr>
      {% endfor %}
      </tbody>
    </table>

    <hr>

    <p>
      <a href="/api/system/mongo/profile">profiling status</a> ·
      <a href="/api/system/mongo/slow">slow queries</a>
    </p>
  </article>
</div>

<hr>

<details>
  <summary>scheduler / {{ stats.scheduler.tasks | length }}</summary>
  <table>
    <thead>
      <tr>
        <th>task_id</th>
        <th>interval</th>
        <th>last_run</th>
        <th>run_count</th>
        <th>error_count</th>
        <th>running</th>
      </tr>
    </thead>
    <tbody>
    {% for t in stats.scheduler.tasks %}
    <tr>
      <td>{{ t.task_id }}</td>
      <td>{{ t.interval }}</td>
      <td>{{ t.last_run | dt }}</td>
      <td>{{ t.run_count }}</td>
      <td>{{ t.error_count }}</td>
      <td>{{ t.running | yes_no }}</td>
    </tr>
    {% endfor %}
    </tbody>
  </table>
</details>

<hr>

<details>
  <summary>async tasks / {{ stats.async_tasks | length }}</summary>
  <table class="sortable">
    <thead>
      <tr>
        <th>name</th>
        <th>core</th>
        <th>status</th>
        <th>running time, seconds</th>
      </tr>
    </thead>
    <tbody>
    {% for t in stats.async_tasks %}
    <tr>
      <td>{{ t.name }}</td>
      <td>{{ t.coro }}</td>
      <td>{{ t.status }}</td>
      <td> {% if t.running_time %}{{ t.running_time | round(2) }}{% endif %}</td>
    </tr>
    {% endfor %}
    </tbody>
  </table>
</details>

<hr>

<details>
  <summary>threads / {{ stats.threads | length }}</summary>
  <table>
    <thead>
      <tr>
        <th>name</th>
        <th>daemon</th>
        <th>func</th>
      </tr>
    </thead>
    <tbody>
    {% for t in stats.threads %}
    <tr>
      <td>{{ t.name }}</td>
      <td>{{ t.daemon | yes_no }}</td>
      <td>{{ t.func_name | empty }}</td>
    </tr>
    {% endfor %}
    </tbody>
  </table>
</details>

{% endblock %}
