@using RackPeek.Domain.Persistence
@using RackPeek.Domain.Resources.Connections
@using RackPeek.Domain.Resources.Servers
@using RackPeek.Domain.Resources.SubResources
@inject IResourceCollection Repository
@if (PortGroup is null || string.IsNullOrWhiteSpace(ResourceName))
{
No ports available.
}
else
{
@for (var i = 0; i < PortGroup.Count; i++)
{
var index = i;
var port = new PortReference
{
Resource = ResourceName,
PortGroup = PortGroupIndex,
PortIndex = index
};
var conn = GetConnection(port);
var other = GetOther(conn, port);
var isConnected = other != null;
Port? otherGroup = null;
if (isConnected)
{
otherGroup = GetDestinationPortGroup(other!);
}
if (isConnected)
{
@(index + 1)
@other!.Resource
@if (otherGroup != null)
{
@otherGroup.Type — @otherGroup.Speed Gbps
(port @(other.PortIndex + 1) / @otherGroup.Count)
}
}
else
{
HandlePortClick(port)">
}
}
}
@code {
[Parameter] public string ResourceName { get; set; } = "";
[Parameter] public int PortGroupIndex { get; set; }
[Parameter] public Port? PortGroup { get; set; }
[Parameter] public EventCallback OnPortClicked { get; set; }
private List _connections = new();
private readonly Dictionary _portResources = new();
protected override async Task OnParametersSetAsync()
{
_connections = (await Repository.GetConnectionsAsync()).ToList();
}
async Task HandlePortClick(PortReference port)
{
if (OnPortClicked.HasDelegate)
await OnPortClicked.InvokeAsync(port);
}
string PortClass(bool connected)
{
return $@"
w-28
h-12
border-r
border-b
border-zinc-800
text-[10px]
leading-tight
flex
flex-col
justify-center
px-1
transition
hover:bg-zinc-800
{(connected ? "bg-blue-950/40 text-blue-200" : "text-zinc-500")}
";
}
Connection? GetConnection(PortReference port)
{
return _connections.FirstOrDefault(c =>
(c.A.Resource == port.Resource &&
c.A.PortGroup == port.PortGroup &&
c.A.PortIndex == port.PortIndex)
||
(c.B.Resource == port.Resource &&
c.B.PortGroup == port.PortGroup &&
c.B.PortIndex == port.PortIndex));
}
PortReference? GetOther(Connection? conn, PortReference port)
{
if (conn == null)
return null;
if (conn.A.Resource == port.Resource &&
conn.A.PortGroup == port.PortGroup &&
conn.A.PortIndex == port.PortIndex)
return conn.B;
return conn.A;
}
Port? GetDestinationPortGroup(PortReference other)
{
if (!_portResources.ContainsKey(other.Resource))
{
var res = Repository.GetByNameAsync(other.Resource).Result;
if (res is IPortResource pr)
_portResources[other.Resource] = pr;
else
_portResources[other.Resource] = null;
}
var portResource = _portResources[other.Resource];
if (portResource?.Ports == null)
return null;
if (other.PortGroup < 0 || other.PortGroup >= portResource.Ports.Count)
return null;
return portResource.Ports[other.PortGroup];
}
}