{% extends 'main_header.html' %}
{% block body %}

    <div class="page-title-bar">
        <!-- <h2>scheduler managed entries</h2> -->
        {% import 'block_enable_auto_refresh.html' as block_enable_refresh %}
        {{ block_enable_refresh.enable_auto_refresh() }}
    </div>

    <div class="container u-full-width">
        <table class="table-entries u-full-width context-menu">
            <thead>
            <tr class="table-entries-header">
                <th scope="col"><input type="checkbox" name="select_all" onclick="toggleAllCheckboxes(this)"/></th>
                <th scope="col">Trigger On/Alive</th>
                <th scope="col">Process Name</th>
                <th scope="col">Trigger Frequency</th>
                <th scope="col"></th> <!-- Trigger Now -->
                <th scope="col">Next Run In</th>
                <th scope="col">Next Time Period</th>
            </tr>
            </thead>
            <tbody>

            {%- for row in details.managed_entries -%}
                {%- set tr_class = '' -%}
                {%- if row.is_on != row.is_alive -%}
                    {%- set tr_class = 'state_inconsistent' -%}
                {%- elif not row.is_alive -%}
                    {%- set tr_class = 'state_inactive' -%}
                {%- endif -%}
                <tr class="{{ tr_class }}">
                    <td>
                        {% set value = "{ process_name: '" + row.process_name + "', timeperiod: 'NA' }" %}
                        <input type="checkbox" name="batch_processing" value="{{ value }}"/>
                    </td>
                    <td>{{ row.is_on }} / {{ row.is_alive }}</td>
                    <td>{{ row.process_name }}</td>
                    <td>
                        <form class="table-form" method="POST" action="/scheduler/managed/entry/interval/">
                            <input type="hidden" name="process_name" value="{{ row.process_name }}"/>
                            <input type="hidden" name="timeperiod" value="NA"/>
                            <input type="text" size="8" maxlength="32" name="interval" value="{{ row.trigger_frequency }}"/>
                            <button type="submit" title="Apply" class="action_button btn-icons btn-center">
                                <i class="fa fa-check"></i></button>
                        </form>
                    </td>
                    <td>
                        <form class="table-form" method="POST" action="/scheduler/managed/entry/trigger/">
                            <input type="hidden" name="process_name" value="{{ row.process_name }}"/>
                            <input type="hidden" name="timeperiod" value="NA"/>
                            <button type="submit" class="action_button btn-center" title="Trigger">
                                <i class="fa fa-bolt"></i><span>Trigger</span></button>
                        </form>
                    </td>
                    <td>{{ row.next_run_in }}</td>
                    <td>{{ row.next_timeperiod }}</td>
                </tr>
            {%- endfor -%}

            </tbody>
        </table>

        <table class="table-entries u-full-width">
            <thead>
            <tr class="table-entries-header">
                <th scope="col" width="15%">Process Name</th>
                <th scope="col">Units Of Work</th>
                <th scope="col">
                    <form class="table-form inline" method="GET" action="/scheduler/gc/flush/all/" onsubmit="refreshWithDelay()">
                        <span class="inline u-pull-right">
                            <button type="submit" class="action_button btn-center inline" title="Flush All">
                                <i class="fa fa-recycle"></i><span>Flush All</span></button>
                        </span>
                    </form>
                    <form class="table-form inline" method="GET" action="/scheduler/gc/refresh/" onsubmit="refreshWithDelay()">
                        <span class="inline u-pull-right">
                            <button type="submit" class="action_button btn-center inline" title="Refresh">
                                <i class="fa fa-refresh"></i><span>Refresh</span></button>
                        </span>
                    </form>
                </th>
            </tr>
            </thead>
            <tbody>

            <!-- nonEmptyQ flow is a hack for Jinja2 to overcome variable visibility scope problem -->
            {% set nonEmptyQ = [] %}
            {%- for process_name, q in details.reprocess_uows.items() -%}
                {%- if q.queue|length != 0 -%}
                    {%- if nonEmptyQ.append('A') -%}{%- endif -%}
                    <tr>
                        <td>{{ process_name }}</td>
                        <td>
                            {%- for priority_entry in q.queue -%}
                                {{ priority_entry.entry.timeperiod }}
                                {%- if not loop.last -%}
                                    {{ ", " }}
                                {%- endif -%}
                            {%- endfor -%}
                        </td>
                        <td>
                            <form class="table-form u-pull-right" method="GET" action="/scheduler/gc/flush/one/" onsubmit="refreshWithDelay()">
                                <input type="hidden" name="process_name" value="{{ process_name }}"/>
                                <button type="submit" class="action_button btn-center u-pull-right" title="Flush">
                                    <i class="fa fa-recycle"></i><span>Flush</span></button>
                            </form>
                        </td>
                    </tr>
                {%- endif -%}
            {%- endfor -%}

            {%- if nonEmptyQ|length == 0 -%}
                <tr>
                    <td>Empty</td>
                    <td>Empty</td>
                    <td>
                        <button type="submit" disabled class="action_button btn-center disabled u-pull-right" title="Flush">
                            <i class="fa fa-recycle"></i><span>Flush</span></button>
                    </td>
                </tr>
            {%- endif -%}

            </tbody>
        </table>

        <table class="table-entries u-full-width">
            <thead>
            <tr class="table-entries-header">
                <th scope="col">Build Version</th>
                <th scope="col">Uptime</th>
                <th scope="col">Resident Memory</th>
                <th scope="col">Virtual Memory</th>
                <th scope="col">% CPU Core Load</th>
                <th scope="col">Available Physical Memory</th>
                <th scope="col">Available Virtual Memory</th>
            </tr>
            </thead>
            <tbody>

            <tr>
                <td>{{ scheduler_version() }}</td>
                <td>{{ scheduler_uptime() }}</td>
                <td>{{ details.footprint.memory_rss }}</td>
                <td>{{ details.footprint.memory_vms }}</td>
                <td>{{ details.footprint.cpu_utilization }}</td>
                <td>{{ details.footprint.mem_virtual_free }}</td>
                <td>{{ details.footprint.mem_swap_free }}</td>
            </tr>

            </tbody>
        </table>

        <table class="table-entries u-full-width">
            <thead>
            <tr class="table-entries-header">
                <th scope="col">
                    <form class="table-form" method="GET"
                          onsubmit="openViewerWindow(`/scheduler/viewer/object/?action=scheduler/gc/log`, `GC Log`); return false;">
                        <button type="submit" class="action_button btn-center" title="GC">
                            <i class="fa fa-file-text-o"></i>
                            <span>GC Log</span>
                        </button>
                    </form>
                </th>
                <th scope="col">
                    <form class="table-form" method="GET"
                          onsubmit="openViewerWindow('/scheduler/viewer/object/?action=scheduler/mx/log', 'MX Log'); return false;">
                        <button type="submit" class="action_button btn-center" title="MX Log">
                            <i class="fa fa-file-text-o"></i>
                            <span>MX Log</span>
                        </button>
                    </form>
                </th>
                <th scope="col">
                    <form class="table-form" method="GET"
                          onsubmit="openViewerWindow('/scheduler/viewer/object/?action=scheduler/scheduler/log', 'Scheduler Log'); return false;">
                        <button type="submit" class="action_button btn-center" title="Scheduler Log">
                            <i class="fa fa-file-text-o"></i>
                            <span>Scheduler Log</span>
                        </button>
                    </form>
                </th>
            </tr>
            </thead>
        </table>
    </div>
    {% import 'block_context_menu.html' as context_menu %}
    {{ context_menu.trigger_menu(false) }}
{% endblock %}
