{% extends "django_dagster/base.html" %}
{% load i18n %}

{% block content_title %}<h1>{{ title }}</h1>{% endblock %}

{% block dagster_breadcrumbs %}
&rsaquo; <a href="{% url 'admin:django_dagster_dagsterrun_changelist' %}">{% trans 'Runs' %}</a>
&rsaquo; {{ run.runId }}
{% endblock %}

{% block dagster_content %}
<div class="submit-row">
    {% if can_cancel %}
    <form method="post" action="{% url 'admin:django_dagster_dagsterrun_cancel' run.runId %}" style="display:inline;">
        {% csrf_token %}
        <input type="submit" value="{% trans 'Cancel Run' %}" onclick="return confirm('{% trans 'Cancel this run?' %}')">
    </form>
    {% endif %}
    {% if can_reexecute %}
    <form method="post" action="{% url 'admin:django_dagster_dagsterrun_reexecute' run.runId %}" style="display:inline;">
        {% csrf_token %}
        <input type="submit" value="{% trans 'Re-execute' %}">
    </form>
    {% endif %}
    <form method="get" action="{% url 'admin:django_dagster_dagsterrun_changelist' %}" style="display:inline;">
        <input type="submit" value="{% trans 'All Runs' %}">
    </form>
    {% if dagster_run_ui_url %}
    <form method="get" action="{{ dagster_run_ui_url }}" target="_blank" style="display:inline;">
        <input type="submit" value="{% trans 'View in Dagster UI ↗' %}">
    </form>
    {% endif %}
</div>

<fieldset class="module aligned">
    <h2>{% trans 'Run Details' %}</h2>
    <div class="form-row">
        <div>
            <label>{% trans 'Run ID:' %}</label>
            <div class="readonly"><code>{{ run.runId }}</code>{% if dagster_run_ui_url %} <a href="{{ dagster_run_ui_url }}" target="_blank" rel="noopener" title="{% trans 'View in Dagster UI' %}" style="opacity:.5;text-decoration:none;font-size:11px;">&#x2197;</a>{% endif %}</div>
        </div>
    </div>
    <div class="form-row">
        <div>
            <label>{% trans 'Job:' %}</label>
            <div class="readonly">{{ run.jobName }}{% if dagster_job_ui_url %} <a href="{{ dagster_job_ui_url }}" target="_blank" rel="noopener" title="{% trans 'View job in Dagster UI' %}" style="opacity:.5;text-decoration:none;font-size:11px;">&#x2197;</a>{% endif %}</div>
        </div>
    </div>
    <div class="form-row">
        <div>
            <label>{% trans 'Status:' %}</label>
            <div class="readonly"><span class="status-badge status-{{ run.status }}">{{ run.status }}</span></div>
        </div>
    </div>
    <div class="form-row">
        <div>
            <label>{% trans 'Started:' %}</label>
            <div class="readonly">{{ run.startTime|default:"—" }}</div>
        </div>
    </div>
    <div class="form-row">
        <div>
            <label>{% trans 'Ended:' %}</label>
            <div class="readonly">{{ run.endTime|default:"—" }}</div>
        </div>
    </div>
    {% if run.stats %}
    <div class="form-row">
        <div>
            <label>{% trans 'Steps succeeded:' %}</label>
            <div class="readonly">{{ run.stats.stepsSucceeded|default:"0" }}</div>
        </div>
    </div>
    <div class="form-row">
        <div>
            <label>{% trans 'Steps failed:' %}</label>
            <div class="readonly">{{ run.stats.stepsFailed|default:"0" }}</div>
        </div>
    </div>
    <div class="form-row">
        <div>
            <label>{% trans 'Materializations:' %}</label>
            <div class="readonly">{{ run.stats.materializations|default:"0" }}</div>
        </div>
    </div>
    {% endif %}
    {% if run.tags %}
    <div class="form-row">
        <div>
            <label>{% trans 'Tags:' %}</label>
            <div class="readonly">
                {% for tag in run.tags %}
                <span style="display:inline-block;background:var(--darkened-bg);padding:2px 6px;margin:2px;border-radius:3px;font-size:12px;">{{ tag.key }}={{ tag.value }}</span>
                {% endfor %}
            </div>
        </div>
    </div>
    {% endif %}
    {% if run.runConfigYaml %}
    <div class="form-row">
        <div>
            <label>{% trans 'Run config:' %}</label>
            <div class="readonly"><pre class="dagster-config">{{ run.runConfigYaml }}</pre></div>
        </div>
    </div>
    {% endif %}
</fieldset>

{% if events %}
<div class="inline-related tabular" style="margin-bottom:20px;">
<fieldset class="module">
    <h2>{% trans 'Event Log' %}</h2>
    <div style="max-height:400px;overflow-y:auto;">
    <table>
        <thead>
            <tr>
                <th style="width:160px;">{% trans 'Timestamp' %}</th>
                <th style="width:70px;">{% trans 'Level' %}</th>
                <th style="width:120px;">{% trans 'Step' %}</th>
                <th style="width:160px;">{% trans 'Event' %}</th>
                <th>{% trans 'Message' %}</th>
            </tr>
        </thead>
        <tbody>
            {% for event in events %}
            <tr class="{% if event.level == 'ERROR' or event.level == 'CRITICAL' %}row-error{% endif %}">
                <td style="white-space:nowrap;font-size:12px;">{{ event.timestamp|default:"—" }}</td>
                <td><span class="event-level event-level-{{ event.level }}">{{ event.level }}</span></td>
                <td style="font-size:12px;">{{ event.stepKey|default:"—" }}</td>
                <td style="font-size:12px;">{{ event.event_type }}</td>
                <td style="font-size:12px;word-break:break-word;">{{ event.message }}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
    </div>
</fieldset>
</div>
{% endif %}

{% if related_runs %}
<div class="inline-related tabular">
<fieldset class="module">
    <h2>{% trans 'Recent Runs for' %} {{ run.jobName }}</h2>
    <table>
        <thead>
            <tr>
                <th>{% trans 'Run ID' %}</th>
                <th>{% trans 'Status' %}</th>
                <th>{% trans 'Started' %}</th>
                <th>{% trans 'Ended' %}</th>
                {% if dagster_url %}
                <th>{% trans 'Dagster UI' %}</th>
                {% endif %}
            </tr>
        </thead>
        <tbody>
            {% for r in related_runs %}
            <tr{% if r.runId == run.runId %} style="font-weight:bold;"{% endif %}>
                <td><a href="{% url 'admin:django_dagster_dagsterrun_change' r.runId %}">{{ r.runId }}</a></td>
                <td><span class="status-badge status-{{ r.status }}">{{ r.status }}</span></td>
                <td>{{ r.startTime|default:"—" }}</td>
                <td>{{ r.endTime|default:"—" }}</td>
                {% if dagster_url %}
                <td>{% if r.dagster_ui_url %}<a href="{{ r.dagster_ui_url }}" target="_blank" rel="noopener" title="{% trans 'View in Dagster UI' %}">&#x2197;</a>{% endif %}</td>
                {% endif %}
            </tr>
            {% endfor %}
        </tbody>
    </table>
</fieldset>
</div>
{% endif %}

{% endblock %}
