<!--

    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.

-->
{% extends "stats/base.html" %}

{% load humanize %}
{% load stats_extras %}

{% block title %}Topic | {{topic.name}}{% endblock %}

{% block breadcrumbs %}
<div class="breadcrumbs">
    <a href="{% url 'home' %}">Home</a>
    &rsaquo; <a href="{% url 'property' topic.namespace.property.name %}">{{ topic.namespace.property }}</a>
    &rsaquo; <a href="{% url 'namespace' topic.namespace.name %}">{{ topic.namespace.name }}</a>
    &rsaquo; {{ topic.short_name }}
</div>
{% endblock %}

{% block content %}

{% if topic.is_global %}
<div class="module filtered" id="changelist">
<div id="changelist-filter">
    <h2>Cluster</h2>
<ul>
   {% for cluster in clusters %}
      <li {% if selectedCluster == cluster.name %}class="selected"{% endif %}
          ><a href="{% url 'topic' topic.url_name %}?cluster={{cluster}}">{{cluster}}</a></li>
   {% endfor %}

   <li>
</ul>
</div>
</div>
{% endif %}


<h2>Stats</h2>

<table>
    <tr class="row1"><th>Average msg size</th><td>{{topic.averageMsgSize | file_size_value}}</td>
        <td>{{topic.averageMsgSize | file_size_unit}}</td></tr>
    <tr class="row2"><th>Rate in</th><td>{{topic.msgRateIn | intcomma }}</td><td>msg/s</td></tr>
    <tr class="row1"><th>Rate out</th><td>{{topic.msgRateOut | intcomma }}</td><td>msg/s</td></tr>

    <tr class="row2"><th>Throughput in</th><td>{{topic.msgThroughputIn | file_size_value }}</td>
        <td>{{topic.msgThroughputIn | file_size_unit }} / sec</td></tr>
    <tr class="row1"><th>Throughput out</th><td>{{topic.msgThroughputOut | file_size_value }}</td>
        <td>{{topic.msgThroughputOut | file_size_unit }} / sec</td></tr>

    <tr class="row2"><th>Pending add entries</th><td>{{topic.pendingAddEntriesCount | intcomma }}</td><td></td></tr>
    <tr class="row1"><th>Producer count</th><td>{{topic.producerCount | intcomma }}</td><td></td></tr>

    <tr class="row2"><th>Storage size</th><td>{{topic.storageSize | file_size_value}}</td>
        <td>{{topic.storageSize | file_size_unit}}</td></tr>
    <tr class="row1">
        <th>Broker</th>
        <td colspan="2">
            <a href="{% url 'broker' topic.broker.url %}">{{topic.broker | escape }}</a>
        </td>
    </tr>
</table>

<h2>Subscriptions</h2>

<table>
<thead>
    <tr>
        <th>Subscription</th>
        <th>Type</th>
        <th title="messages">Backlog</th>
        <th title="Msg/s">Rate out</th>
        <th title="Bytes/s">Throughput out</th>
        <th title="Msg/s">Rate expired</th>
        <th title="Msg/s">Unacked</th>
        <th title="Delete">Delete Subscription</th>
        <th title="Clear messages for subscription">Clear Backlog</th>
        <th title="Peek messages">Peek</th>
    </tr>
</thead>

<tbody>
{% for sub, consumers in subscriptions %}
    <tr class="{% cycle 'row1' 'row2' %}">
        {% if consumers %}
        <th><a data-toggle="#consumers-{{sub.id}}" href="#">{{sub.name}}</a></th>
        {% else %}
        <th>{{sub.name}}</th>
        {% endif %}
        <td>{{sub.get_subscriptionType_display}}</td>
        <td>{{sub.msgBacklog | intcomma }}</td>
        <td>{{sub.msgRateOut | intcomma}}</td>
        <td>{{sub.msgThroughputOut | intcomma}}</td>
        <td>{{sub.msgRateExpired | intcomma}}</td>
        <td>{{sub.unackedMessages | intcomma}}</td>
        {% if consumers %}
        <td><div title="Cannot delete subscription" class="x"/></td>
        {% else %}
        <td>
            <form method="GET" action="{% url 'deleteSubscription' topic.url_name sub.name %}">
                <input type="submit" value="Delete Subscription" class="small-button"/>
            </form></td>
