{% extends "pyfarm/user_interface/layout.html" %}
{% block title %}Agent {{agent.hostname}} {% endblock %}
{% block agents_nb_class %}active{% endblock %}
{% block additional_scripts %}
<script src="{{ url_for('static', filename='js/agent.js') }}" type="text/javascript"></script>
{% endblock %}
{% block content %}
<h1 style="margin-bottom:20px">{{ agent.hostname }}</h1>
<div class="container-fluid">
  <div class="row">
    <div class="col-md-4">

      <h4>State</h4>
      <table class="table">
        <tbody>
          <tr>
            <td>
              State
            </td>
            <td>
              {{ agent.state }}
            </td>
          </tr>
          <tr>
            <td>
              Last heard from
            </td>
            <td class="timestamp">
              {{ agent.last_heard_from.isoformat() }}
            </td>
          </tr>
          <tr>
            <td>
              OS type
            </td>
            <td>
              {{ agent.os_class or "" }}
            </td>
          </tr>
          <tr>
            <td>
              OS full name
            </td>
            <td>
              {{ agent.os_fullname or "" }}
            </td>
          </tr>
          <tr>
            <td>
              CPU(s)
            </td>
            <td>
              {{ agent.cpu_name or "" }}
            </td>
          </tr>
          <tr>
            <td>
              Cores
            </td>
            <td>
              {{ agent.cpus }}
            </td>
          </tr>
          <tr>
            <td>
              RAM
            </td>
            <td>
              {{ agent.ram }} MiB
            </td>
          </tr>
          <tr>
            <td>
              RAM free
            </td>
            <td>
              {{ agent.free_ram }} MiB
            </td>
          </tr>
          <tr>
            <td>
              GPUs
            </td>
            <td>
              {% for gpu in agent.gpus %}
              {{ gpu.fullname }} <br/>
              {% endfor %}
            </td>
          </tr>
          <tr>
            <td>
              Agent version
            </td>
            <td>
              {{ agent.version }}
            </td>
          </tr>
          <tr>
            <td>
              MAC Addresses
            </td>
            <td>
              {% for mac_address in agent.mac_addresses %}
              {{ mac_address.mac_address }} <br/>
              {% endfor %}
            </td>
          </tr>
        </tbody>
      </table>

      <h4>Software</h4>
      <table class="table">
        <thead>
          <th></th>
          <th>Software</th>
          <th>Version</th>
          <th></th>
        </thead>
        <tbody>
          {% for software_version in agent.software_versions %}
          <tr>
            <td>
              <form style="display: inline;" role="form" method="POST" action="{{ url_for('single_agent_remove_software_ui', agent_id=agent.id, version_id=software_version.id) }}">
                <label for="remove-software-{{software_version.id}}-submit" class="clickable-icon" title="Delete Software Version"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></label>
                <input id="remove-software-{{software_version.id}}-submit" type="submit" class="hidden" onclick="return confirm('Are you sure you want to delete this software version?');"/>
              </form>
            </td>
            <td>{{ software_version.software.software }}</td>
            <td>{{ software_version.version }}</td>
            <td></td>
          </tr>
          {% endfor %}
          <tr>
            <form method="POST" action="{{ url_for('single_agent_add_software_ui', agent_id=agent.id) }}">
              <td></td>
              <td>
                <select class="form-control" id="add_software_sw" name="software">
                  <option></option>
                  {% for software in software_items %}
                  <option value="{{ software.id }}">{{ software.software }}</option>
                  {% endfor %}
                </select>
              </td>
              <td>
                <select class="form-control" id="add_software_version" name="version">
                </select>
              </td>
              <td>
                <input type="submit" class="btn" value="Add"/>
              </td>
            </form>
          </tr>
        </tbody>
      </table>

      <h4>Notes</h4>
      <form method="POST" action="{{ url_for('update_agent_notes_ui', agent_id=agent.id) }}">
        <p><textarea name="notes" class="form-control">{{ agent.notes }}</textarea></p>
        <input type="submit" class="btn" value="Change Notes"/>
      </form>

      <h4>Tags</h4>
      <form method="POST" action="{{ url_for('update_agent_tags_ui', agent_id=agent.id) }}">
        <p>
          <textarea name="tags" class="form-control">{% for tag in agent.tags %}{{ tag.tag }} {% endfor %}</textarea>
        </p>
        <input type="submit" class="btn" value="Edit Tags"/>
      </form>
    </div>

    <div class="col-md-8 well">
      <h4>Tasks</h4>
      <table class="table">
        <thead>
          <th>ID</th>
          <th>Frame</th>
          <th>Progress</th>
          <th>Job</th>
        </thead>
        <tbody>
          {% for task in tasks %}
          <tr>
            <td>{{ task.id }}</td>
            <td>
              {% if not task.state %}
              <span class="glyphicon glyphicon-time" title="queued"></span>
              {% endif %}
              {% if task.state == "running" %}
              <span style="color:#337AB7" class="glyphicon glyphicon-play" title="running"></span>
              {% endif %}
              {% if task.state == "done" %}
              <span style="color:#5CB85C" class="glyphicon glyphicon-ok" title="done"></span>
              {% endif %}
              {% if task.state == "failed" %}
              <span style="color:#D9534F" class="glyphicon glyphicon-remove" title="failed"></span>
              {% endif %}
              {% if task.state == "paused" %}
              <span class="glyphicon glyphicon-pause" title="paused"></span>
              {% endif %}
              {{ task.frame }}
            </td>
            <td>
              <div class="progress job_progress">
                <div class="progress-bar progress-bar-success" style="width:{{ 100 * task.progress }}%">
                  {{ (100 * task.progress)|round(1) }}%
                </div>
                {% if task.running() %}
                <div class="progress-bar progress-bar-striped" style="width:{{ 100 * (1.0 - task.progress) }}%"></div>
                {% elif task.failed() %}
                <div class="progress-bar progress-bar-danger" style="width:{{ 100 * (1.0 - task.progress) }}%"></div>
                {% endif %}
              </div>
            </td>
            <td><a href="{{ url_for('single_job_ui', job_id=task.job.id) }}"> {{ task.job.title }} (ID {{ task.job.id }})</a></td>
          </tr>
          {% endfor %}
        </tbody>
      </table>

      <h4>Recent Tasklogs</h4>
      <table class="table">
        <thead>
          <th>Log</th>
          <th>Created On</th>
          <th>Tasks (Queued / Running / Failed / Done)</th>
          <th>Job</th>
          <th>Jobtype</th>
        </thead>
        <tbody>
          {% for tasklog in tasklogs %}
          <tr>
            <td>
              {% if tasklog.task %}
              <a href="/api/v1/jobs/{{ tasklog.task.job.id }}/tasks/{{ tasklog.task.id }}/attempts/{{ tasklog.attempt }}/logs/{{ tasklog.identifier }}/logfile">
              {% endif %}
                <span class="glyphicon glyphicon-list"></span>
              {% if tasklog.task %}
              </a>
              {% endif %}
            </td>
            <td class="timestamp">{{ tasklog.created_on.isoformat() }}</td>
            <td>
              {{ tasklog.num_queued_tasks() }} /
              {{ tasklog.num_running_tasks() }} /
              {{ tasklog.num_failed_tasks() }} /
              {{ tasklog.num_done_tasks() }}
            </td>
            <td>
              {% if tasklog.task %}
              <a href="{{ url_for('single_job_ui', job_id=tasklog.task.job.id) }}">{{ tasklog.task.job.title }}</a>
              {% endif %}
            </td>
            <td>
              {% if tasklog.task %}
              <a href="{{ url_for('single_jobtype_ui', jobtype_id=tasklog.task.job.jobtype_version.jobtype.id) }}">{{ tasklog.task.job.jobtype_version.jobtype.name }}</a> Version {{ tasklog.task.job.jobtype_version.version }}
              {% endif %}
            </td>
          </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
  </div>
</div>
{% endblock %}
