DesktopHardwareReport.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using RackPeek.Domain.Persistence;
  2. namespace RackPeek.Domain.Resources.Desktops;
  3. public record DesktopHardwareReport(
  4. IReadOnlyList<DesktopHardwareRow> Desktops
  5. );
  6. public record DesktopHardwareRow(
  7. string Name,
  8. string CpuSummary,
  9. int TotalCores,
  10. int TotalThreads,
  11. double RamGb,
  12. int TotalStorageGb,
  13. int SsdStorageGb,
  14. int HddStorageGb,
  15. string NicSummary,
  16. string GpuSummary
  17. );
  18. public class DesktopHardwareReportUseCase(IResourceCollection repository) : IUseCase {
  19. public async Task<DesktopHardwareReport> ExecuteAsync() {
  20. IReadOnlyList<Desktop> desktops = await repository.GetAllOfTypeAsync<Desktop>();
  21. var rows = desktops.Select(desktop => {
  22. var totalCores = desktop.Cpus?.Sum(c => c.Cores) ?? 0;
  23. var totalThreads = desktop.Cpus?.Sum(c => c.Threads) ?? 0;
  24. var cpuSummary = desktop.Cpus == null
  25. ? "Unknown"
  26. : string.Join(", ",
  27. desktop.Cpus
  28. .GroupBy(c => c.Model)
  29. .Select(g => $"{g.Count()}× {g.Key}"));
  30. var ramGb = desktop.Ram?.Size ?? 0;
  31. var totalStorage = desktop.Drives?.Sum(d => d.Size) ?? 0;
  32. var ssdStorage = desktop.Drives?
  33. .Where(d => d.Type == "ssd")
  34. .Sum(d => d.Size) ?? 0;
  35. var hddStorage = desktop.Drives?
  36. .Where(d => d.Type == "hdd")
  37. .Sum(d => d.Size) ?? 0;
  38. var nicSummary = desktop.Ports == null
  39. ? "Unknown"
  40. : string.Join(", ",
  41. desktop.Ports
  42. .GroupBy(n => n.Speed ?? 0)
  43. .OrderBy(g => g.Key)
  44. .Select(g => {
  45. var count = g.Sum(n => n.Count ?? 0);
  46. return $"{count}×{g.Key}G";
  47. }));
  48. var gpuSummary = desktop.Gpus == null
  49. ? "None"
  50. : string.Join(", ",
  51. desktop.Gpus
  52. .GroupBy(g => g.Model)
  53. .Select(g => $"{g.Count()}× {g.Key}"));
  54. return new DesktopHardwareRow(
  55. desktop.Name,
  56. cpuSummary,
  57. totalCores,
  58. totalThreads,
  59. ramGb,
  60. totalStorage,
  61. ssdStorage,
  62. hddStorage,
  63. nicSummary,
  64. gpuSummary
  65. );
  66. }).ToList();
  67. return new DesktopHardwareReport(rows);
  68. }
  69. }