@using Opserver.Data.HAProxy
@using Opserver.Views.Shared.Guages
@inject HAProxyModule HAProxy
@{
    var allGroups = HAProxy.Groups;
    var allProxies = HAProxy.GetAllProxies();
    var allServers = allProxies.SelectMany(p => p.Servers).ToList();
    var totalUp = allServers.Count(s => s.MonitorStatus == MonitorStatus.Good);
    var totalOut = allServers.Count(s => s.OutOfRotation);
    var totalDown = allServers.Count(s => s.MonitorStatus != MonitorStatus.Good && !s.OutOfRotation);
}
<div class="panel panel-default">
    <div class="panel-heading">@allGroups.GetWorstStatus().IconSpan() HAProxy</div>
    <div class="panel-body">
        <div class="col-xs-4">
            <partial Name="Gauges/Circle" Model="@(new CircleModel("Group", allGroups))" />
        </div>
        <div class="col-xs-4">
            <partial Name="Gauges/Circle" Model="@(new CircleModel("Proxy", allProxies))" />
        </div>
        <div class="col-xs-4">
            <partial Name="Gauges/Circle" Model="@(new CircleModel("Server", allServers))" />
        </div>
        <table class="table table-super-condensed table-striped col-md-12">
            <thead>
                <tr>
                    <th>Group</th>
                    <th>In</th>
                    <th>Out</th>
                    <th>Down</th>
                    <th>Sessions</th>
                    <th>Rate</th>
                </tr>
            </thead>
            <tbody>
            @foreach (var group in allGroups)
            {
                var servers = group.GetProxies().SelectMany(p => p.Servers).ToList();
                var gOut = servers.Count(s => s.OutOfRotation);
                var gDown = servers.Count(s => s.MonitorStatus != MonitorStatus.Good && !s.InMaintenance);
                <tr>
                    <td>@group.IconSpan() @group.Name</td>
                    <td>@servers.Count(s => s.MonitorStatus == MonitorStatus.Good).ToComma()</td>
                    <td class="@(gOut > 0 ? MonitorStatus.Maintenance.TextClass() : null)">@gOut.ToComma()</td>
                    <td class="@(gDown > 0 ? MonitorStatus.Critical.TextClass() : null)">@gDown.ToComma()</td>
                    <td>@servers.Sum(s => s.CurrentSessions).ToComma()</td>
                    <td>@servers.Sum(s => s.CurrentNewSessionsPerSecond).ToComma()</td>
                </tr>
            }
            </tbody>
            <tfoot>
                <tr class="total-row">
                    <td><strong>Total</strong></td>
                    <td>@totalUp.ToComma()</td>
                    <td class="@(totalOut > 0 ? MonitorStatus.Maintenance.TextClass() : null)">@totalOut.ToComma()</td>
                    <td class="@(totalDown > 0 ? MonitorStatus.Critical.TextClass() : null)">@totalDown.ToComma()</td>
                    <td>@allServers.Sum(s => s.CurrentSessions).ToComma()</td>
                    <td>@allServers.Sum(s => s.CurrentNewSessionsPerSecond).ToComma()</td>
                </tr>
            </tfoot>
        </table>
    </div>
</div>