From 6606cf9dd51eda4355535b7a99ffed4656256bc3 Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Mon, 10 Aug 2026 15:16:09 +0200 Subject: [PATCH] =?UTF-8?q?feat(ui):=20affiche=20les=20m=C3=A9triques=20li?= =?UTF-8?q?ve=20des=20agents=20(CPU,=20RAM,=20disque,=20uptime)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - AgentResponse expose CpuUsagePercent/MemoryUsageBytes/DiskUsagePercent/ UptimeSeconds (mappés dans les 3 handlers GetAgent/GetAll/GetBySite) - AgentItem client complété ; nouvelles tuiles AgentMetrics + MetricTile (jauges colorées) intégrées en ChildRowContent dans AllAgentsPage --- .../Agents/Queries/GetAgent/GetAgentQuery.cs | 3 +- .../GetAgentsBySite/GetAgentsBySiteQuery.cs | 3 +- .../Queries/GetAllAgents/GetAllAgentsQuery.cs | 3 +- src/Heimdall.Application/DTOs/AgentDto.cs | 6 +- .../Components/AgentMetrics.razor | 73 +++++++++++++++++++ src/Heimdall.Web/Components/MetricTile.razor | 39 ++++++++++ .../Pages/Admin/AllAgentsPage.razor | 5 ++ .../Services/HeimdallApiClient.cs | 4 + 8 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 src/Heimdall.Web/Components/AgentMetrics.razor create mode 100644 src/Heimdall.Web/Components/MetricTile.razor diff --git a/src/Heimdall.Application/Agents/Queries/GetAgent/GetAgentQuery.cs b/src/Heimdall.Application/Agents/Queries/GetAgent/GetAgentQuery.cs index 061cb21..8b97306 100644 --- a/src/Heimdall.Application/Agents/Queries/GetAgent/GetAgentQuery.cs +++ b/src/Heimdall.Application/Agents/Queries/GetAgent/GetAgentQuery.cs @@ -27,5 +27,6 @@ public class GetAgentHandler : IRequestHandler new( a.Id, a.SiteId, a.Site?.Name ?? string.Empty, a.Name, a.Hostname, a.Version, a.Status, a.LastHeartbeat, a.PublicIp, a.HasGpu, a.CpuCores, a.MemoryMb, - a.GpuType.ToString(), a.CreatedAt); + a.GpuType.ToString(), a.CreatedAt, + a.CpuUsagePercent, a.MemoryUsageBytes, a.DiskUsagePercent, a.UptimeSeconds); } diff --git a/src/Heimdall.Application/Agents/Queries/GetAgentsBySite/GetAgentsBySiteQuery.cs b/src/Heimdall.Application/Agents/Queries/GetAgentsBySite/GetAgentsBySiteQuery.cs index f24af16..00921b0 100644 --- a/src/Heimdall.Application/Agents/Queries/GetAgentsBySite/GetAgentsBySiteQuery.cs +++ b/src/Heimdall.Application/Agents/Queries/GetAgentsBySite/GetAgentsBySiteQuery.cs @@ -37,5 +37,6 @@ public class GetAgentsBySiteHandler : IRequestHandler new( a.Id, a.SiteId, a.Site?.Name ?? string.Empty, a.Name, a.Hostname, a.Version, a.Status, a.LastHeartbeat, a.PublicIp, a.HasGpu, a.CpuCores, a.MemoryMb, - a.GpuType.ToString(), a.CreatedAt); + a.GpuType.ToString(), a.CreatedAt, + a.CpuUsagePercent, a.MemoryUsageBytes, a.DiskUsagePercent, a.UptimeSeconds); } diff --git a/src/Heimdall.Application/Agents/Queries/GetAllAgents/GetAllAgentsQuery.cs b/src/Heimdall.Application/Agents/Queries/GetAllAgents/GetAllAgentsQuery.cs index f6b463f..0cd6faf 100644 --- a/src/Heimdall.Application/Agents/Queries/GetAllAgents/GetAllAgentsQuery.cs +++ b/src/Heimdall.Application/Agents/Queries/GetAllAgents/GetAllAgentsQuery.cs @@ -33,5 +33,6 @@ public class GetAllAgentsHandler : IRequestHandler new( a.Id, a.SiteId, a.Site?.Name ?? string.Empty, a.Name, a.Hostname, a.Version, a.Status, a.LastHeartbeat, a.PublicIp, a.HasGpu, a.CpuCores, a.MemoryMb, - a.GpuType.ToString(), a.CreatedAt); + a.GpuType.ToString(), a.CreatedAt, + a.CpuUsagePercent, a.MemoryUsageBytes, a.DiskUsagePercent, a.UptimeSeconds); } diff --git a/src/Heimdall.Application/DTOs/AgentDto.cs b/src/Heimdall.Application/DTOs/AgentDto.cs index e035da3..052f9c3 100644 --- a/src/Heimdall.Application/DTOs/AgentDto.cs +++ b/src/Heimdall.Application/DTOs/AgentDto.cs @@ -34,7 +34,11 @@ public record AgentResponse( int CpuCores, long MemoryMb, string GpuType, - DateTime CreatedAt); + DateTime CreatedAt, + double? CpuUsagePercent = null, + long? MemoryUsageBytes = null, + double? DiskUsagePercent = null, + long? UptimeSeconds = null); public record RegisterAgentResponse( Guid AgentId, diff --git a/src/Heimdall.Web/Components/AgentMetrics.razor b/src/Heimdall.Web/Components/AgentMetrics.razor new file mode 100644 index 0000000..de2aa6b --- /dev/null +++ b/src/Heimdall.Web/Components/AgentMetrics.razor @@ -0,0 +1,73 @@ +@* AgentMetrics.razor — jauge CPU / RAM / disque + uptime d'un agent. + Affiche les métriques live remontées par le heartbeat (nullable). *@ +@using Heimdall.Web.Services + +@if (Agent is null) +{ + Aucune métrique disponible. + return; +} + + + + + + + + + + + + + + + + +@code { + /// Agent whose live metrics are shown. + [Parameter, EditorRequired] + public AgentItem? Agent { get; set; } + + private double? CpuPercent => Agent?.CpuUsagePercent is { } c ? Math.Clamp(c, 0, 100) : null; + private double? DiskPercent => Agent?.DiskUsagePercent is { } d ? Math.Clamp(d, 0, 100) : null; + + private double? MemoryPercent + { + get + { + if (Agent is null || Agent.MemoryMb <= 0) return null; + var usedMb = Agent.MemoryUsageBytes is { } b ? b / 1024.0 / 1024.0 : 0; + return Math.Clamp(usedMb / Agent.MemoryMb * 100, 0, 100); + } + } + + private string FormatCpu() + => Agent?.CpuUsagePercent is { } c ? $"{c:F1} %" : "—"; + + private string FormatMemory() + { + if (Agent is null) return "—"; + if (Agent.MemoryUsageBytes is { } b) + return $"{FormatMb(b)} / {Agent.MemoryMb} Mo"; + return $"{Agent.MemoryMb} Mo"; + } + + private string FormatDisk() + => Agent?.DiskUsagePercent is { } d ? $"{d:F1} %" : "—"; + + private string FormatUptime() + { + if (Agent?.UptimeSeconds is not { } s) return "—"; + var t = TimeSpan.FromSeconds(s); + return t.TotalDays >= 1 ? $"{(int)t.TotalDays}j {t.Hours}h" : $"{t.Hours}h {t.Minutes}min"; + } + + private static string FormatMb(long bytes) + => bytes >= 1024 * 1024 * 1024 ? $"{bytes / 1024.0 / 1024.0 / 1024.0:F1} Go" + : bytes >= 1024 * 1024 ? $"{bytes / 1024.0 / 1024.0:F0} Mo" + : $"{bytes / 1024.0:F0} Ko"; +} diff --git a/src/Heimdall.Web/Components/MetricTile.razor b/src/Heimdall.Web/Components/MetricTile.razor new file mode 100644 index 0000000..2966a3e --- /dev/null +++ b/src/Heimdall.Web/Components/MetricTile.razor @@ -0,0 +1,39 @@ +@* MetricTile.razor — tuile de métrique compacte (label + valeur + jauge optionnelle). *@ +@using MudBlazor + + + + @if (Icon is not null) + { + + } + @Label + + + @Value + + @if (Progress is { } p) + { + + } + + +@code { + [Parameter, EditorRequired] public string Label { get; set; } = ""; + [Parameter, EditorRequired] public string Value { get; set; } = "—"; + [Parameter] public Color Color { get; set; } = Color.Default; + [Parameter] public Color ProgressColor { get; set; } = Color.Primary; + [Parameter] public double? Progress { get; set; } + [Parameter] public string? Icon { get; set; } + + private static string ColorToCss(Color color) => color switch + { + Color.Primary => "var(--mud-palette-primary)", + Color.Secondary => "var(--mud-palette-secondary)", + Color.Info => "var(--mud-palette-info)", + Color.Success => "var(--mud-palette-success)", + Color.Warning => "var(--mud-palette-warning)", + Color.Error => "var(--mud-palette-error)", + _ => "var(--mud-palette-text-primary)", + }; +} diff --git a/src/Heimdall.Web/Components/Pages/Admin/AllAgentsPage.razor b/src/Heimdall.Web/Components/Pages/Admin/AllAgentsPage.razor index 929cdc5..ec3cad3 100644 --- a/src/Heimdall.Web/Components/Pages/Admin/AllAgentsPage.razor +++ b/src/Heimdall.Web/Components/Pages/Admin/AllAgentsPage.razor @@ -94,6 +94,11 @@ else + + + + + @* ─── Pagination ─── *@ diff --git a/src/Heimdall.Web/Services/HeimdallApiClient.cs b/src/Heimdall.Web/Services/HeimdallApiClient.cs index 51180e4..db896ec 100644 --- a/src/Heimdall.Web/Services/HeimdallApiClient.cs +++ b/src/Heimdall.Web/Services/HeimdallApiClient.cs @@ -524,6 +524,10 @@ public class AgentItem public long MemoryMb { get; set; } public string GpuType { get; set; } = "None"; public DateTime CreatedAt { get; set; } + public double? CpuUsagePercent { get; set; } + public long? MemoryUsageBytes { get; set; } + public double? DiskUsagePercent { get; set; } + public long? UptimeSeconds { get; set; } } // ─── Settings DTOs ────────────────────────────────────────────────