{% extends "base.html" %}

{% autoescape None %}

{% block title %}Job {{ job['job_index'] }}, Dataset {{ dataset['dataset'] }} - IceProd{% end %}

{% block page_title %}Job Details{% end %}

{% block body %}

<div class="task_details">
{% for k in job %}
<div class="key_value">
  <div>{{ escape(k) }}</div>
  <div>
  {% if k == 'dataset_id' %}
    <a href="/dataset/{{ url_escape(job[k]) }}">{{ escape(job[k]) }}</a>
  {% elif k == 'tasks' %}
  {% else %}
    {{ job[k] }}
  {% end %}
  </div>
</div>
{% end %}

{% if (current_user) %}
<div class="action">
    {% if job['status'] in states.job_prev_statuses(states.JOB_STATUS_START) %}
    <button onclick='job_reset()'>Reset</button>
    {% end %}
    <button onclick='job_hard_reset()'>Hard Reset</button>
    {% if job['status'] in states.job_prev_statuses('suspended') %}
    <button onclick='job_suspend()'>Suspend</button>
    {% end %}
</div>
{% end %}

</div>

<div class="task_completion">
    <h2>Tasks</h2>
    <table>
        <tr>
            <th>Name</th>
            <th>Type</th>
            <th>Status</th>
            <th>Failures</th>
            <th>Walltime</th>
            <th>Walltime Err</th>
        </tr>
        {% for task in job['tasks'] %}
        <tr>
            <td><a href="/dataset/{{ url_escape(job['dataset_id']) }}/task/{{ url_escape(task['task_id']) }}">{{ task['name'] }}</a></td>
            <td>{{ 'GPU' if 'gpu' in task['requirements'] and task['requirements']['gpu'] else 'CPU' }}</td>
            <td>{{ task['status'] }}</td>
            <td>{{ task['failures'] }}</td>
            <td>{{ '{:.2f}'.format(task['walltime']/3600) }}</td>
            <td>{{ '{:.2f}'.format(task['walltime_err']/3600) }}</td>
        </tr>
        {% 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>
<script>
var rest_api = '{{ rest_api }}';
const passkey = "{{ passkey }}";
const dataset_id = "{{ job['dataset_id'] if job else '' }}";
const job_id = "{{ job['job_id'] if job else '' }}";

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function service_worker(action) {
    try {
        const url = '/actions/job_status';
        const args = {
            dataset_id: dataset_id,
            action: action,
            job_ids: [job_id],
        };
        const ret = await fetch_json('POST', url, args, passkey);
        if ('error' in ret) {
            message_alert('error - '+ret['error']);
            return false;
        }
        const id = ret['result'];

        while (true) {
            await sleep(1000);
            const url = '/actions/job_status/'+id;
            const ret = await fetch_json('GET', url, null, passkey);
            if (ret['status'] == 'error') {
                if ('error_message' in ret) {
                    message_alert('error - '+ret['error_message']);
                } else {
                    message_alert('error - '+action+' failed');
                }
                return false;
            } else if (ret['status'] == 'complete') {
                message('complete');
                break;
            } else if ('payload' in ret && 'progress' in ret['payload']) {
                message('progress - '+ret['payload']['progress']+'%')
            }
        }
    } catch(err) {
        message_alert('error - '+err);
        return false;
    }
    reload();
    return true;
}

async function job_reset() {
    return await service_worker('reset')
}

async function job_hard_reset() {
    return await service_worker('hard_reset')
}

async function job_suspend() {
    return await service_worker('suspend')
}
</script>
{% end %}

{% end %}