<!--        <td><a href="{% url 'deleteSubscription' topic.url_name sub.name %}">Delete Subscription</a></td>-->
        {% endif %}
        {% if sub.msgBacklog > 0 %}
        <td><a title="Clear backlog from this subscription" href="{% url 'clearSubscription' topic.url_name sub.name %}">clear</a></td>
        <td><a href="{% url 'messages' topic.url_name sub.name %}">View Messages</a></td>
        {% else %}
        <td></td>
        <td></td>
        {% endif %}
    </tr>

    <tr class="{% cycle 'row1' 'row2' %}" id="consumers-{{sub.id}}"
        style="display:none">
        <td></td>
        <td colspan="6">
            <table>
            <thead>
                <tr>
                    <th>Consumer</th>
                    <th>Address</th>
                    <th title="Msg/s">Rate out</th>
                    <th title="bytes/s">Throughput out</th>
                    <th title="Msg/s">Redelivery rate</th>
                    <th>Connected since</th>
                    <th>Available permits</th>
                    <th title="Messages">Unacked</th>
                    <th title="Blocked for max unacked messages">Blocked</th>
                </tr>
            </thead>
            <tbody>
            {% for consumerStats in consumers %}
                <tr>
                    <td>{{consumerStats.consumerName}}</td>
                    <td>{{consumerStats.address}}</td>
                    <td>{{consumerStats.msgRateOut | intcomma}}</td>
                    <td>{{consumerStats.msgThroughputOut | intcomma}}</td>
                    <td>{{consumerStats.msgRateRedeliver | intcomma}}</td>
                    <td title="{{consumerStats.connectedSince}} UTC">{{consumerStats.connectedSince | naturaltime}}</td>
                    <td>{{consumerStats.availablePermits | intcomma}}</td>
                    <td>{{consumerStats.unackedMessages | intcomma}}</td>
                    <td>{{consumerStats.blockedConsumerOnUnackedMsgs | yesno }}</td>
                </tr>
            {% empty %}
                <tr><td>No consumers</tr></td>
            {% endfor %}
            </tbody>
            </table>
        </td>
    </tr>
{% empty %}
    <tr><td>No subscriptions</tr></td>
{% endfor %}
</tbody>
</table>

{%if topic.is_global%}

<h2>Replication from {{topic.cluster}}</h2>

<table>
<thead>
    <tr>
        <th>Remote Cluster</th>
        <th title="Msg/s">Rate in</th>
        <th title="Msg/s">Rate out</th>
        <th>Mbps in</th>
        <th>Mbps out</th>
        <th title="Messages">Replication backlog</th>
    </tr>
</thead>
<tbody>
{% for peer in peers %}
    <tr>
        <td>{{peer.remote_cluster__name}}</td>
        <td title="{{peer.remote_cluster__name}} ⟶ {{selectedCluster}}">{{peer.msgRateIn__sum | intcomma}}</td>
        <td title="{{selectedCluster}} ⟶ {{peer.remote_cluster__name}}">{{peer.msgRateOut__sum | intcomma}}</td>
        <td title="{{peer.remote_cluster__name}} ⟶ {{selectedCluster}}">{{peer.msgThroughputIn__sum | mbps | floatformat | intcomma}}</td>
        <td title="{{selectedCluster}} ⟶ {{peer.remote_cluster__name}}">{{peer.msgThroughputOut__sum | mbps | floatformat | intcomma}}</td>
        <td title="{{selectedCluster}} ⟶ {{peer.remote_cluster__name}}">{{peer.replicationBacklog__sum | intcomma}}</td>
    </tr>
{% empty %}
    <tr><td>No replication</tr></td>
{% endfor %}
</tbody>
</table>
{% endif %}

<script>
$("a[data-toggle]").on("click", function(e) {
  e.preventDefault();  // prevent navigating
  var selector = $(this).data("toggle");  // get corresponding selector from data-toggle
  $(selector).toggle( "slow", function() {
  });
});
</script>
{% endblock %}
