FirewallHardwareReport.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using RackPeek.Domain.Resources.Hardware.Models;
  2. namespace RackPeek.Domain.Resources.Hardware.Reports;
  3. public record FirewallHardwareReport(
  4. IReadOnlyList<FirewallHardwareRow> Firewalls
  5. );
  6. public record FirewallHardwareRow(
  7. string Name,
  8. string Model,
  9. bool Managed,
  10. bool Poe,
  11. int TotalPorts,
  12. int MaxPortSpeedGb,
  13. string PortSummary
  14. );
  15. public class FirewallHardwareReportUseCase(IHardwareRepository repository)
  16. {
  17. public async Task<FirewallHardwareReport> ExecuteAsync()
  18. {
  19. var hardware = await repository.GetAllAsync();
  20. var firewalls = hardware.OfType<Firewall>();
  21. var rows = firewalls.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 FirewallHardwareRow(
  38. Name: sw.Name,
  39. Model: sw.Model ?? "Unknown",
  40. Managed: sw.Managed ?? false,
  41. Poe: sw.Poe ?? false,
  42. TotalPorts: totalPorts,
  43. MaxPortSpeedGb: maxSpeed,
  44. PortSummary: portSummary
  45. );
  46. }).ToList();
  47. return new FirewallHardwareReport(rows);
  48. }
  49. }