


{% extends "base.html" %}


{% block postbody %}
    <script>
        window.onload = pageInit
    </script>
{% endblock  %}



{% block additional_header %}
<script type="text/javascript">




// Dajax callback functions:
function pass(data){
}


function pageInit() {
    countdown_callback()
    refresh_simfile_status_all();
}












// These handler are the callback functions for our django-ajax.
// They update the classes and contents of the table:
function update_table_entry(status_info)
{
    var id = status_info.sim_id;
    var state = status_info.state
    var is_in_queue = status_info.is_queued

    // set_sim_file_status(id, state, queued)
    var is_in_queue_str = "SimNotQueued"
    if(is_in_queue == true){
        is_in_queue_str = "SimQueued"
    }


    // Set the class, for coloring:
    var simfile_id_tag = 'SimFileID'+id
    var simfile_id_obj = document.getElementById('SimFileID'+id)

    document.getElementById('SimFileID'+id).className = state + " " + is_in_queue_str;
    document.getElementById( "SimFileTDStatus"+id ).innerHTML = state

    var textTD = document.getElementById('SimFileID'+id).getElementsByTagName('a')[0];
    if( is_in_queue ) {
        textTD.innerHTML = 'Cancel';
    }
    else {
        textTD.innerHTML = 'Run';
    }


    // Update the Last Run Info and hrefs:
    var node = document.getElementById("SimFileLatestRun"+id)
    var day = moment(status_info.latest_exec_date, "YYYY-MM-DD HH:mm");
    node.innerHTML =  status_info.latest_exec_date
    if( day ){
        node.innerHTML =  day.fromNow();
    }

    var node = document.getElementById("SimFileLatestRunTime"+id)
    var duration = moment.duration(status_info.latest_exec_duration, 'seconds');
    if( duration ){
        node.innerHTML =  duration.humanize()
    }
    node.href =  "simfileruns/" + status_info.latest_exec_id
}


function update_sim_gui_callback_batch(results)
{
    for(var data_index in results){
         if (results.hasOwnProperty(data_index)) {
            var data = results[data_index]
            update_table_entry(data)
         }
    }
}

// Functions that call ajax on the server:
function refresh_simfile_status_all() {
    ids = '{% for sim_file in simfiles %} {{sim_file.id}} {% endfor %} '
    Dajaxice.mreorg.curator.frontend.overview_update_sim_gui_batch(update_sim_gui_callback_batch, {'simfile_ids':ids})
    refresh_time_until_next = refresh_interval
}
function refresh_simfile_status_single(sim_id) {
    Dajaxice.mreorg.curator.frontend.overview_update_sim_gui_batch(update_sim_gui_callback_batch, {'simfile_ids':sim_id});
}



// Called when the simulation is clicked:
function toggle_sim_for_resubmit(id) {
    Dajaxice.mreorg.curator.frontend.overview_toggle_simfile_for_resubmit(pass, {'simfile_id':id});
    refresh_simfile_status_single(id);
}




// New functions:
function update_queue(action) {
    Dajaxice.mreorg.curator.frontend.update_queue(refresh_simfile_status_all, {'action':action});
}

function queue_clear_all() {
    update_queue('clear-all')
}
function queue_resubmit_all_sims() {
    update_queue('add-all')
}
function queue_resubmit_all_failures() {
    update_queue('add-all-failures')
}
function queue_resubmit_all_failures_not_timeout() {
    update_queue('add-all-failures-not-timeout')
}
function queue_resubmit_all_changed() {
    update_queue('add-all-changed')
}









function reloadPage() {
    window.location="/";
}

function deleteSimFile(id) {
    Dajaxice.mreorg.curator.frontend.overview_delete_simfile(reloadPage, {'simfile_id': id} );
}


var refresh_interval = 60;
var refresh_time_until_next = 0;
function countdown_callback(){
    document.getElementById( "refresh_span" ).innerHTML = "Updating in "
    if(refresh_time_until_next ==0){
        refresh_time_until_next = refresh_interval;
        refresh_simfile_status_all();
    }
    else{
        refresh_time_until_next = refresh_time_until_next -1;
        document.getElementById( "refresh_span" ).innerHTML = "Refresh("+ refresh_time_until_next +")";
    }
    setTimeout("countdown_callback()", 1000);
}




</script>
{% endblock %}









{% block contents %}
<div class='section' >
      <div class='sectionheader'>
          <h2><a>Overview</a></h2>
          <div class='actionbar'>
              <div class='inner'>
                  <ul>
                      <li> <a onclick="queue_resubmit_all_sims()"><span>Submit-All<span></span></a></li>
                      <li> <a onclick="queue_resubmit_all_failures()"><span>Submit-Failures<span></span></a></li>
                      <li> <a onclick="queue_resubmit_all_failures_not_timeout()"><span>Submit-Failures (Not Timeout)<span></span></a></li>
                      <li> <a onclick="queue_resubmit_all_changed()"><span>Submit-Changed<span></span></a></li>
                      <li> <a onclick="queue_clear_all();" ><span>Clear-Queue</span></a></li>
                      <li> <a onclick="refresh_simfile_status_all()" ><span id='refresh_span'>Refresh</span></a></li>
                  </ul>
              </div>
          </div>
      </div>

      <div class='sectioncontents'>


    <table class='std'>
        <tbody>

        <tr>
          <th width="5%"> Queue</th>
          <th width="10%"> Status </th>
          <th width="15%"> File </th>
          <th width="15%"> Last Run </th>
          <th width="10%"> Sim Time </th>
          <th width="30%"> DocString </th>
        </tr>


        {% for sim_file in simfiles %}

        {% if sim_file.does_file_exist %}
        <tr class="" id="SimFileID{{sim_file.id}}">
            <td class="Toggle interactive"> <a onclick="toggle_sim_for_resubmit({{sim_file.id}})" > -- </a>  </td>
            <td class="Status" id="SimFileTDStatus{{sim_file.id}}"> {{ sim_file.get_status }} </td>
            <td> <a href="/simfiles/{{sim_file.id}}"> {{sim_file.get_short_filename }} </a> </td>
            <td> <a id="SimFileLatestRun{{sim_file.id}}" href="/simfileruns/{{sim_file.get_last_run.id}}">{{sim_file.get_last_run.execution_date|timesince}} A </a></td>
            <td> <a id="SimFileLatestRunTime{{sim_file.id}}">{{sim_file.get_last_run.execution_time}} s</a></td>
            <td> {{sim_file.get_docstring}} </td>
        </tr>

        {% else %}
        <tr class="" id="SimFileID{{sim_file.id}}">
            <td class="Toggle"> <a onclick='deleteSimFile({{sim_file.id}});'> Delete </a>  </td>
            <td class="Status"> <a>  [File does not exist] </a> </td>
            <td> <a>  {{sim_file.get_short_filename }}  </a> </td>
            <td> <a> </a></td>
            <td> </td>
        </tr>
        {% endif %}


        {% endfor %}

        </tbody>
    </table>
</div>




{% endblock %}

