DescribeRouterUseCase.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Persistence;
  3. using RackPeek.Domain.Resources.SubResources;
  4. namespace RackPeek.Domain.Resources.Hardware.Routers;
  5. public record RouterDescription(
  6. string Name,
  7. string? Model,
  8. bool? Managed,
  9. bool? Poe,
  10. int TotalPorts,
  11. double TotalSpeedGb,
  12. string PortSummary
  13. );
  14. public class DescribeRouterUseCase(IResourceCollection repository) : IUseCase
  15. {
  16. public async Task<RouterDescription> ExecuteAsync(string name)
  17. {
  18. name = Normalize.HardwareName(name);
  19. ThrowIfInvalid.ResourceName(name);
  20. var routerResource = await repository.GetByNameAsync(name) as Router;
  21. if (routerResource == null)
  22. throw new NotFoundException($"Router '{name}' not found.");
  23. // If no ports exist, return defaults
  24. var ports = routerResource.Ports ?? new List<Port>();
  25. // Total ports count
  26. var totalPorts = ports.Sum(p => p.Count ?? 0);
  27. // Total speed (sum of each port speed * count)
  28. var totalSpeedGb = ports.Sum(p => (p.Speed ?? 0) * (p.Count ?? 0));
  29. // Build a port summary string
  30. var portGroups = ports
  31. .GroupBy(p => p.Type ?? "Unknown")
  32. .Select(g =>
  33. {
  34. var count = g.Sum(x => x.Count ?? 0);
  35. var speed = g.Sum(x => (x.Speed ?? 0) * (x.Count ?? 0));
  36. return $"{g.Key}: {count} ports ({speed} Gb total)";
  37. });
  38. var portSummary = string.Join(", ", portGroups);
  39. return new RouterDescription(
  40. routerResource.Name,
  41. routerResource.Model,
  42. routerResource.Managed,
  43. routerResource.Poe,
  44. totalPorts,
  45. totalSpeedGb,
  46. portSummary
  47. );
  48. }
  49. }