DescribeSwitchUseCase.cs 1.6 KB

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