{% extends "base.html" %}
{% block title %}Reduction job #{{ run.title|default:"null" }}{% endblock %}
{% load view %}
{% load colour_table_rows %}
{% load natural_time_difference %}
{% load replace %}
{% load naturaltime from humanize %}
{% load static %}

{% block body %}
    {% comment %} only pull in plotly if there are interactive plots {% endcomment %}
    {% if interactive_plots %}
    <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
    {% endif %}
    {% if not run %}
        <div class="text-center col-md-6 col-md-offset-3 well well-small">
            Reduction run not found or error encountered: {{ message }}.
        </div>
    {% else %}
        <title>Reduction Job #{{ run.title }}</title>
        <div class="row">
            <div class="col-md-12 text-center">
                <h2>Reduction Job #{{ run.title }}</h2>
                {% if is_rerun %}
                    <p>
                        (This run has been re-reduced,
                        <a href="#" class="js-reduction-run-history" id="run_history">see history</a>)
                    </p>
                {% endif %}
            </div>
        </div>
        {% if plot_error_message %}
            <div class="row">
                <div class="col-md-8 col-md-offset-2">
                    <p class="panel panel-warning text-center">{{ plot_error_message }}</p>
                </div>
            </div>
        {% else %}
            {% if static_plots %}
            <div class="row plot-container">
                {% for plot_file in static_plots %}
                    {% if static_plots|length == 1 %}
                        <img class="center-block" src="{{ plot_file }}" alt="Plot image stored at {{ plot_file }}">
                    {% else %}
                        <img class="col-md-6" src="{{ plot_file }}" alt="Plot image stored at {{ plot_file }}">
                    {% endif %}
                {% endfor %}
            </div>
            {% endif %}
            {% if interactive_plots %}
             <div class="row plot-container">
                {% for name, data in interactive_plots.items %}
                    <div id="{{ name }}"></div>
                    <script>
                        var data = JSON.parse('{{ data|escapejs }}');
                        var elem = document.getElementById("{{ name }}");
                        Plotly.newPlot(elem, data);
                    </script>
                {% endfor %}
            </div>
            {% endif %}
        {% endif %}
        {% if run.message %}
            {% if 'Skipped' in run.message %}
                <div class="alert alert-{% replace run.status.value_verbose 'Skipped' 'info' %} word-wrap" role="alert">
                    <i class="fa fa-{% replace run.status.value_verbose 'Skipped' 'question' %} fa-{% replace run.status.value_verbose 'Skipped' 'question' %}-circle fa-lg"></i>
                    <a href="#" class="js-log-display"> {{ run.message }}</a>
                </div>
            {% else %}
                <div class="alert alert-{% replace run.status.value_verbose 'Error' 'danger' %} word-wrap" role="alert">
                    <i class="fa fa-{% replace run.status.value_verbose 'Error' 'exclamation' %} fa-{% replace run.status.value_verbose 'Error' 'exclamation' %}-circle fa-lg"></i>
                    <a href="#" class="js-log-display">[{{ run.finished }}] {{ run.message }}</a>
                </div>
            {% endif %}
        {% endif %}
        <div class="row" id="reduction_job_panel">
            <div class="col-md-8 col-md-offset-2">
                <div class="panel panel-primary panel-small panel-2-column">
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-md-6">
                                <div id="run_description">
                                {% if run.run_description %}
                                    <strong>Run description: </strong> {{ run.run_description }}
                                    {% endif %}
                                </div>
                                <div id="started_by">
                                {% if started_by is not null %}
                                    <strong>Started by:</strong> {{ started_by }}
                                    {% endif %}
                                </div>
                                <div id="status">
                                    <strong>Status:</strong> <strong
                                        class="text-{% colour_table_row run.status.value_verbose %}">{{ run.status.value_verbose }}</strong>
                                </div>
                                <div id="instrument">
                                    <strong>Instrument:</strong> <a
                                        href="{% url 'runs:list' instrument=run.instrument.name %}">{{ run.instrument.name }}</a>
                                </div>
                                <div id="rb_number">
                                    <strong>RB Number:</strong> <a
                                        href="{% url 'experiment_summary' reference_number=run.experiment.reference_number %}">{{ run.experiment.reference_number }}</a>
                                </div>
                                <div id="last_updated">
                                    <strong>Last Updated:</strong> {{ run.last_updated }}
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div>
                                    <strong>Start:</strong>
                                    {% if run.started %}
                                        {{ run.started }}
                                    {% elif is_skipped %}
                                        <em>Not run</em>
                                    {% else %}
                                        <em>Not yet started</em>
                                    {% endif %}
                                </div>
                                <div>
                                    <strong>Finish:</strong>
                                    {% if is_skipped %}
                                        <em>Not run</em>
                                    {% elif run.finished %}
                                        {{ run.finished }}
                                    {% else %}
                                        <em>Not yet finished</em>
                                    {% endif %}
                                </div>
                                <div>
                                    <strong>Duration:</strong>
                                    {% if run.started and run.finished %}
                                        {% natural_time_difference run.started run.finished %}
                                    {% elif is_skipped %}
                                        <em>Not run</em>
                                    {% else %}
                                        <em>Not yet finished</em>
                                    {% endif %}
                                </div>
                                <div>
                                    <strong>Retrying:</strong>
                                    {% if run.finished %}
                                        {% if run.retry_when %}
                                            {{ run.retry_when|naturaltime }}
                                            {% if run.retry_run %}
                                                <a href="{% url 'runs:summary' instrument_name=run.instrument.name run_number=run.retry_run.run_number run_version=run.retry_run.run_version %}">
                                                    ({{ run.retry_run.status.value_verbose }})</a>
                                            {% endif %}
                                        {% else %}
                                            Never
                                        {% endif %}
                                    {% else %}
                                        <em>Not yet finished</em>
                                    {% endif %}
                                </div>
                                <div>
                                    <strong>Software used:</strong>
                                    {% if run.software is not null %}
                                        {{ run.software.name }} - {{ run.software.version }}
                                    {% else %}
                                        <em>No software data found</em>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="file-path">
                                    <strong>Data:</strong>
                                    {% if run.data_location.all %}
                                        {% for location in run.data_location.all %}
                                            {{ location.file_path }}<br/>
                                        {% endfor %}
                                    {% else %}
                                        <em>No data found</em>
                                    {% endif %}
                                </div>
                                <div id="reduction_host"><strong>Host:</strong> {{ run.reduction_host }}</div>
                            </div>
                            <div class="col-md-6">
                                <div class="file-path">
                                    <strong>Reduced:</strong>
                                    {% if reduction_location %}
                                        <a href="{{ data_analysis_link_url }}" target="_blank">{{ reduction_location }}<i class="fa fa-external-link" aria-hidden="true"></i></a><br/>
                                    {% elif run.reduction_location.all %}
                                        {% for location in run.reduction_location.all %}
                                            {{ location.file_path }}<br/>
                                        {% endfor %}
                                    {% else %}
                                        <em>No reduced data found</em>
                                    {% endif %}
                                </div>
                                {% if not is_skipped %}
                                <div>
                                    <strong>Logs:</strong>
                                    <a href="#" class="js-log-display" id="show_reduction_logs">Show reduction logs</a>
                                </div>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        {% if not is_skipped %}
            <div id="re-run_and_graphs">
                {% if run.graph %}
                    <div class="panel panel-default" id="re-run_job">
                        <div class="panel-heading">
                            <div class="panel-title"><a data-toggle="collapse" href="#collapseGraphs"
                                                        data-target="#graphs"><i
                                    class="fa fa-chevron-down"></i> Images</a></div>
                        </div>
                        <div class="panel-body collapse in" id="graphs">
                            {% for graph in run.graph %}
                                <div class="text-center"><a href="{{ graph }}" target="_blank"
                                                            rel="noopener noreferrer"><img src="{{ graph }}"/></a>
                                </div>
                            {% endfor %}
                        </div>
                    </div>
            {% endif %}
            {% if has_run_variables and has_reduce_vars  %}
                {% autoescape on %}
                    {% view "instrument.views.variables.render_run_variables" instrument run_number run_version %}
                {% endautoescape %}
            {% elif not has_run_variables %}
                    <div>
                        <p class="text-center" id="warning_message">No variables found for this run.</p>
                    </div>
            {% elif not has_reduce_vars %}
                <div>
                    <p class="text-center" id="warning_message">The reduce_vars.py script is missing for this instrument. Please create it before being able to submit re-runs.</p>
                </div>
            {% endif %}
        </div>
        <div class="row col-md-12 my-2">
            <div class="text-center">
                <a href="{% url 'runs:list' run.instrument.name %}" class="btn btn-primary back" id="cancel">Back to {{ run.instrument.name }} runs</a>
            </div>
        </div>
        {% endif %}
        <div class="run-history modal fade" tabindex="-1" role="dialog" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span
                                class="sr-only">Close</span></button>
                        <h3 class="modal-title">Job History</h3>
                    </div>
                    <div class="modal-body">
                        <table class="table table-striped table-bordered">
                            <thead>
                                <tr>
                                    <th>Job Number</th>
                                    <th>Status</th>
                                    <th>Last Updated</th>
                                    <th>Submitted By</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for job in history %}
                                    <tr>
                                        <td>
                                            {% if job.run_version != run.run_version %}
                                                <a href="{% url 'runs:summary' instrument_name=run.instrument.name run_number=job.run_number run_version=job.run_version %}">{{ job.title }}</a>
                                            {% else %}
                                                {{ job.title }}
                                            {% endif %}
                                        </td>
                                        <td class="text-{% colour_table_row job.status.value_verbose %}">
                                            <strong>{{ job.status.value_verbose }}</strong></td>
                                        <td title="{{ job.last_updated|date:'SHORT_DATETIME_FORMAT' }}">{{ job.last_updated|naturaltime }}</td>
                                        <td>
                                            {% if started_by %}
                                                {{ started_by }}
                                            {% else %}
                                                Unknown
                                            {% endif %}
                                        </td>
                                    </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <div class="log-display modal fade" tabindex="-1" role="dialog" aria-hidden="true">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span
                                class="sr-only">Close</span></button>
                        <h4 class="modal-title">Reduction logs</h4>
                    </div>
                    <div class="modal-body">
                        <pre class="prettyprint js-log-display-container">## Reduction log ##<br/>{{ run.reduction_log }}<br/><br/><br/>## Reduction admin log ##<br/>{{ run.admin_log }}</pre>
                    </div>
                </div>
            </div>
        </div>
    {% endif %}
{% endblock %}



{% block stylesheets %}
    <link rel="stylesheet" href="{% static 'css/vendor/prettify.css' %}">
    <link rel="stylesheet" href="{% static 'css/vendor/bootstrap-tour.min.css' %}">
{% endblock %}
{% block scripts %}
    <script src="{% static 'javascript/run_summary.js' %}"></script>
    <script src="{% static 'javascript/vendor/prettify.js' %}"></script>
    <script src="{% static 'javascript/run_variables.js' %}"></script>
    <script src="{% static 'javascript/vendor/bootstrap-tour.min.js' %}"></script>

    <script src="{% static 'javascript/tours/run_summary_tour.js' %}"></script>
    <script src="{% static 'javascript/tours/navbar_tour.js' %}"></script>
    <script src="{% static 'javascript/create_tour.js' %}"></script>
{% endblock %}
