<!--
   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.
-->
<h2>SCM Information</h2>
<table class="table table-bordered table-striped">
    <tbody>
    <tr>
        <td>Scm Id:</td>
        <td>{{$ctrl.overview.jmx.ScmId}}</td>
    </tr>
    <tr>
        <td>Cluster Id:</td>
        <td>{{$ctrl.overview.jmx.ClusterId}}</td>
    </tr>
    </tbody>
</table>

<h2>Usage Statistics</h2>
<table class="table table-bordered table-striped">
    <tbody>
    <tr>
        <th>Datanode Usage</th>
        <th>in percentage (%)</th>
    </tr>
    <tr>
        <td>Min</td>
        <td>{{statistics.nodes.usages.min}}</td>
    </tr>
    <tr>
        <td>Median</td>
        <td>{{statistics.nodes.usages.median}}</td>
    </tr>
    <tr>
        <td>Max</td>
        <td>{{statistics.nodes.usages.max}}</td>
    </tr>
    <tr>
        <td>Standard Deviation</td>
        <td>{{statistics.nodes.usages.stdev}}</td>
    </tr>
    </tbody>
</table>

<h2>State Statistics</h2>
<table class="table table-bordered table-striped">
    <tbody>
    <tr>
        <th>Datanode State</th>
        <th>Count</th>
    </tr>
    <tr>
        <td>Healthy Nodes</td>
        <td>{{statistics.nodes.state.healthy}}</td>
    </tr>
    <tr>
        <td>Dead Nodes</td>
        <td>{{statistics.nodes.state.dead}}</td>
    </tr>
    <tr>
        <td>Decommissioning Nodes</td>
        <td>{{statistics.nodes.state.decommissioning}}</td>
    </tr>
    <tr>
        <td>Entering Maintenance Nodes</td>
        <td>{{statistics.nodes.state.enteringmaintenance}}</td>
    </tr>
    <tr>
        <td>Volume Failures Nodes</td>
        <td>{{statistics.nodes.state.volumefailures}}</td>
    </tr>
    </tbody>
</table>

<h2>Space Statistics</h2>
<table class="table table-bordered table-striped">
    <tbody>
    <tr>
        <th>Datanode Space</th>
        <th>Size</th>
    </tr>
    <tr>
        <td>Capacity</td>
        <td>{{statistics.nodes.space.capacity}}</td>
    </tr>
    <tr>
        <td>ScmUsed</td>
        <td>{{statistics.nodes.space.scmused}}</td>
    </tr>
    <tr>
        <td>Remaining</td>
        <td>{{statistics.nodes.space.remaining}}</td>
    </tr>
    <tr>
        <td>Non ScmUsed</td>
        <td>{{statistics.nodes.space.nonscmused}}</td>
    </tr>
    </tbody>
</table>

<h2>Pipeline Statistics</h2>
<table class="table table-bordered table-striped">
    <tbody>
    <tr>
        <th>Pipeline State</th>
        <th>Size</th>
    </tr>
    <tr>
        <td>Closed</td>
        <td>{{statistics.pipelines.closed}}</td>
    </tr>
    <tr>
        <td>Allocated</td>
        <td>{{statistics.pipelines.allocated}}</td>
    </tr>
    <tr>
        <td>Open</td>
        <td>{{statistics.pipelines.open}}</td>
    </tr>
    <tr>
        <td>Dormant</td>
        <td>{{statistics.pipelines.dormant}}</td>
    </tr>
    </tbody>
</table>

<h2>Container Statistics</h2>
<table class="table table-bordered table-striped">
    <tbody>
    <tr>
        <th>Operational State</th>
        <th>Size</th>
    </tr>
    <tr>
        <td>Open</td>
        <td>{{statistics.containers.lifecycle.open}}</td>
    </tr>
    <tr>
        <td>Closing</td>
        <td>{{statistics.containers.lifecycle.closing}}</td>
    </tr>
    <tr>
        <td>Quasi Closed</td>
        <td>{{statistics.containers.lifecycle.quasi_closed}}</td>
    </tr>
    <tr>
        <td>Closed</td>
        <td>{{statistics.containers.lifecycle.closed}}</td>
    </tr>
    <tr>
        <td>Deleting</td>
        <td>{{statistics.containers.lifecycle.deleting}}</td>
    </tr>
    <tr>
        <td>Deleted</td>
        <td>{{statistics.containers.lifecycle.deleted}}</td>
    </tr>
    <tr>
        <td>Recovering</td>
        <td>{{statistics.containers.lifecycle.recovering}}</td>
    </tr>
    </tbody>
