Heimdall Gitea / Build and tests (pull_request) Failing after 3m28s
Heimdall Gitea / Build and scan server image (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Skipped
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / Publish versioned release (pull_request) Skipped
269 lines
10 KiB
C#
269 lines
10 KiB
C#
using Heimdall.Domain.Enums;
|
|
using Heimdall.Domain.Primitives;
|
|
|
|
namespace Heimdall.Domain.Entities;
|
|
|
|
public class Agent : Entity<Guid>, IAuditableEntity, ISoftDeletable
|
|
{
|
|
public Guid SiteId { get; private set; }
|
|
public string Name { get; private set; } = string.Empty;
|
|
public string Hostname { get; private set; } = string.Empty;
|
|
public string Version { get; private set; } = string.Empty;
|
|
public AgentStatus Status { get; private set; } = AgentStatus.Offline;
|
|
public DateTime? LastHeartbeat { get; private set; }
|
|
public string? LastAppliedCameraConfigVersion { get; private set; }
|
|
public DateTime? LastAppliedCameraConfigAt { get; private set; }
|
|
public string? PublicIp { get; private set; }
|
|
|
|
/// <summary>HMACSHA256 hash of the agent's API key.</summary>
|
|
public string? ApiKeyHash { get; private set; }
|
|
|
|
/// <summary>First 8 characters of the API key for identification.</summary>
|
|
public string? ApiKeyPrefix { get; private set; }
|
|
|
|
// Capabilities (legacy)
|
|
public bool HasGpu { get; private set; }
|
|
public int CpuCores { get; private set; }
|
|
public long MemoryMb { get; private set; }
|
|
public GpuAcceleration GpuType { get; private set; } = GpuAcceleration.None;
|
|
public string? GpuName { get; private set; }
|
|
public long? VramMb { get; private set; }
|
|
|
|
// Live metrics (sent with each heartbeat)
|
|
public double? CpuUsagePercent { get; private set; }
|
|
public long? MemoryUsageBytes { get; private set; }
|
|
public double? DiskUsagePercent { get; private set; }
|
|
public long? UptimeSeconds { get; private set; }
|
|
public double? GpuLoadPercent { get; private set; }
|
|
public long? VramUsageBytes { get; private set; }
|
|
|
|
// ─── CPU inventory ────────────────────────────────────────────
|
|
public string? CpuVendor { get; private set; }
|
|
public string? CpuModel { get; private set; }
|
|
public string? CpuArchitecture { get; private set; }
|
|
public int? PhysicalCores { get; private set; }
|
|
public int? LogicalCores { get; private set; }
|
|
public int? ThreadsPerCore { get; private set; }
|
|
public double? CpuFrequencyMhz { get; private set; }
|
|
public string? CpuFlags { get; private set; }
|
|
public long? CpuCacheBytes { get; private set; }
|
|
|
|
// ─── Memory inventory ─────────────────────────────────────────
|
|
public long? MemTotalBytes { get; private set; }
|
|
public long? MemAvailableBytes { get; private set; }
|
|
public long? SwapTotalBytes { get; private set; }
|
|
public long? SwapFreeBytes { get; private set; }
|
|
|
|
// ─── Container limits ─────────────────────────────────────────
|
|
public long? ContainerCpuLimit { get; private set; }
|
|
public long? ContainerMemoryLimitBytes { get; private set; }
|
|
|
|
// ─── Disk inventory (JSON) ────────────────────────────────────
|
|
public string? DiskInventoryJson { get; private set; }
|
|
|
|
// ─── GPU inventory (JSON) ─────────────────────────────────────
|
|
public string? GpuInventoryJson { get; private set; }
|
|
|
|
// ─── Host info ────────────────────────────────────────────────
|
|
public string? OsName { get; private set; }
|
|
public string? KernelVersion { get; private set; }
|
|
public string? Arch { get; private set; }
|
|
public long? UptimeMachineSeconds { get; private set; }
|
|
|
|
// ─── Container info ───────────────────────────────────────────
|
|
public string? ContainerId { get; private set; }
|
|
public string? ContainerImage { get; private set; }
|
|
|
|
// ─── Network & Temperature (JSON) ─────────────────────────────
|
|
public string? NetworkInterfacesJson { get; private set; }
|
|
public string? TemperatureJson { get; private set; }
|
|
public DateTime? InventoryObservedAtUtc { get; private set; }
|
|
|
|
// Audit
|
|
public DateTime CreatedAt { get; set; }
|
|
public DateTime UpdatedAt { get; set; }
|
|
|
|
// Soft delete
|
|
public bool IsDeleted { get; private set; }
|
|
public DateTime? DeletedAt { get; private set; }
|
|
|
|
// Navigation
|
|
public Site Site { get; private set; } = null!;
|
|
private readonly List<Camera> _cameras = [];
|
|
public IReadOnlyCollection<Camera> Cameras => _cameras.AsReadOnly();
|
|
|
|
private Agent() { } // EF Core
|
|
|
|
public Agent(Guid siteId, string name, string hostname, string version)
|
|
: base(Guid.NewGuid())
|
|
{
|
|
SiteId = siteId;
|
|
Name = name;
|
|
Hostname = hostname;
|
|
Version = version;
|
|
}
|
|
|
|
public void Register(string apiKeyHash, string apiKeyPrefix)
|
|
{
|
|
ApiKeyHash = apiKeyHash;
|
|
ApiKeyPrefix = apiKeyPrefix;
|
|
Status = AgentStatus.Online;
|
|
LastHeartbeat = DateTime.UtcNow;
|
|
}
|
|
|
|
public void UpdateHeartbeat(string? publicIp = null)
|
|
{
|
|
LastHeartbeat = DateTime.UtcNow;
|
|
Status = AgentStatus.Online;
|
|
if (publicIp is not null)
|
|
PublicIp = publicIp;
|
|
}
|
|
|
|
public void AcknowledgeCameraConfig(string version, DateTime appliedAtUtc)
|
|
{
|
|
if (version.Length != 64 || !version.All(Uri.IsHexDigit))
|
|
throw new ArgumentException("Invalid camera configuration version", nameof(version));
|
|
if (appliedAtUtc.Kind != DateTimeKind.Utc)
|
|
throw new ArgumentException("Acknowledgement time must be UTC", nameof(appliedAtUtc));
|
|
LastAppliedCameraConfigVersion = version.ToLowerInvariant();
|
|
LastAppliedCameraConfigAt = appliedAtUtc;
|
|
}
|
|
|
|
public void UpdateLiveMetrics(
|
|
double? cpuUsagePercent,
|
|
long? memoryUsageBytes,
|
|
double? diskUsagePercent,
|
|
long? uptimeSeconds)
|
|
{
|
|
CpuUsagePercent = cpuUsagePercent;
|
|
MemoryUsageBytes = memoryUsageBytes;
|
|
DiskUsagePercent = diskUsagePercent;
|
|
UptimeSeconds = uptimeSeconds;
|
|
}
|
|
|
|
public void MarkOffline()
|
|
{
|
|
Status = AgentStatus.Offline;
|
|
}
|
|
|
|
public void MarkError()
|
|
{
|
|
Status = AgentStatus.Error;
|
|
}
|
|
|
|
public void UpdateCapabilities(bool hasGpu, int cpuCores, long memoryMb, GpuAcceleration gpuType)
|
|
{
|
|
HasGpu = hasGpu;
|
|
CpuCores = cpuCores;
|
|
MemoryMb = memoryMb;
|
|
GpuType = gpuType;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates GPU-specific details reported by the agent (name, VRAM, load).
|
|
/// Called alongside capabilities when the agent detects GPU hardware.
|
|
/// </summary>
|
|
public void UpdateGpuInfo(string? gpuName, long? vramMb, double? gpuLoadPercent, long? vramUsageBytes)
|
|
{
|
|
GpuName = gpuName;
|
|
VramMb = vramMb;
|
|
GpuLoadPercent = gpuLoadPercent;
|
|
VramUsageBytes = vramUsageBytes;
|
|
}
|
|
|
|
public void SoftDelete()
|
|
{
|
|
IsDeleted = true;
|
|
DeletedAt = DateTime.UtcNow;
|
|
}
|
|
|
|
// ─── Inventory update methods ─────────────────────────────────
|
|
|
|
public void UpdateCpuInventory(CpuInventoryData data)
|
|
{
|
|
CpuVendor = data.Vendor;
|
|
CpuModel = data.Model;
|
|
CpuArchitecture = data.Architecture;
|
|
PhysicalCores = data.PhysicalCores;
|
|
LogicalCores = data.LogicalCores;
|
|
ThreadsPerCore = data.ThreadsPerCore;
|
|
CpuFrequencyMhz = data.FrequencyMhz;
|
|
CpuFlags = data.Flags;
|
|
CpuCacheBytes = data.CacheBytes;
|
|
ContainerCpuLimit = data.ContainerCpuLimit;
|
|
}
|
|
|
|
public void UpdateMemoryDiskInventory(MemoryDiskInventoryData data)
|
|
{
|
|
MemTotalBytes = data.MemTotalBytes;
|
|
MemAvailableBytes = data.MemAvailableBytes;
|
|
SwapTotalBytes = data.SwapTotalBytes;
|
|
SwapFreeBytes = data.SwapFreeBytes;
|
|
ContainerMemoryLimitBytes = data.ContainerMemoryLimitBytes;
|
|
DiskInventoryJson = data.DiskInventoryJson;
|
|
}
|
|
|
|
public void UpdateGpuInventory(string? gpuInventoryJson)
|
|
{
|
|
GpuInventoryJson = gpuInventoryJson;
|
|
}
|
|
|
|
public void MarkInventoryObserved(DateTime observedAtUtc)
|
|
{
|
|
if (observedAtUtc.Kind != DateTimeKind.Utc)
|
|
throw new ArgumentException("Inventory observation time must be UTC", nameof(observedAtUtc));
|
|
InventoryObservedAtUtc = observedAtUtc;
|
|
}
|
|
|
|
public void UpdateHostInventory(HostInventoryData data)
|
|
{
|
|
OsName = data.OsName;
|
|
KernelVersion = data.KernelVersion;
|
|
Arch = data.Arch;
|
|
UptimeMachineSeconds = data.UptimeMachineSeconds;
|
|
ContainerId = data.ContainerId;
|
|
ContainerImage = data.ContainerImage;
|
|
NetworkInterfacesJson = data.NetworkInterfacesJson;
|
|
TemperatureJson = data.TemperatureJson;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Groups CPU inventory fields to keep the update method under the parameter limit.
|
|
/// </summary>
|
|
public sealed record CpuInventoryData(
|
|
string? Vendor,
|
|
string? Model,
|
|
string? Architecture,
|
|
int? PhysicalCores,
|
|
int? LogicalCores,
|
|
int? ThreadsPerCore,
|
|
double? FrequencyMhz,
|
|
string? Flags,
|
|
long? CacheBytes,
|
|
long? ContainerCpuLimit);
|
|
|
|
/// <summary>
|
|
/// Groups memory and disk inventory fields.
|
|
/// </summary>
|
|
public sealed record MemoryDiskInventoryData(
|
|
long? MemTotalBytes,
|
|
long? MemAvailableBytes,
|
|
long? SwapTotalBytes,
|
|
long? SwapFreeBytes,
|
|
long? ContainerMemoryLimitBytes,
|
|
string? DiskInventoryJson);
|
|
|
|
/// <summary>
|
|
/// Groups host/container/network/temperature inventory fields.
|
|
/// </summary>
|
|
public sealed record HostInventoryData(
|
|
string? OsName,
|
|
string? KernelVersion,
|
|
string? Arch,
|
|
long? UptimeMachineSeconds,
|
|
string? ContainerId,
|
|
string? ContainerImage,
|
|
string? NetworkInterfacesJson,
|
|
string? TemperatureJson);
|