{% extends "cluster/base.html" %}
{% load bootstrap_toolkit %}
{% load humanize %}
{% load cluster_extras %}

{% block subtitle %}{{ node.hostname}} / Nodes / {% endblock %}

{% block content %}
<ul class="breadcrumb">
    <li>
        <a href="{% url cluster:nodes %}">Nodes</a> <span class="divider">/</span>
    </li>
    <li class="active">{{ node.hostname }}</li>
</ul>

<table class="table table-striped">
    <tbody>
        <tr>
            <th>Hostname</th>
            <td>{{ node.hostname }}
        </tr>
    </tbody>
</table>

{% if node.is_db %}
<h2>DB info</h2>

<table class="table table-striped">
    <thead>
        <tr>
            <th rowspan="2">Database</th>
            <th rowspan="2">Docs</th>
            <th colspan="2">Security</th>
            <th>Security check</th>
        </tr>
        <tr>
            <th>Admins</th>
            <th>Members</th>
        </tr>
    </thead>
    <tbody>
        {% for db, info, security, security_correct in db_info.databases %}
        <tr>
            <td>{{ db }}</td>
            <td>
                {% if info %}
                <span class="badge {% if info %}badge-success{% else %}badge-important{% endif %}">{{ info.doc_count|intcomma }}</span></td>
                {% else %}
                <form action="{% url cluster:node_actions node.id %}" method="post" onsubmit="return confirm('Are you sure?')">
                    <input type="hidden" name="cmd" value="db_create" />
                    <input type="hidden" name="db" value="{{ db }}"/>
                    {% csrf_token %}
                    <input type="submit" class="btn btn-warning btn-mini" value="Создать базу данных" />
                </form>
                {% endif %}
            <td>
                <ul>
                    {% for name in security.admins.names %}
                    <li>{{ name }}</li>
                    {% endfor %}
                    
                    {% for role in security.admins.roles %}
                    <li>@{{ role }}</li>
                    {% endfor %}
                </ul>
            </td>
            <td>
                <ul>
                    {% for name in security.members.names %}
                    <li>{{ name }}</li>
                    {% endfor %}
                    
                    {% for role in security.members.roles %}
                    <li>@{{ role }}</li>
                    {% endfor %}
                </ul>
            </td>
            <td>
                {% if security_correct %}
                <span class="badge badge-success">Ok</span>
                {% else %}
                {% if info %}
                <form action="{% url cluster:node_actions node.id %}" method="post" onsubmit="return confirm('Are you sure? It\'s danger!')">
                    <input type="hidden" name="cmd" value="security_fix" />
                    <input type="hidden" name="db" value="{{ db }}"/>
                    {% csrf_token %}
                    <input type="submit" class="btn btn-warning btn-mini" value="Исправить" />
                </form>
                {% endif %}
                {% endif %}
            </td>
        </tr>
        {% endfor %}
    </tbody>
</table>

<h2>Replications</h2>
<table class="table table-striped table-condensed">
    <thead>
        <tr>
            <th>Hostname</th>
            <th>Database</th>
            <th>Status</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        {% for perm in db_info.permutations %}
        <tr>
            <td>{{ perm.hostname }}</td>
            <td>{{ perm.db }}</td>
            <td><span class="badge {% if perm.status == 'triggered' %}badge-success{% else %}badge-important{% endif %}">{% if 'status' in perm %}{{ perm.status }}{% else %}not exists{% endif %}</span></td>
            <td>
                {% if perm.status != 'triggered' and db_info.replications_error != 'not_found' %}
                <form action="{% url cluster:node_actions node.id %}" method="post" onsubmit="return confirm('Репликация будет удалена и создана новая. Ога?">
                    <input type="hidden" name="cmd" value="replication_create" />
                    <input type="hidden" name="hostname" value="{{ perm.hostname }}" />
                    <input type="hidden" name="db" value="{{ perm.db }}"/>
                    {% csrf_token %}
                    <input type="submit" class="btn btn-warning btn-mini" value="Запустить" />
                </form>
                {% endif %}
            </td>
        </tr>
        {% endfor %}
    </tbody>
</table>

<h2>Raw replications</h2>
{% if db_info.replications_error == 'not_found' %}
<form action="{% url cluster:node_actions node.id %}" method="post">
    {% csrf_token %}
    <input type="hidden" name="cmd" value="replication_view_create" />
    <div class="form-actions">
        <input class="btn btn-success" type="submit" value="Создать статус-вьюшку"/>
    </div>
</form>

{% else %}
<table class="table table-striped table-condensed">
    <thead>
        <tr>
            <th>Source</th>
            <th>Target</th>
            <th>Status</th>
        </tr>
    </thead>
    <tbody>
        {% for replication in db_info.replications %}
        <tr>
            <td>{{ replication.doc.source|hide_url_password }}</td>
            <td>{{ replication.doc.target|hide_url_password }}</td>
            <td><span class="badge {% if replication.key == "triggered" %}badge-success{% else %}badge-important{% endif %}">{{ replication.key }}</span></td>
        </tr>
        {% endfor %}
    </tbody>
</table>
{% endif %}


<h2>Tasks</h2>
<table class="table table-striped table-condensed">
    <thead>
        <tr>
            <th>Type</th>
            <th>Status</th>
            <th>Progress</th>
            <th>Started on</th>
            <th>Last update on</th>
        </tr>
    </thead>
    <tbody>
        {% for task in db_info.tasks %}
        <tr>
            <td>{{ task.type }}</td>
            <td>
                {% if task.type == 'replication' %}
                {{ task.source }} &rarr; {{ task.target }} ({{ task.checkpointed_source_seq|intcomma }}/{{ task.source_seq|intcomma }})
                {% endif %}
                
                {% if task.type == 'view_compaction' %}
                {{ task.design_document }}@{{ task.database }}
                {% endif %}
                
                {% if task.type == 'indexer' %}
                {{ task.design_document }}@{{ task.database }} ({{ task.changes_done|intcomma }}/{{ task.total_changes|intcomma }})
                {% endif %}
                
            </td>
            <td><span class="badge {% if task.progress == 100 %}badge-success{% endif %}">{{ task.progress }}%</span></td>
            <td>{{ task.started_on|unix|date:"Y-m-d H:i" }}</td>
            <td>{{ task.updated_on|unix|timesince }}</td>
        </tr>
        {% endfor %}
    </tbody>
</table>

{% endif %}

{% endblock %}
