SwitchHardwareReport.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using RackPeek.Domain.Persistence;
  2. namespace RackPeek.Domain.Resources.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(IResourceCollection repository) : IUseCase {
  16. public async Task<SwitchHardwareReport> ExecuteAsync() {
  17. IReadOnlyList<Switch> switches = await repository.GetAllOfTypeAsync<Switch>();
  18. var rows = switches.Select(sw => {
  19. var totalPorts = sw.Ports?.Sum(p => p.Count ?? 0) ?? 0;
  20. var maxSpeed = sw.Ports?
  21. .Max(p => p.Speed ?? 0) ?? 0;
  22. var portSummary = sw.Ports == null
  23. ? "Unknown"
  24. : string.Join(", ",
  25. sw.Ports
  26. .GroupBy(p => p.Speed ?? 0)
  27. .OrderBy(g => g.Key)
  28. .Select(g => {
  29. var count = g.Sum(p => p.Count ?? 0);
  30. return $"{count}×{g.Key}G";
  31. }));
  32. return new SwitchHardwareRow(
  33. sw.Name,
  34. sw.Model ?? "Unknown",
  35. sw.Managed ?? false,
  36. sw.Poe ?? false,
  37. totalPorts,
  38. maxSpeed,
  39. portSummary
  40. );
  41. }).ToList();
  42. return new SwitchHardwareReport(rows);
  43. }
  44. }