{% extends 'settings/layout.html' %}
{% load humanize %}
{% load i18n %}
{% load celery_tags %}

{% block title %}{% trans "Celery Status" %}{% endblock %}

{% block header %}{% trans "Celery Status" %}{% endblock %}

{% block panel %}

<div class="notification">
    <p>
    {% trans "You can set up monitoring to check if Celery is running by querying:" %}
    {% url "settings-celery-ping" as url %}
    <a href="{{ url }}" target="_blank" rel="nofollow noopener noreferrer">{{ url }}</a>
    </p>
</div>

{% if queues %}
<section class="block content">
    <h2>{% trans "Queues" %}</h2>
    <div class="columns has-text-centered is-multiline">
        <div class="column is-4">
            <div class="notification">
                <p class="header">{% trans "Streams" %}</p>
                <p class="title is-5">{{ queues.streams|intcomma }}</p>
            </div>
        </div>
        <div class="column is-4">
            <div class="notification">
                <p class="header">{% trans "Broadcast" %}</p>
                <p class="title is-5">{{ queues.broadcast|intcomma }}</p>
            </div>
        </div>
        <div class="column is-4">
            <div class="notification">
                <p class="header">{% trans "Inbox" %}</p>
                <p class="title is-5">{{ queues.inbox|intcomma }}</p>
            </div>
        </div>

        <div class="column is-4">
            <div class="notification">
                <p class="header">{% trans "Imports" %}</p>
                <p class="title is-5">{{ queues.imports|intcomma }}</p>
            </div>
        </div>
        <div class="column is-4">
            <div class="notification">
                <p class="header">{% trans "Import triggered" %}</p>
                <p class="title is-5">{{ queues.import_triggered|intcomma }}</p>
            </div>
        </div>
        <div class="column is-4">
            <div class="notification">
                <p class="header">{% trans "Connectors" %}</p>
                <p class="title is-5">{{ queues.connectors|intcomma }}</p>
            </div>
        </div>

        <div class="column is-6">
            <div class="notification">
                <p class="header">{% trans "Images" %}</p>
                <p class="title is-5">{{ queues.images|intcomma }}</p>
            </div>
        </div>
        <div class="column is-6">
            <div class="notification">
                <p class="header">{% trans "Suggested Users" %}</p>
                <p class="title is-5">{{ queues.suggested_users|intcomma }}</p>
            </div>
        </div>

        <div class="column is-4">
            <div class="notification">
                <p class="header">{% trans "Lists" %}</p>
                <p class="title is-5">{{ queues.lists|intcomma }}</p>
            </div>
        </div>
        <div class="column is-4">
            <div class="notification">
                <p class="header">{% trans "Email" %}</p>
                <p class="title is-5">{{ queues.email|intcomma }}</p>
            </div>
        </div>
        <div class="column is-4">
            <div class="notification">
                <p class="header">{% trans "Misc" %}</p>
                <p class="title is-5">{{ queues.misc|intcomma }}</p>
            </div>
        </div>

        <div class="column is-4">
            <div class="notification">
                <p class="header">{% trans "Low priority" %}</p>
                <p class="title is-5">{{ queues.low_priority|intcomma }}</p>
            </div>
        </div>
        <div class="column is-4">
            <div class="notification">
                <p class="header">{% trans "Medium priority" %}</p>
                <p class="title is-5">{{ queues.medium_priority|intcomma }}</p>
            </div>
        </div>
        <div class="column is-4">
            <div class="notification">
                <p class="header">{% trans "High priority" %}</p>
                <p class="title is-5">{{ queues.high_priority|intcomma }}</p>
            </div>
        </div>
    </div>
</section>
{% else %}
<div class="notification is-danger is-flex is-align-items-start">
    <span class="icon icon-warning is-size-4 pr-3" aria-hidden="true"></span>
    <span>
        {% trans "Could not connect to Redis broker" %}
    </span>
</div>

{% endif %}

{% if stats %}
<section class="block content">
    <h2>{% trans "Active Tasks" %}</h2>
    {% for worker in active_tasks.values %}
        <div class="table-container">
            <table class="table is-striped is-fullwidth">
                <tr>
                    <th>{% trans "ID" %}</th>
                    <th>{% trans "Task name" %}</th>
                    <th>{% trans "Run time" %}</th>
                    <th>{% trans "Priority" %}</th>
                </tr>
                {% if not worker %}
                <tr>
                    <td colspan="4">
                        <em>{% trans "No active tasks" %}</em>
                    </td>
                </tr>
                {% endif %}
                {% for task in worker %}
                <tr>
                    <td>{{ task.id }}</td>
                    <td>{{ task.name|shortname }}</td>
                    <td>{{ task.time_start|runtime }}</td>
                    <td>{{ task.delivery_info.routing_key }}</td>
                </tr>
                {% endfor %}
            </table>
        </div>
    {% endfor %}
</section>

<section class="block content">
    <h2>{% trans "Workers" %}</h2>

    {% for worker_name, worker in stats.items %}
    <div class="notification">
        <h3>{{ worker_name }}</h3>
        {% trans "Uptime:" %} {{ worker.uptime|uptime }}
    </div>
    {% endfor %}
</section>

{% else %}

<div class="notification is-danger is-flex is-align-items-start">
    <span class="icon icon-warning is-size-4 pr-3" aria-hidden="true"></span>
    <span>
        {% trans "Could not connect to Celery" %}
    </span>
</div>
{% endif %}

<section class="block">
    <div class="content"><h2>{% trans "Clear Queues" %}</h2></div>

    <div class="content notification is-warning is-flex is-align-items-start">
        <span class="icon icon-warning is-size-4 pr-3" aria-hidden="true"></span>
        {% trans "Clearing queues can cause serious problems including data loss! Only play with this if you really know what you're doing. You must shut down the Celery worker before you do this." %}
    </div>

    <form action="{% url 'settings-celery' %}" method="post">
        {% csrf_token %}

        <div class="columns">
            <div class="column is-3">
                <div class="content"><h3>{{ form.queues.label_tag }}</h3></div>
                {{ form.queues }}
            </div>

            <div class="column is-9">
                <div class="content"><h3>{{ form.tasks.label_tag }}</h3></div>
                {{ form.tasks }}
            </div>
        </div>

        <div class="buttons is-right">
            <button type="submit" class="button is-danger">{% trans "Clear Queues" %}</button>
        </div>
    </form>
</section>

{% if errors %}
<div class="block content">
    <h2>{% trans "Errors" %}</h2>
    {% for error in errors %}
    <pre>{{ error }}</pre>
{% endfor %}

</div>
{% endif %}

{% endblock %}
