SwitchHardwareReport.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using RackPeek.Domain.Resources.Models;
  2. namespace RackPeek.Domain.Resources.Hardware.Switches;
  3. public record SwitchHardwareReport(
  4. IReadOnlyList<SwitchHardwareRow> Switches
  5. );
  6. public record SwitchHardwareRow(
  7. string Name,
  8. string Model,
  9. bool Managed,
  10. bool Poe,
  11. int TotalPorts,
  12. double MaxPortSpeedGb,
  13. string PortSummary
  14. );
  15. public class SwitchHardwareReportUseCase(IHardwareRepository repository) : IUseCase
  16. {
  17. public async Task<SwitchHardwareReport> ExecuteAsync()
  18. {
  19. var hardware = await repository.GetAllAsync();
  20. var switches = hardware.OfType<Switch>();
  21. var rows = switches.Select(sw =>
  22. {
  23. var totalPorts = sw.Ports?.Sum(p => p.Count ?? 0) ?? 0;
  24. var maxSpeed = sw.Ports?
  25. .Max(p => p.Speed ?? 0) ?? 0;
  26. var portSummary = sw.Ports == null
  27. ? "Unknown"
  28. : string.Join(", ",
  29. sw.Ports
  30. .GroupBy(p => p.Speed ?? 0)
  31. .OrderBy(g => g.Key)
  32. .Select(g =>
  33. {
  34. var count = g.Sum(p => p.Count ?? 0);
  35. return $"{count}×{g.Key}G";
  36. }));
  37. return new SwitchHardwareRow(
  38. sw.Name,
  39. sw.Model ?? "Unknown",
  40. sw.Managed ?? false,
  41. sw.Poe ?? false,
  42. totalPorts,
  43. maxSpeed,
  44. portSummary
  45. );
  46. }).ToList();
  47. return new SwitchHardwareReport(rows);
  48. }
  49. }