</table>
<table class="table table-bordered table-striped">
    <tbody>
    <tr>
        <th>Health</th>
        <th>Size</th>
    </tr>
    <tr>
        <td>Under Replicated</td>
        <td>{{statistics.containers.health.under_replicated}}</td>
    </tr>
    <tr>
        <td>Mis Replicated</td>
        <td>{{statistics.containers.health.mis_replicated}}</td>
    </tr>
    <tr>
        <td>Over Replicated</td>
        <td>{{statistics.containers.health.over_replicated}}</td>
    </tr>
    <tr>
        <td>Missing</td>
        <td>{{statistics.containers.health.missing}}</td>
    </tr>
    <tr>
        <td>Unhealthy</td>
        <td>{{statistics.containers.health.unhealthy}}</td>
    </tr>
    <tr>
        <td>Empty</td>
        <td>{{statistics.containers.health.empty}}</td>
    </tr>
    <tr>
        <td>Open Unhealthy</td>
        <td>{{statistics.containers.health.open_unhealthy}}</td>
    </tr>
    <tr>
        <td>Quasi Closed Stuck</td>
        <td>{{statistics.containers.health.quasi_closed_stuck}}</td>
    </tr>
    <tr>
        <td>Open Without Pipeline</td>
        <td>{{statistics.containers.health.open_without_pipeline}}</td>
    </tr>
    </tbody>
</table>

<h2>Node Status</h2>
<div class="row">
    <div class="col-md-6 text-left">
        <label>Show: </label>
        <select class="form-select" ng-model="RecordsToDisplay" ng-change="UpdateRecordsToShow()">
            <option value="10" ng-selected="{true}">10</option>
            <option value="20">20</option>
            <option value="50">50</option>
            <option value="All">All</option>
        </select>
    </div>
    <div class="col-md-6 text-right">
        <label>Search: </label> <input type="text" ng-model="search" ng-change="applyGlobalSearch()" ng-model-options="{ debounce: 300 }">
    </div>
</div>
<table class="table table-bordered table-striped col-md-6">
    <thead>
        <tr>
            <th ng-click = "columnSort('hostname')" class="nodeStatusInfo"><span ng-class = "{'sorting' : (columnName != 'hostname'), 'sortasc' : (columnName == 'hostname' && !reverse),
                                        'sortdesc':(columnName == 'hostname' && !reverse)}">HostName</span></th>
            <th ng-click = "columnSort('usedspacepercent')" class="nodeStatusInfo" ><span ng-class="{'sorting' : (columnName != 'usedspacepercent'), 'sortasc' : (columnName == 'usedspacepercent' && !reverse),
                                        'sortdesc':(columnName == 'usedspacepercent' && !reverse)}">Used Space Percent</span></th>
            <th ng-click = "columnSort('capacity')" class="nodeStatusInfo" ><span ng-class="{'sorting' : (columnName != 'capacity'), 'sortasc' : (columnName == 'capacity' && !reverse),
                                        'sortdesc':(columnName == 'capacity' && !reverse)}">Capacity</span></th>
            <th ng-click = "columnSort('opstate')" class="nodeStatusInfo" ><span ng-class="{'sorting' : (columnName != 'opstate'), 'sortasc' : (columnName == 'opstate' && !reverse),
                                        'sortdesc':(columnName == 'opstate' && !reverse)}">Operational State</span></th>
            <th ng-click = "columnSort('comstate')" class="nodeStatusInfo">  <span ng-class="{'sorting' : (columnName != 'comstate'), 'sortasc' : (columnName == 'comstate' && !reverse),
                                        'sortdesc':(columnName == 'comstate' && !reverse)}">Commisioned State</span> </th>
            <th ng-click = "columnSort('lastheartbeat')" class="nodeStatusInfo">  <span ng-class="{'sorting' : (columnName != 'lastheartbeat'), 'sortasc' : (columnName == 'heartbeat' && !reverse),
                                        'sortdesc':(columnName == 'lastheartbeat' && !reverse)}">Last Heartbeat</span> </th>
            <th ng-click = "columnSort('uuid')" class="uuId" ><span ng-class="{'sorting' : (columnName != 'uuid'), 'sortasc' : (columnName == 'uuid' && !reverse),
                                        'sortdesc':(columnName == 'uuid' && !reverse)}">UUID</span></th>
            <th ng-click = "columnSort('version')" class="version" ><span ng-class="{'sorting' : (columnName != 'version'), 'sortasc' : (columnName == 'version' && !reverse),
                                        'sortdesc':(columnName == 'version' && !reverse)}">Version</span></th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="typestat in nodeStatus|orderBy:columnName:reverse">
            <td ng-switch="typestat.port > 0">
                <span ng-switch-when="true">
                    <a href="{{typestat.protocol}}://{{typestat.hostname}}:{{typestat.port}}" target="_blank">{{typestat.hostname}}</a>
                </span>
                <span ng-switch-when="false">
                    {{typestat.hostname}}
                </span>
            </td>
            <td>{{typestat.usedspacepercent}}</td>
            <td>{{typestat.capacity}}</td>
            <td>{{typestat.opstate}}</td>
            <td>{{typestat.comstate}}</td>
            <td>{{typestat.lastheartbeat}}</td>
            <td>{{typestat.uuid}}</td>
            <td>{{typestat.version}}</td>
       </tr>
    </tbody>
