| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- using RackPeek.Domain.Helpers;
- using RackPeek.Domain.Persistence;
- using RackPeek.Domain.Resources.SubResources;
- namespace RackPeek.Domain.Resources.Switches;
- public record SwitchDescription(
- string Name,
- string? Model,
- bool? Managed,
- bool? Poe,
- int TotalPorts,
- double TotalSpeedGb,
- string PortSummary,
- Dictionary<string, string> Labels
- );
- public class DescribeSwitchUseCase(IResourceCollection repository) : IUseCase {
- public async Task<SwitchDescription> ExecuteAsync(string name) {
- name = Normalize.HardwareName(name);
- ThrowIfInvalid.ResourceName(name);
- var switchResource = await repository.GetByNameAsync(name) as Switch;
- if (switchResource == null)
- throw new NotFoundException($"Switch '{name}' not found.");
- // If no ports exist, return defaults
- List<Port> ports = switchResource.Ports ?? new List<Port>();
- // Total ports count
- var totalPorts = ports.Sum(p => p.Count ?? 0);
- // Total speed (sum of each port speed * count)
- var totalSpeedGb = ports.Sum(p => (p.Speed ?? 0) * (p.Count ?? 0));
- // Build a port summary string
- IEnumerable<string> portGroups = ports
- .GroupBy(p => p.Type ?? "Unknown")
- .Select(g => {
- var count = g.Sum(x => x.Count ?? 0);
- var speed = g.Sum(x => (x.Speed ?? 0) * (x.Count ?? 0));
- return $"{g.Key}: {count} ports ({speed} Gb total)";
- });
- var portSummary = string.Join(", ", portGroups);
- return new SwitchDescription(
- switchResource.Name,
- switchResource.Model,
- switchResource.Managed,
- switchResource.Poe,
- totalPorts,
- totalSpeedGb,
- portSummary,
- switchResource.Labels
- );
- }
- }
|