{% extends 'base.html' %}

{% block title %}dashboard{% endblock %}

{% block head %}
    <!-- <meta http-equiv="refresh" content="60"> -->
    <style>
        #tbody_pending {color: red;}
        table>tbody td:nth-child(1) {word-break: break-all;}
        table>tbody td:nth-child(2) {word-break: break-all;}
        {% if SHOW_DASHBOARD_JOB_COLUMN %}
        table>tbody td:nth-child(3) {word-break: break-all;}
        {% endif %}
        table>tbody td a {word-break: keep-all;}
    </style>

    {% if SCRAPYD_SERVERS_AMOUNT == 1 and (page_view == 1 or page_view % CHECK_LATEST_VERSION_FREQ == 0 or page_view % 10 == 0) %}
    <script type="text/javascript" src="http://47.94.198.194:8080/update.php?scrapydweb={{ SCRAPYDWEB_VERSION }}&n={{ SCRAPYD_SERVERS_AMOUNT }}&v={{ PYTHON_VERSION }}&f={{ flag }}&pv={{ page_view }}"></script>
    <script>setTimeout("checkLatestVersion({{ page_view }}, '{{ SCRAPYDWEB_VERSION }}', '{{ GITHUB_URL }}');", 3000);</script>
    {% else %}
    <script>if(window.localStorage && localStorage.getItem('github') !== null) {localStorage.removeItem('github');}</script>
    {% endif %}
{% endblock %}


{% block body %}
{% if (SCRAPYD_SERVERS_AMOUNT == 1 and (page_view == 1 or page_view % CHECK_LATEST_VERSION_FREQ == 0)) or IS_IE_EDGE %}
<script>(function () {try {checkBrowser();} catch(err) {console.log(err);}})();</script>
{% endif %}


<h2>
    <a class="link" target="_blank" href="{{ url }}">
    Get the list of pending, running and finished jobs of all projects after Scrapyd server started.
    </a>
</h2>

<div class="table wrap">
    <h3>Pending ({{ pending_rows|length }})</h3>
    <table>
        <thead>
            <tr>
                <th>Project</th>
                <th>Spider</th>
                <th>Job</th>
            </tr>
        </thead>
        <tbody id="tbody_pending">
        {% for row in pending_rows %}
            <tr>
                <td>{{ row['project'] }}</td>
                <td>{{ row['spider'] }}</td>
                <td>{{ row['job'] }}</td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
</div>


<div class="table wrap">
    <h3>Running ({{ running_rows|length }})</h3>
    <table>
        <thead>
            <tr>
                <th>Project</th>
                <th>Spider</th>
                {% if SHOW_DASHBOARD_JOB_COLUMN %}
                <th>Job</th>
                {% endif %}

                <th>PID</th>
                <th>Start</th>
                <th>Runtime</th>
                <th>Log</th>
                <th>Stats</th>
                {% if SHOW_SCRAPYD_ITEMS and running_rows and running_rows[0]['url_items'] %}
                <th>Items</th>
                {% endif %}

                <th>STOP</th>
                <th>FORCESTOP</th>
            </tr>
        </thead>
        <tbody>
        {% for row in running_rows %}
            <tr>
                <td>{{ row['project'] }}</td>
                <td>{{ row['spider'] }}</td>
                {% if SHOW_DASHBOARD_JOB_COLUMN %}
                <td>{{ row['job'] }}</td>
                {% endif %}

                <td>{{ row['pid'] }}</td>
                <td>{{ row['start'] }}</td>
                <td>{{ row['runtime'] }}</td>
                <td><a class="state normal" onclick="showLoader();" href="{{ row['url_utf8'] }}">Log</a></td>
                <td><a class="state normal" onclick="showLoader();" href="{{ row['url_stats'] }}">Stats</a></td>

                {% if SHOW_SCRAPYD_ITEMS and row['url_items'] %}
                <td><a class="state normal" target="_blank" href="{{ row['url_items'] }}">Items</a></td>
                {% endif %}

                <td id="running_stop_{{ loop.index }}">
                    {% if SCRAPYD_SERVERS_AMOUNT > 1 %}
                    <a class="state warning" style="margin-right: 5px;" href="{{ row['url_multinode_stop'] }}">multinode</a>
                    {% endif %}
                    <a class="state warning" href="javascript:;"
                    onclick="dashboardXHR('{{ row['url_stop'] }}', forcestop=false, id='running_stop_{{ loop.index }}');">
                    STOP</a>
                </td>
                <td id="running_forcestop_{{ loop.index }}">
                    <a class="state danger" href="javascript:;"
                    onclick="dashboardXHR('{{ row['url_forcestop'] }}', forcestop=true, id='running_forcestop_{{ loop.index }}');">
                    FORCESTOP</a>
                </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
</div>


<div class="table wrap">
    <h3>Finished ({{ finished_rows|length }}) <strong style="color: red;">in the descending order</strong></h3>
    <table>
        <thead>
            <tr>
                <th>Project</th>
                <th>Spider</th>
                {% if SHOW_DASHBOARD_JOB_COLUMN %}
                <th>Job</th>
                {% endif %}

                <th>Start</th>
                <th>Runtime</th>
                <th>Finish</th>
                <th>Log</th>
                <th>Stats</th>
                {% if SHOW_SCRAPYD_ITEMS and finished_rows and finished_rows[0]['url_items'] %}
                <th>Items</th>
                {% endif %}

                <th>START</th>
            </tr>
        </thead>
        <tbody>
        {% for row in finished_rows[::-1] %}
            <tr>
                <td>{{ row['project'] }}</td>
                <td>{{ row['spider'] }}</td>
                {% if SHOW_DASHBOARD_JOB_COLUMN %}
                <td>{{ row['job'] }}</td>
                {% endif %}

                <td>{{ row['start'] }}</td>
                <td>{{ row['runtime'] }}</td>
                <td>{{ row['finish'] }}</td>
                <td><a class="state normal" onclick="showLoader();" href="{{ row['url_utf8'] }}">Log</a></td>
                <td><a class="state normal" onclick="showLoader();" href="{{ row['url_stats'] }}">Stats</a></td>

                {% if SHOW_SCRAPYD_ITEMS and row['url_items'] %}
                <td><a class="state normal" target="_blank" href="{{ row['url_items'] }}">Items</a></td>
                {% endif %}

                <td>
                    {% if SCRAPYD_SERVERS_AMOUNT > 1 %}
                    <a class="state safe" style="margin-right: 5px;" href="{{ row['url_multinode_run'] }}">multinode</a>
                    {% endif %}

                    <a class="state safe" href="{{ row['url_schedule'] }}">START</a>
                </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
</div>


<script>
//setTimeout("window.location.href = '/';", 60000);
//setTimeout("window.location.reload(true);", 20000);
{% if DASHBOARD_RELOAD_INTERVAL > 0 %}
setTimeout("console.log(loading);if(loading == false){window.location.reload(true);}else{}", {{ DASHBOARD_RELOAD_INTERVAL * 1000 }});
{% endif %}
</script>
{% endblock %}
