@page "/subnets" @using RackPeek.Domain.Persistence @using RackPeek.Domain.Resources @inject IResourceCollection ResourceCollection

Subnet Browser

@if (_grouped is null) {
loading…
} else if (!_grouped.Any()) {
no matching IPs found
} else {
@foreach (var subnetGroup in _grouped.OrderBy(x => x.Key)) {
@subnetGroup.Key
    @foreach (var ipGroup in subnetGroup.Value.OrderBy(x => x.Key)) {
  • ├─ @ipGroup.Key
      @foreach (var (resource, _) in ipGroup.Value) { var url = GetResourceUrl(resource);
    • └─ @resource.Name (@resource.GetType().Name.Replace("Resource",""))
    • }
  • }
}
}
@code { private string _filter = string.Empty; private IReadOnlyList<(Resource resource, string ip)> _all = []; private Dictionary>>? _grouped; protected override async Task OnInitializedAsync() { _all = await ResourceCollection.GetResourceIpsAsync(); ApplyFilter(); } private string Filter { get => _filter; set { if (_filter == value) return; _filter = value; ApplyFilter(); } } private void ApplyFilter() { var filtered = string.IsNullOrWhiteSpace(_filter) ? _all : _all.Where(x => x.ip.Contains(_filter, StringComparison.OrdinalIgnoreCase)) .ToList(); _grouped = filtered .Where(x => !string.IsNullOrWhiteSpace(x.ip)) .GroupBy(x => GetSubnet(x.ip)) .ToDictionary( g => g.Key, g => g.GroupBy(x => x.ip) .ToDictionary( ip => ip.Key, ip => ip.ToList() ) ); StateHasChanged(); } private string GetSubnet(string ip) { var parts = ip.Split('.'); return parts.Length == 4 ? $"{parts[0]}.{parts[1]}.{parts[2]}.x" : "unknown"; } private string GetResourceUrl(Resource resource) { return resource switch { RackPeek.Domain.Resources.SystemResources.SystemResource => $"resources/systems/{Uri.EscapeDataString(resource.Name)}", RackPeek.Domain.Resources.Services.Service => $"resources/services/{Uri.EscapeDataString(resource.Name)}", _ => "#" }; } }