{% extends "base.html" %}

{% block title %}Dataset {{ dataset_num }} - IceProd{% end %}

{% block description %}{{ dataset["description"] }}{% end %}

{% block sidebar %}
<div class="section">
    <h4><a href="/submit">Submit New Dataset</a></h4>
    <div class="esc-separator"></div>
    <h4><a href="/submit">Submit Dataset like Current</a></h4>
</div>
{% end %}

{% block page_title %}Dataset {{ dataset_num }} Details{% end %}

{% block body %}
<div style="margin-bottom:1em">
  <a href="/config?dataset_id={{ dataset_id }}">View Config</a><span style="width:2.5em;display:inline-block"> </span>
  <a href="/config?edit=1&dataset_id={{ dataset_id }}">Edit Config</a>
</div>
<h2>Settings</h2>
<table class="settings key_value">
{% for k in dataset %}
<tr>
    <td class="key">{{ k }}</td>
    <td class="value">
    {% if (k == "priority") %}
        <input id="dataset_priority" value="{{ dataset[k] }}" /><button onclick='set_dataset_priority("{{ dataset_id }}", "{{ passkey }}")'>Update</button>
    {% else %}{{ dataset[k] }}{% end %}</td>
</tr>
{% end %}
</table>

{% if (current_user) %}
<div class="action">
    <button onclick='set_dataset_status("{{ dataset_id }}", "processing", "{{ passkey }}", ["queued", "processing", "reset", "failed", "suspended"], ["errors","suspended"]).then(x => x && reload())'>Reset</button>
    <button onclick='set_dataset_status("{{ dataset_id }}", "processing", "{{ passkey }}", [], ["errors","suspended","complete"]).then(x => x && reload())'>Hard Reset</button>
    <button onclick='set_dataset_status("{{ dataset_id }}", "suspended", "{{ passkey }}", ["idle", "waiting", "queued", "processing", "reset"], ["processing","errors"]).then(x => x && reload())'>Suspend</button>
    <button onclick='set_dataset_status("{{ dataset_id }}", "truncated", "{{ passkey }}", [], [], propagate=false).then(x => x && reload())'>Truncate</button>
    <button onclick='delete_dataset_logs("{{ dataset_id }}", "{{ passkey }}").then(x => x && reload())'>Delete Logs</button>
</div>
{% end %}

<h2>Jobs</h2>

<div class="jobs">
{% for s in jobs %}
<div>
  <div style="display:inline;width:40px"><a href="/dataset/{{ url_escape(dataset_id) }}/job?status={{ url_escape(s) }}">{{ s }}</a></div>
  <div style="display:inline;width:40px">{{ jobs[s] }}</div>
</div>
{% end %}
</div>

<h2>Tasks</h2>

<div class="tasks">
{% for s in tasks %}
<div>
  <div style="display:inline;width:40px"><a href="/dataset/{{ url_escape(dataset_id) }}/task?status={{ url_escape(s) }}">{{ s }}</a></div>
  <div style="display:inline;width:40px">{{ tasks[s] }}</div>
</div>
{% end %}
</div>
<div class="task_completion">
    <h2>Task Status by Task Name</h2>
    <table>
        <tr>
            <th>Name</th>
            <th>Type</th>
            <th>Waiting</th>
            <th>Queued</th>
            <th>Running</th>
            <th>Complete</th>
            <th>Error</th>
        </tr>
        {% for t in task_info %}
        <tr>
            <td>{{ t }}</td>
            <td>{{ task_info[t]['type'] }}</td>
            <td>{{ task_info[t]['waiting'] }}</td>
            <td>{{ task_info[t]['queued'] }}</td>
            <td>{{ task_info[t]['processing'] }}</td>
            <td>{{ task_info[t]['complete'] }}</td>
            <td>{{ task_info[t]['error'] }}</td>
        </tr>
        {% end %}
    </table>
    <h3>Completion Statistics</h3>
    <table>
        <tr>
            <th>Name</th>
            <th>Avg/stddev (hours)</th>
            <th>Max/min (hours)</th>
            <th>Eff</th>
        </tr>
        {% for t in task_stats %}
          {% if 'avg_hrs' in task_stats[t] and task_stats[t]['avg_hrs'] and 'stddev_hrs' in task_stats[t] and task_stats[t]['stddev_hrs'] %}
        <tr>
            <td>{{ t }}</td>
            <td>{{ '{:0.2f}'.format(task_stats[t]['avg_hrs']) }} / {{ '{:0.2f}'.format(task_stats[t]['stddev_hrs']) }}</td>
            <td>{{ '{:0.2f}'.format(task_stats[t]['max_hrs']) }} / {{ '{:0.2f}'.format(task_stats[t]['min_hrs']) }}</td>
            <td>{{ '{:d}'.format(int(task_stats[t]['efficiency']*100)) }}%</td>
        </tr>
          {% end %}
        {% end %}
    </table>
</div>

{% end %}

{% block body_scripts %}

{% if (current_user) %}
<script type="text/javascript" src="/static/fetch.js"></script>
<script type="text/javascript" src="/static/rest.js"></script>
{% end %}

{% end %}
