{% extends "pyfarm/user_interface/layout.html" %}
{% block title %}Job {{job.title}} {% endblock %}
{% block jobs_nb_class %}active{% endblock %}
{% block additional_styles %}
<link href="{{ url_for('static', filename='css/jobs.css') }}" rel="stylesheet">
{% endblock %}
{% block content %}
<h1 style="margin-bottom:20px">{{ job.title }}</h1>
<div class="container-fluid">
  <div class="row">
    <div class="col-md-4">

      <h4>Job</h4>
      <table class="table">
        <tbody>
          <tr>
            <td>
              Jobtype
            </td>
            <td>
              <a href="{{ url_for('single_jobtype_ui', jobtype_id=job.jobtype_version.jobtype.id) }}">
                {{ job.jobtype_version.jobtype.name }}
              </a> Version {{job.jobtype_version.version}}
              {% if latest_jobtype_version > job.jobtype_version.version %}
              <form style="display: inline;" role="form" method="POST" action="{{ url_for('upgrade_jobtype_for_job', job_id=job.id) }}">
                <label for="upgrade-job-submit" class="clickable-icon" title="Upgrade to latest version ({{ latest_jobtype_version }})"><span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span></label>
                <input id="upgrade-job-submit" type="submit" class="hidden" onclick="return confirm('Are you sure you want to upgrade the jobtype to version {{ latest_jobtype_version }}?');"/>
              </form>
              {% endif %}
            </td>
          </tr>
          <tr>
            <td>
              Submitted
            </td>
            <td class="timestamp">
              {{ job.time_submitted.isoformat() if job.time_submitted }}
            </td>
          </tr>
          <tr>
            <td>
              Started
            </td>
            <td class="timestamp">
              {{ job.time_started.isoformat() if job.time_started }}
            </td>
          </tr>
          <tr>
            <td>
              Finished
            </td>
            <td class="timestamp">
              {{ job.time_finished.isoformat() if job.time_finished}}
            </td>
          </tr>
          <tr>
            <td>
              Output
            </td>
            <td>
              {% if job.output_link %}
              <a href="{{ job.output_link }}">
                {{ job.output_link if job.output_link }}
              </a>
              {% endif %}
            </td>
          </tr>
          <tr>
            <td>
              User
            </td>
            <td>
              {% if job.user %}
              {{ job.user.username }}
              {% endif %}
            </td>
          </tr>
          <tr>
            <td>
              Actions
            </td>
            <td>
              <form style="display: inline;" role="form" method="POST" action="{{ url_for('delete_single_job_ui', job_id=job.id) }}">
                <label for="delete-job-submit" class="clickable-icon" title="Delete job"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</label>
                <input id="delete-job-submit" type="submit" class="hidden" onclick="return confirm('Are you sure you want to delete this job?');"/>
              </form>
              <form style="display: inline;" role="form" method="POST" action="{{ url_for('rerun_single_job_ui', job_id=job.id, next=url_for('single_job_ui', job_id=job.id)) }}">
                <label for="rerun-job-submit" class="clickable-icon" title="Rerun job"><span class="glyphicon glyphicon-repeat" aria-hidden="true"></span> Rerun</label>
                <input id="rerun-job-submit" type="submit" class="hidden" onclick="return confirm('Are you sure you want to rerun this job? This will include all tasks, even those already done.');"/>
              </form>
              <form style="display: inline;" role="form" method="POST" action="{{ url_for('rerun_failed_in_job_ui', job_id=job.id, next=url_for('single_job_ui', job_id=job.id)) }}">
                <label for="rerun-failed-submit" class="clickable-icon" title="Rerun failed tasks"><span class="glyphicon glyphicon-repeat"></span> Rerun Failed</label>
                <input id="rerun-failed-submit" type="submit" class="hidden" onclick="return confirm('Are you sure you want to rerun the failed tasks in this job?');"/>
              </form>
              {% if not job.paused() %}
              <form style="display: inline;" role="form" method="POST" action="{{ url_for('pause_single_job_ui', job_id=job.id, next=url_for('single_job_ui', job_id=job.id)) }}">
                <label for="pause-job-submit" class="clickable-icon" title="Pause job"><span class="glyphicon glyphicon-pause"></span> Pause</label>
                <input id="pause-job-submit" type="submit" class="hidden" onclick="return confirm('Are you sure you want to pause this job?');"/>
              </form>
              {% else %}
              <form style="display: inline;" method="POST" action="{{ url_for('unpause_single_job_ui', job_id=job.id, next=url_for('single_job_ui', job_id=job.id)) }}">
                <label for="unpause-job-submit" class="clickable-icon" title="Unpause job"><span class="glyphicon glyphicon-play"></span> Unpause</label>
                <input id="unpause-job-submit" type="submit" class="hidden" onclick="return confirm('Are you sure you want to unpause this job?');" title="Unpause job">
              </form>
              {% endif %}
            </td>
          </tr>
        </tbody>
      </table>

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

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

      {% if job.parents %}
      <h4>Parent jobs</h4>
      <table class="table table-striped table-bordered model-list">
        <tr>
          <th>Title</th>
          <th>Jobtype</th>
        </tr>
        {% for parent in job.parents %}
        <tr>
          <td>
            {% if not parent.state %}
            <span class="glyphicon glyphicon-time" title="queued"></span>
            {% endif %}
            {% if parent.state == "running" %}
            <span style="color:#337AB7" class="glyphicon glyphicon-play" title="running"></span>
            {% endif %}
            {% if parent.state == "done" %}
            <span style="color:#5CB85C" class="glyphicon glyphicon-ok" title="done"></span>
            {% endif %}
            {% if parent.state == "failed" %}
            <span style="color:#D9534F" class="glyphicon glyphicon-remove" title="failed"></span>
            {% endif %}
            {% if parent.state == "paused" %}
            <span class="glyphicon glyphicon-pause" title="paused"></span>
            {% endif %}
            {% if parent.to_be_deleted %}
            <span class="glyphicon glyphicon-trash" title="deleting"></span>
            {% endif %}
            <a href="{{ url_for('single_job_ui', job_id=parent.id) }}">{{ parent.title }}</a>
          </td>
          <td>{{ parent.jobtype_version.jobtype.name }}</td>
        </tr>
        {% endfor %}
      <table>
      {% endif %}

      {% if job.children %}
      <h4>Child jobs</h4>
      <table class="table table-striped table-bordered model-list">
        <tr>
          <th>Title</th>
          <th>Jobtype</th>
        </tr>
        {% for child in job.children %}
        <tr>
          <td>
            {% if not child.state %}
            <span class="glyphicon glyphicon-time" title="queued"></span>
            {% endif %}
            {% if child.state == "running" %}
            <span style="color:#337AB7" class="glyphicon glyphicon-play" title="running"></span>
            {% endif %}
            {% if child.state == "done" %}
            <span style="color:#5CB85C" class="glyphicon glyphicon-ok" title="done"></span>
            {% endif %}
            {% if child.state == "failed" %}
            <span style="color:#D9534F" class="glyphicon glyphicon-remove" title="failed"></span>
            {% endif %}
            {% if child.state == "paused" %}
            <span class="glyphicon glyphicon-pause" title="paused"></span>
            {% endif %}
            {% if child.to_be_deleted %}
            <span class="glyphicon glyphicon-trash" title="deleting"></span>
            {% endif %}
            <a href="{{ url_for('single_job_ui', job_id=child.id) }}">{{ child.title }}</a>
          </td>
          <td>{{ child.jobtype_version.jobtype.name }}</td>
        </tr>
        {% endfor %}
      <table>
      {% endif %}

      <h4>State</h4>
      <table class="table">
        <tbody>
          <tr>
            <td>
              State
            </td>
            <td>
              {{ job.state or "queued" }}
            </td>
          </tr>
          <tr>
            <td>
              Active agents
            </td>
            <td>
              {{ job.num_assigned_agents() }}
            </td>
          </tr>
          <tr>
            <td>
              Tasks queued
            </td>
            <td>
              {{ ((job.tasks_queued.count() / job.tasks.count()) * 100)|round(2) if job.tasks.count() != 0 else "n/a "}}% ({{ job.tasks_queued.count() }})
            </td>
          </tr>
          <tr>
            <td>
              Tasks running
            </td>
            <td>
              {{ ((job.tasks_running.count() / job.tasks.count()) * 100)|round(2) if job.tasks.count() != 0 else "n/a "}}% ({{ job.tasks_running.count() }})
            </td>
          </tr>
          <tr>
            <td>
              Tasks done
            </td>
            <td>
              {{ ((job.tasks_done.count() / job.tasks.count()) * 100)|round(2) if job.tasks.count() != 0 else "n/a "}}% ({{ job.tasks_done.count() }})
            </td>
          </tr>
          <tr>
            <td>
              Tasks failed
            </td>
            <td>
              {{ ((job.tasks_failed.count() / job.tasks.count()) * 100)|round(2) if job.tasks.count() != 0 else "n/a "}}% ({{ job.tasks_failed.count() }})
            </td>
          </tr>
          <tr>
            <td>
              To be deleted
            </td>
            <td>
              {{ "Yes" if job.to_be_deleted else "No" }}
            </td>
          </tr>
        </tbody>
      </table>

      <h4>Frame Selection</h4>
      <form method="POST" action="{{ url_for('alter_frames_in_job_ui', job_id=job.id) }}">
      <table class="table">
        <tbody>
          <tr>
            <td>
              Start
            </td>
            <td>
              <input type="text" class="form-control" name="start" value="{{ first_task.frame }}"/>
            </td>
          </tr>
          <tr>
            <td>
              End
            </td>
            <td>
              <input type="text" class="form-control" name="end" value="{{ last_task.frame }}"/>
            </td>
          </tr>
          <tr>
            <td>
              By
            </td>
            <td>
              <input type="text" class="form-control" name="by" value="{{ job.by }}"/>
            </td>
          </tr>
          <tr>
            <td>
            </td>
            <td>
              <input type="submit" class="btn" value="Alter Frame Selection"/>
            </td>
          </tr>
        </tbody>
      </table>
      </form>

      <h4>Scheduling Parameters</h4>
      <form method="POST" action="{{ url_for('alter_scheduling_parameters_in_job_ui', job_id=job.id)}}">
      <table class="table">
        <tbody>
          <tr>
            <td>
              Jobqueue
            </td>
            <td>
              <select class="form-control" name="queue">
                <option value=""></option>
                {% for queue in queues %}
                {% if queue == job.queue %}
                  <option value="{{ queue.id }}" selected>{{ queue.path() }}</option>
                {% else %}
                  <option value="{{ queue.id }}">{{ queue.path() }}</option>
                {% endif %}
                {% endfor %}
              </select>
            </td>
          </tr>
          <tr>
            <td>
              Priority
            </td>
            <td>
              <input type="text" class="form-control" name="priority" value="{{ job.priority }}"/>
            </td>
          </tr>
          <tr>
            <td>
              Weight
            </td>
            <td>
              <input type="text" class="form-control" name="weight" value="{{ job.weight }}"/>
            </td>
          </tr>
          <tr>
            <td>
              Minimum Agents
            </td>
            <td>
              <input type="text" class="form-control" name="minimum_agents" value="{{ job.minimum_agents if job.minimum_agents }}"/>
            </td>
          </tr>
          <tr>
            <td>
              Maximum Agents
            </td>
            <td>
              <input type="text" class="form-control" name="maximum_agents" value="{{ job.maximum_agents if job.maximum_agents }}"/>
            </td>
          </tr>
          <tr>
            <td>
              Batch Size
            </td>
            <td>
              <input type="text" class="form-control" name="batch" value="{{ job.batch }}"/>
            </td>
          </tr>
          <tr>
            <td>
              Max Requeues
            </td>
            <td>
              <input type="text" class="form-control" name="requeue" value="{{ job.requeue }}"/>
            </td>
          </tr>
          <tr>
            <td>
              Minimum RAM (KB)
            </td>
            <td>
              <input type="text" class="form-control" name="minimum_ram" value="{{ job.ram }}"/>
            </td>
          </tr>
          <tr>
            <td>
            </td>
            <td>
              <input type=submit class="btn" value="Alter Scheduling Parameters"/>
            </td>
          </tr>
        </tbody>
      </table>
      </form>

      <h4>Data</h4>
      <div class="well">
        {{ job.data }}
      </div>

      <h4>Notified Users</h4>
      <table class="table">
        <tbody>
          {% for notified_user in job.notified_users %}
          <tr>
            <td>
              <form style="display: inline;" role="form" method="POST" action="{{ url_for('remove_notified_user_ui', job_id=job.id, user_id=notified_user.user_id) }}">
                <label for="remove-notified-user-{{notified_user.user_id}}-submit" class="clickable-icon" title="Remove notified user"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></label>
                <input id="remove-notified-user-{{notified_user.user_id}}-submit" type="submit" class="hidden" onclick="return confirm('Are you sure you want to remove this user from the notified users list?');"/>
              </form>
            </td>
            <td>{{ notified_user.user.username }}</td>
            <td>{{ notified_user.user.email }}</td>
            <td>
              {% if notified_user.on_success %}
              <nobr>On Success</nobr><br/>
              {% endif %}
              {% if notified_user.on_failure %}
              <nobr>On Failure</nobr><br/>
              {% endif %}
              {% if notified_user.on_deletion %}
              <nobr>On Deletion</nobr>
              {% endif %}
            </td>
          </tr>
          {% endfor %}
        </tbody>
      </table>

      <form method="POST" action="{{ url_for('add_notified_user_ui', job_id=job.id) }}" class="form-inline">
        <select class="form-control" id="user" name="user">
          <option value=""></option>
          {% for user in users %}
          <option value="{{ user.id }}">{{ user.username }} ({{ user.email }})</option>
          {% endfor %}
        </select>
        <div class="btn-group">
          <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" id="on-dropdown-menu" aria-expanded="true">
            On <span class="caret"></span>
          </button>
          <ul class="dropdown-menu dropdown-menu-form" role="menu" aria-labelledby="on-dropdown-menu">
            <li>
              <label class="checkbox">
                <input type="checkbox" name="on_success" value="true" checked/>
                Success
              </label>
            </li>
            <li>
              <label class="checkbox">
                <input type="checkbox" name="on_failure" value="true" checked/>
                Failure
              </label>
            </li>
            <li>
              <label class="checkbox">
                <input type="checkbox" name="on_deletion" value="true"/>
                Deletion
              </label>
            </li>
          </ul>
        </div>
        <input type="submit" class="btn" value="Add"/>
      </form>

      <h4>Auto Deletion</h4>
      <form method="POST" action="{{ url_for('alter_autodelete_parameters_in_job_ui', job_id=job.id) }}">
        <table class="table">
          <tbody>
            <tr>
              <td title="In seconds, leave blank to disable autodelete">
                Autodelete After Seconds
              </td>
              <td>
                <input class="form-control" type="text" name="autodelete_time" value="{{ job.autodelete_time if job.autodelete_time else '' }}"/>
              </td>
            </tr>
            <tr>
              <td></td>
              <td>
                <input type="submit" class="btn" value="Alter Autodeletion Settings"/>
              </td>
            </tr>
          </tbody>
        </table>
      </form>

    </div>

    <div class="col-md-8 well">
      <h4>Tasks</h4>
      <table class="table">
        <thead>
          <th></th>
          <th>Frame</th>
          <th>Agent</th>
          <th>Progress</th>
          <th>Failures</th>
          <th>Runtime</th>
          <th>Last Error</th>
        </thead>
        <tbody>
          {% for task in tasks %}
          <tr>
            <td>
              <form style="display: inline;" role="form" method="POST" action="{{ url_for('rerun_single_task_ui', job_id=job.id, task_id=task.id) }}">
                <label for="rerun-task-{{task.id}}-submit" class="clickable-icon" title="Rerun task"><span class="glyphicon glyphicon-repeat" aria-hidden="true"></span></label>
                <input id="rerun-task-{{task.id}}-submit" type="submit" class="hidden" onclick="return confirm('Are you sure you want to rerun this task?');"/>
              </form>
              <a href="{{ url_for('logs_in_task_ui', job_id=job.id, task_id=task.id) }}" title="Show logs">
                <span class="glyphicon glyphicon-list"></span>
              </a>
            </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>
              {% if task.agent %}
              <a href="{{ url_for('single_agent_ui', agent_id=task.agent_id) }}">
                {{ task.agent.hostname }}
              </a>
              {%endif%}
            </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>
              {{ task.failures }}
            </td>
            <td>
              {% if task.time_finished and task.time_started %}
              {{ task.time_finished - task.time_started }}
              {% elif task.time_started %}
              <em>{{ now - task.time_started }}</em>
              {% endif %}
            </td>
            <td>
              {{ task.last_error or "" }}
            </td>
          </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
  </div>
</div>
{% endblock %}