</table>

<div class="row">
    <div class="col-md-6 text-left">
        <label>Page:</label>
        <select class="form-select" ng-model="currentPage" ng-change="handlePagination(currentPage, false)">
            <option ng-repeat="page in getPagesArray()" ng-value="page">{{page}}</option>
        </select>
        <span>of {{lastIndex}}. </span>
        <span ng-if="nodeStatus && nodeStatus.length > 0">
            Showing {{getCurrentPageFirstItemIndex()}} to {{getCurrentPageLastItemIndex()}} of the total {{totalItems}} entries.
        </span>
    </div>
    <div class="col-md-6 text-right">
        <nav aria-label="..." ng-show="RecordsToDisplay !== 'All'">
            <ul class="pagination" style="margin: 0; padding: 0">
                <li class="page-item" ng-class="{disabled:currentPage==1}"
                    ng-click="handlePagination(currentPage-1,(currentPage==1))">
                    <span class="page-link" tabindex="-1">Previous</span>
                </li>
                <li class="page-item active">
                    <span class="page-link">{{currentPage}} </span>
                </li>
                <li class="page-item" ng-class="{disabled:lastIndex==currentPage}"
                    ng-click="handlePagination(currentPage+1, (lastIndex==currentPage))">
                    <span class="page-link" tabindex="-1">Next</span>
                </li>
            </ul>
        </nav>
    </div>
</div>

<h2>Status</h2>
<table class="table table-bordered table-striped" class="col-md-6">
    <tbody>
    <tr>
        <td>Client Rpc port</td>
        <td>{{$ctrl.overview.jmx.ClientRpcPort}}</td>
    </tr>
    <tr>
        <td>Datanode Rpc port</td>
        <td>{{$ctrl.overview.jmx.DatanodeRpcPort}}</td>
    </tr>
    <tr>
        <td>Node Manager: Safe mode status</td>
        <td>{{$ctrl.overview.jmx.InSafeMode}}</td>
    </tr>
    <tr ng-hide="!$ctrl.overview.jmx.SafeModeExitForceful">
        <td> Force Exit Safe Mode </td>
        <td>{{$ctrl.overview.jmx.SafeModeExitForceful}}</td>
    </tr>
    <tr ng-hide="!$ctrl.overview.jmx.PrimordialNode">
        <td> Primordial Node (HA) </td>
        <td>{{$ctrl.overview.jmx.PrimordialNode}}</td>
    </tr>
    </tbody>
</table>

<h2>Meta-Data Volume Information</h2>
<table class="table table-bordered table-striped" class="col-md-6">
    <tbody>
    <tr>
        <td>Ratis Directory</td>
        <td>{{$ctrl.overview.jmx.RatisLogDirectory}}</td>
    </tr>
    <tr>
        <td>RocksDB Directory</td>
        <td>{{$ctrl.overview.jmx.RocksDbDirectory}}</td>
    </tr>
    </tbody>
</table>

<h2>SCM Roles (HA)</h2>
<h4 ng-show="$ctrl.overview.jmx.ScmRatisRoles.length == 1 && $ctrl.overview.jmx.ScmRatisRoles[0].length == 1">{{$ctrl.overview.jmx.ScmRatisRoles[0][0]}}</h4>
<div ng-show="$ctrl.overview.jmx.ScmRatisRoles.length > 1">
    <table class="table table-striped table-bordered" class="col-md-6">
        <thead>
        <tr>
            <th>Host Name</th>
            <th>Node ID</th>
            <th>Ratis Port</th>
            <th>Role</th>
        </tr>
        </thead>
        <tbody ng-repeat="roles in $ctrl.overview.jmx.ScmRatisRoles">
        <tr class="scm-roles-background" ng-if="$ctrl.role.Id == roles[1]">
            <td>{{roles[0]}}</td>
            <td>{{roles[1]}}</td>
            <td>{{roles[2]}}</td>
            <td>{{roles[3]}}</td>
        </tr>
        <tr ng-if="$ctrl.role.Id != roles[1]">
            <td>{{roles[0]}}</td>
            <td>{{roles[1]}}</td>
            <td>{{roles[2]}}</td>
            <td>{{roles[3]}}</td>
        </tr>
        </tbody>
    </table>
</div>

<h2>Safemode rules statuses</h2>

<table class="table table-bordered table-striped" class="col-md-6">
    <thead>
    <tr>
        <th>Rule Id</th>
        <th>Rule definition</th>
        <th>Passed</th>
    </tr>
    </thead>
    <tbody>
    <tr ng-repeat="typestat in $ctrl.overview.jmx.SafeModeRuleStatus">
        <td>{{typestat.key}}</td>
        <td ng-bind-html="formatValue(typestat.value[0])"></td>
        <td>{{typestat.value[1]}}</td>
    </tr>
    </tbody>
</table>
