SystemResourceMapper.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using RackPeek.Domain.Resources.SubResources;
  2. using RackPeek.Domain.Resources.SystemResources;
  3. namespace RackPeek.Domain.Discovery;
  4. /// <summary>Maps host facts onto the System resource RackPeek stores. Pure.</summary>
  5. public static class SystemResourceMapper {
  6. /// <summary>
  7. /// <paramref name="nameOverride" /> is what makes repeated runs on a box stable
  8. /// regardless of hostname changes, and is the recommended way to run this from a
  9. /// timer. Without it the hostname is used.
  10. /// </summary>
  11. public static SystemResource ToResource(SystemFacts facts, string? nameOverride = null) {
  12. var discoveryId = DiscoveryId.Create(
  13. DiscoveryId.SystemScheme,
  14. facts.MachineId ?? facts.Hostname);
  15. var resource = new SystemResource {
  16. Kind = SystemResource.KindLabel,
  17. Name = DiscoveryNaming.Suggest(
  18. nameOverride ?? DiscoveryNaming.HostLabel(facts.Hostname),
  19. "system",
  20. discoveryId),
  21. DiscoveryId = discoveryId,
  22. Type = facts.Type,
  23. Os = facts.Os,
  24. Cores = facts.Cores,
  25. Ram = facts.RamGb,
  26. Ip = facts.Ip,
  27. Drives = facts.Drives.Count == 0
  28. ? null
  29. : facts.Drives.Select(d => new Drive { Type = d.Type, Size = d.SizeGb }).ToList()
  30. };
  31. // The bridge to network discovery: a scan identifies this machine by one of
  32. // these, so carrying them lets the resolver land both collectors on one card.
  33. if (facts.Macs.Count > 0)
  34. resource.Labels["macs"] = string.Join(",", facts.Macs);
  35. return resource;
  36. }
  37. }