DescribeRouterUseCase.cs 1.6 KB

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