{% extends "rq_dashboard/base.html" %}


{% block content %}
<div class="row" id="rq-instances-row">
    <div class="span12">
        <div class="section">
            <h1>RQ Instances</h1>
            <p class="intro">Select below the RQ instance that you want to observe.</p>
            <select id="rq-instances">
            </select>
        </div>
    </div>
</div>
<div class="row">
    <div class="span6">
        <div class="section">

            <h1>Queues</h1>
            <p class="fixed intro">This list below contains all the registered queues with the number of jobs currently
                in the queue. Select a queue from above to view all jobs currently pending on the queue.</p>

            <table id="queues" class="table table-bordered">
                <thead>
                <tr>
                    <th>Queue</th>
                    <th class="narrow">Jobs</th>
                </tr>
                </thead>
                <tbody>
                <tr data-role="loading-placeholder">
                    <td colspan="2">Loading...</td>
                </tr>
                </tbody>
            </table>

            <script name="queue-row" type="text/template">
                <tr data-role="queue" <% if (d.name === 'failed' && d.count > 0) { %> class="failed"<% } %>>
                <td><i class="icon-inbox" style="opacity: .5;"></i> <a href="<%= d.url %>"><%= d.name %></a></td>
                <td class="narrow"><%= d.count %></td>
                </tr>
            </script>

            <script name="no-queues-row" type="text/template">
                <tr>
                    <td colspan="3">No queues.</td>
                </tr>
            </script>

        </div>
    </div>

    <div class="span6">
        <div class="section">

            <h1>Workers</h1>

            <button id="workers-btn" type="button" class="btn btn-info">Toggle workers list</button>
            <p id="workers-count" class="fixed intro">No workers registered!</p>

            <table id="workers" class="table table-bordered">
                <thead>
                <tr>
                    <th style="width:48px">State</th>
                    <th>Worker</th>
                    <th>Queues</th>
                </tr>
                </thead>
                <tbody>
                <tr data-role="loading-placeholder">
                    <td colspan="3">Loading...</td>
                </tr>
                </tbody>
            </table>

            <script name="worker-row" type="text/template">
                <tr data-role="worker">
                    <td><i class="icon-<%= d.state %>" title="Job ID: <%= d.current_job.job_id %>&#013;&#010;Description: <%= d.current_job.description %>&#013;&#010;Created at: <%= d.current_job.created_at %>&#013;&#010;"></i></td>
                    <td><%= d.name %></td>
                    <td><%= d.queues.join(', ') %></td>
                </tr>
            </script>

            <script name="no-workers-row" type="text/template">
                <tr>
                    <td colspan="3">No workers.</td>
                </tr>
            </script>

        </div>
    </div>
</div>

<div class="row">
    <div class="span12">
        <div class="section">

            <h1>Jobs on <strong{% if queue.name == 'failed' %} class="failed"{% endif %}>{{ queue.name }}</strong></h1>
            <p class="intro">
                <a href="{{ url_for('rq_dashboard.empty_queue', queue_name=queue.name) }}" id="empty-btn"
                   class="btn btn-danger btn-small" style="float: right" data-toggle="tooltip"
                   title="Remove all jobs from this queue (<b>destructive</b>)" data-html=true><i
                        class="icon-trash icon-white"></i> Empty</a>
                <a href="{{ url_for('rq_dashboard.compact_queue', queue_name=queue.name) }}" id="compact-btn"
                   class="btn btn-small" style="float: right; margin-right: 8px;" data-toggle="tooltip"
                   title="Remove all stale jobs from this queue (non-destructive)"><i class="icon-resize-small"></i>
                    Compact</a>
                <a href="{{ url_for('rq_dashboard.requeue_all') }}" id="requeue-all-btn" class="btn btn-small"
                   style="float: right; margin-right: 8px;"><i class="icon-retweet"></i> Requeue All</a>
                This list below contains all the registered jobs on queue <strong>{{ queue.name }}</strong>, sorted by
                age (oldest on top).</p>

            <table id="jobs" class="table table-bordered">
                <thead>
                <tr>
                    <th>Name</th>
                    <th>Age</th>
                    <th class="narrow">Actions</th>
                </tr>
                </thead>
                <tbody>
                <tr data-role="loading-placeholder">
                    <td colspan="2">Loading...</td>
                </tr>
                </tbody>
            </table>

            <script name="job-row" type="text/template">
                <tr data-role="job" data-job-id="<%= d.id %>">
                    <td>
                        <i class="icon-file" style="opacity: .5;"></i>
                        <span class="description"><%= $('<div/>').text(d.description).html() %></span>
                        <% if (d.exc_info) { %>
                        <span class="origin">from <strong><%= d.origin %></strong></span>
                        <% } %>
                        <div class="job_id"><%= d.id %></div>
                        <% if (d.exc_info) { %>
                        <span class="end_date">Failed <%= d.ended_at %></span>
                        <pre class="exc_info"><%= $('<div/>').text(d.exc_info).html() %></pre>
                        <% } %>
                    </td>
                    <td><span class="creation_date"><%= d.created_at %></span></td>
                    <td class="actions narrow">
                        <% if (d.exc_info) { %>
                        <a href="#" data-role="requeue-job-btn" class="btn btn-small"><i class="icon-retweet"></i>
                            Requeue</a>
                        <% } %>
                        <a href="#" data-role="cancel-job-btn" class="btn btn-small"><i class="icon-remove"></i> Cancel</a>
                    </td>
                </tr>
            </script>

            <script name="no-jobs-row" type="text/template">
                <tr>
                    <td colspan="3">No jobs.</td>
                </tr>
            </script>

            <div id="page-selection" class="pagination pagination-centered">
                <ul>
                </ul>
            </div>

            <script name="first-page-link" type="text/template">
                <li><a href="<%= url %>">&laquo;</a></li>
            </script>

            <script name="no-previous-page-link" type="text/template">
                <li class="disabled"><a href="#">&lsaquo;</a></li>
            </script>

            <script name="previous-page-link" type="text/template">
                <li><a href="<%= url %>">&lsaquo;</a></li>
            </script>

            <script name="page-link" type="text/template">
                <li><a href="<%= url %>"><%= number %></a></li>
            </script>

            <script name="next-page-link" type="text/template">
                <li><a href="<%= url %>">&rsaquo;</a></li>
            </script>

            <script name="no-next-page-link" type="text/template">
                <li class="disabled"><a href="#">&rsaquo;</a></li>
            </script>

            <script name="last-page-link" type="text/template">
                <li><a href="<%= url %>">&raquo;</a></li>
            </script>

        </div>
    </div>
</div>
<div class="row">
    <div class="span12">
        <div class="section">
          {%- set url = request.args.get("url") -%}
          <p class="intro"><a href="{{ url or '/' }}" id="home-btn" class="btn btn-small" style="float: left"><i
              class="icon-arrow-left"></i> {{'Back' if url else 'Home'}}</a></p>
        </div>
    </div>
</div>


{% endblock %}


{% block inline_js %}
var POLL_INTERVAL = {{ poll_interval }};
{% include "rq_dashboard/dashboard.js" with context %}
{% endblock %}
