{% extends 'base.html' %}

{% block title %}index{% endblock %}

{% block head %}
    <!-- <meta http-equiv="refresh" content="60"> -->
{% endblock %}

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


<div class="table wrap">
    <h3>Pending</h3>
    <table>
        <thead>
            <tr>
                <th>Project</th>
                <th>Spider</th>
                <th>Job</th>
            </tr>
        </thead>
        <tbody>
        {% 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</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>UTF8</th>
                <th>Stats</th>
                {% if (not HIDE_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="{{ url_for('log.log', node=node, opt='utf8', project=row['project'], spider=row['spider'], job=row['job']) }}">
                    UTF8
                    </a>
                </td>
                <td>
                    <a class="state normal" onclick="showLoader();"
                    href="{{ url_for('log.log', node=node, opt='stats', project=row['project'], spider=row['spider'], job=row['job']) }}">
                    Stats
                    </a>
                </td>

            {% if (not HIDE_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|length > 1 %}
                    <a class="state warning" style="margin-right: 5px;"
                    href="{{ url_for('overview.overview', node=node, opt='stop', project=row['project'], version_job=row['job']) }}">
                    multinode
                    </a>
                    {% endif %}

                    <a class="state warning" href="javascript:;"
                    onclick="stopXHR('{{ url_for('api.api', node=node, opt='stop', project=row['project'], version_spider_job=row['job']) }}');">
                    STOP
                    </a>
                </td>
                <td>
                    <a class="state danger" href="javascript:;"
                    onclick="stopXHR('{{ url_for('api.api', node=node, opt='forcestop', project=row['project'], version_spider_job=row['job']) }}', forcestop=1);">
                    FORCESTOP
                    </a>
                </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
</div>


<div class="table wrap">
    <h3>Finished (latest to oldest)</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>UTF8</th>
                <th>Stats</th>
                {% if (not HIDE_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="{{ url_for('log.log', node=node, opt='utf8', project=row['project'], spider=row['spider'], job=row['job']) }}">
                    UTF8
                    </a>
                </td>
                <td>
                    <a class="state normal" onclick="showLoader();"
                    href="{{ url_for('log.log', node=node, opt='stats', project=row['project'], spider=row['spider'], job=row['job']) }}">
                    Stats
                    </a>
                </td>

            {% if (not HIDE_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|length > 1 %}
                    <a class="state safe" style="margin-right: 5px;"
                    href="{{ url_for('overview.overview', node=node, opt='schedule', project=row['project'], version_job=DEFAULT_LATEST_VERSION, spider=row['spider']) }}">
                    multinode
                    </a>
                    {% endif %}

                    <a class="state safe"
                    href="{{ url_for('schedule.schedule', node=node, project=row['project'], version=DEFAULT_LATEST_VERSION, spider=row['spider']) }}">
                    START
                    </a>
                </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
</div>


<script>
//setTimeout("window.location.href = '/';", 60000);
//setTimeout("window.location.reload(true);", 20000);
setTimeout("console.log(loading);if(loading == false){window.location.reload(true);}else{}", 300000);

function stopXHR(url, forcestop) {
    if (forcestop !== undefined) {
        var r = confirm("Force unclean shutdown?");
        if(r == false) {
            return;
        }
    }

    showLoader();
    var req = new XMLHttpRequest();
    req.onreadystatechange = function() {
        if (this.readyState == 4) {
            hideLoader();
            if (this.status == 200) {
                obj = JSON.parse(this.responseText);
                if (obj.status == 'ok') {
                    alert(this.responseText);
                    window.location.reload(true);
                } else {
                    //if (window.confirm(location.protocol+"//"+location.host+url+" got status: "+obj.status+", open a new tab for details?")) {
                        //var win = window.open(url, '_blank');
                        //win.focus();
                    //}
                    alert("REQUEST got status: "+obj.status);
                    var left = (screen.width-600)/2;
                    var top = (screen.height-800)/2;
                    window.open(url, "_blank", "left="+left+", top="+top+", width=600, height=800, toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=yes");
                }
            } else {
                alert("REQUEST got code: "+this.status);
                var left = (screen.width-600)/2;
                var top = (screen.height-800)/2;
                window.open(url, "_blank", "left="+left+", top="+top+", width=600, height=800, toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=yes");
            }
        }
    };
    req.open("get", url, Async = true);
    req.send();
}
</script>
{% endblock %}
