diff --git a/src/Heimdall.Web/Components/AgentCard.razor b/src/Heimdall.Web/Components/AgentCard.razor new file mode 100644 index 0000000..ee1e8fb --- /dev/null +++ b/src/Heimdall.Web/Components/AgentCard.razor @@ -0,0 +1,127 @@ +@* AgentCard.razor — carte agent avec jauges live CPU/RAM/Disque/Uptime. + Seuils : CPU/RAM > 90 % → warning, > 95 % → error. Hors ligne → jauges grisées. *@ +@using Heimdall.Web.Services +@inject NavigationManager Nav + + + @* ─── En-tête : nom + statut ─── *@ + + + + @Agent.Name + + + + + + @Agent.Hostname + @if (!string.IsNullOrEmpty(Agent.PublicIp)) { · @Agent.PublicIp } + + + @if (Agent.Status != "Online") + { + + + + @(Agent.Status == "Offline" ? "Agent hors ligne" : "Agent en erreur") + + @if (Agent.LastHeartbeat is { } hb) + { + + Dernier contact : @hb.ToLocalTime().ToString("g") + + } + + } + else + { + + @* CPU *@ + + + CPU + @FormatPercent(CpuPercent) + + + + @* RAM *@ + + + Mémoire + @FormatPercent(MemoryPercent) + + + + @* Disque *@ + + + Disque + @FormatPercent(DiskPercent) + + + + @* Uptime *@ + + Uptime : @FormatUptime() + + + } + + +@code { + [Parameter, EditorRequired] public AgentItem Agent { get; set; } = default!; + + 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.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 static Color GetStatusColor(string status) => status switch + { + "Online" => Color.Success, + "Offline" => Color.Error, + "Error" => Color.Warning, + _ => Color.Default + }; + + private static string GetStatusIcon(string status) => status switch + { + "Online" => Icons.Material.Filled.CheckCircle, + "Offline" => Icons.Material.Filled.Cancel, + "Error" => Icons.Material.Filled.Warning, + _ => Icons.Material.Filled.Help + }; + + private static string FormatStatus(string status) => status switch + { + "Online" => "En ligne", + "Offline" => "Hors ligne", + "Error" => "Erreur", + _ => status + }; + + private static string FormatPercent(double? v) => v is { } p ? $"{p:F0}%" : "—"; + + private static Color GetMetricColor(double? v) => v switch + { + > 95 => Color.Error, + > 90 => Color.Warning, + _ => Color.Success + }; + + 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"; + } +} diff --git a/src/Heimdall.Web/Components/Pages/Admin/AllAgentsPage.razor b/src/Heimdall.Web/Components/Pages/Admin/AllAgentsPage.razor index dc73e6a..7e01371 100644 --- a/src/Heimdall.Web/Components/Pages/Admin/AllAgentsPage.razor +++ b/src/Heimdall.Web/Components/Pages/Admin/AllAgentsPage.razor @@ -58,48 +58,16 @@ else } else { - - - Nom - Site - Hostname - Version - Statut - Dernier heartbeat - IP - Actions - - - - @context.Name - - - @context.SiteName - - @context.Hostname - @context.Version - - - - - @(context.LastHeartbeat?.ToString("g") ?? "Jamais") - - @context.PublicIp - - - - - - - - - - - - + + + @foreach (var agent in _agents) + { + + + + } + + @* ─── Pagination ─── *@ await LoadData(); + protected override async Task OnInitializedAsync() + { + await LoadData(); + await StartAutoRefresh(); + } + + // ─── Auto-refresh 15s (métriques live) ───────────────────────── + private CancellationTokenSource? _refreshCts; + private bool _disposed; + + private async Task StartAutoRefresh() + { + _refreshCts = new CancellationTokenSource(); + try + { + while (!_refreshCts.Token.IsCancellationRequested) + { + await Task.Delay(TimeSpan.FromSeconds(15), _refreshCts.Token); + if (_disposed) return; + try + { + var result = await Api.GetAllAgentsAsync(_page); + if (result is not null) + { + _agents = result.Items; + _total = result.TotalCount; + _totalPages = Math.Max(1, (int)Math.Ceiling(result.TotalCount / (double)result.PageSize)); + } + if (!_disposed) await InvokeAsync(StateHasChanged); + } + catch { /* keep last state */ } + } + } + catch (OperationCanceledException) { /* arrêt volontaire */ } + } private async Task LoadData() { @@ -159,19 +161,10 @@ else await LoadData(); } - private static Color GetStatusColor(string status) => status switch + public void Dispose() { - "Online" => Color.Success, - "Offline" => Color.Error, - "Error" => Color.Warning, - _ => Color.Default - }; - - private static string GetStatusIcon(string status) => status switch - { - "Online" => Icons.Material.Filled.CheckCircle, - "Offline" => Icons.Material.Filled.Cancel, - "Error" => Icons.Material.Filled.Warning, - _ => Icons.Material.Filled.Help - }; + _disposed = true; + _refreshCts?.Cancel(); + _refreshCts?.Dispose(); + } } diff --git a/src/Heimdall.Web/Components/Pages/Dashboard.razor b/src/Heimdall.Web/Components/Pages/Dashboard.razor index 7e22866..44d404a 100644 --- a/src/Heimdall.Web/Components/Pages/Dashboard.razor +++ b/src/Heimdall.Web/Components/Pages/Dashboard.razor @@ -161,33 +161,27 @@ @if (_agentsLoading) { - + + @for (var i = 0; i < 2; i++) + { + + + + + + } + } else if (_agents is { Count: > 0 }) { - - - Agent - Dernier heartbeat - Statut - GPU - - - @context.Name - @(context.LastHeartbeat?.ToLocalTime().ToString("g") ?? "—") - @context.Status - - @if (context.HasGpu) - { - - } - else - { - — - } - - - + + @foreach (var agent in _agents) + { + + + + } + Tous les agents → } else @@ -310,7 +304,43 @@ new("Désactivée", 0, Color.Default, 0), ]; - protected override async Task OnInitializedAsync() => await LoadData(); + protected override async Task OnInitializedAsync() + { + await LoadData(); + await StartAutoRefresh(); + } + + // ─── Auto-refresh 15s (métriques agents) ───────────────────────── + private CancellationTokenSource? _refreshCts; + private bool _disposed; + + private async Task StartAutoRefresh() + { + _refreshCts = new CancellationTokenSource(); + try + { + while (!_refreshCts.Token.IsCancellationRequested) + { + await Task.Delay(TimeSpan.FromSeconds(15), _refreshCts.Token); + if (_disposed) return; + // Rafraîchit uniquement la section agents (léger) : les stats + // globales restent au refresh manuel pour éviter le flash. + try + { + _agentsLoading = true; + var agentsPage = await Api.GetAllAgentsAsync(1, 5); + _agents = agentsPage?.Items?.ToList(); + } + catch { /* keep last state */ } + finally + { + _agentsLoading = false; + if (!_disposed) await InvokeAsync(StateHasChanged); + } + } + } + catch (OperationCanceledException) { /* arrêt volontaire */ } + } private async Task LoadData() { @@ -377,4 +407,11 @@ }; private record CameraStatusEntry(string Label, int Count, Color Color, int Percent); + + public void Dispose() + { + _disposed = true; + _refreshCts?.Cancel(); + _refreshCts?.Dispose(); + } } diff --git a/tests/Heimdall.Web.Tests/Components/AgentCardTests.cs b/tests/Heimdall.Web.Tests/Components/AgentCardTests.cs new file mode 100644 index 0000000..0c5d605 --- /dev/null +++ b/tests/Heimdall.Web.Tests/Components/AgentCardTests.cs @@ -0,0 +1,66 @@ +using Bunit; +using Heimdall.Web.Components; +using Heimdall.Web.Services; +using Heimdall.Web.Tests.Components.Pages; +using Shouldly; + +namespace Heimdall.Web.Tests.Components; + +public class AgentCardTests : PageTestBase +{ + private static AgentItem Agent(string status = "Online", double? cpu = 35, double? disk = 50, long? memBytes = null) => new() + { + Id = Guid.NewGuid(), + SiteId = Guid.NewGuid(), + SiteName = "Site Alpha", + Name = "Agent-1", + Hostname = "agent-1", + Version = "1.2.3", + Status = status, + LastHeartbeat = DateTime.UtcNow.AddMinutes(-1), + PublicIp = "10.0.0.5", + CpuCores = 4, + MemoryMb = 8192, + CpuUsagePercent = cpu, + DiskUsagePercent = disk, + MemoryUsageBytes = memBytes ?? 4096L * 1024 * 1024, + UptimeSeconds = 90000 + }; + + [Fact] + public void AgentCard_AfficheJaugesEtUptime() + { + var cut = Render(p => p.Add(x => x.Agent, Agent())); + + cut.Markup.ShouldContain("Agent-1"); + cut.Markup.ShouldContain("agent-1"); + cut.Markup.ShouldContain("En ligne"); + cut.Markup.ShouldContain("CPU"); + cut.Markup.ShouldContain("Mémoire"); + cut.Markup.ShouldContain("Disque"); + cut.Markup.ShouldContain("Uptime"); + // 35% CPU affiché + cut.Markup.ShouldContain("35%"); + } + + [Fact] + public void AgentCard_HorsLigne_AfficheEtatSansJauges() + { + var cut = Render(p => p.Add(x => x.Agent, Agent("Offline"))); + + cut.Markup.ShouldContain("Hors ligne"); + cut.Markup.ShouldContain("Agent hors ligne"); + cut.Markup.ShouldNotContain("Uptime"); + } + + [Fact] + public void AgentCard_CpuEleve_StatutAlerte() + { + // CPU 96% → couleur error (progress bar rouge) + var cut = Render(p => p.Add(x => x.Agent, Agent(cpu: 96))); + + cut.Markup.ShouldContain("96%"); + // La progress bar porte la classe de couleur erreur + cut.Markup.ShouldContain("mud-progress-linear"); + } +} diff --git a/tests/Heimdall.Web.Tests/Components/Pages/AllAgentsPageTests.cs b/tests/Heimdall.Web.Tests/Components/Pages/AllAgentsPageTests.cs index 39268dc..c770f16 100644 --- a/tests/Heimdall.Web.Tests/Components/Pages/AllAgentsPageTests.cs +++ b/tests/Heimdall.Web.Tests/Components/Pages/AllAgentsPageTests.cs @@ -15,7 +15,7 @@ public class AllAgentsPageTests : PageTestBase private static AgentItem Agent(string name = "Agent-1", string siteName = "Site Alpha") => new() { - Id = AgentId, + Id = Guid.NewGuid(), SiteId = SiteId, SiteName = siteName, Name = name, @@ -47,10 +47,11 @@ public class AllAgentsPageTests : PageTestBase { cut.Markup.ShouldContain("Agent-1"); cut.Markup.ShouldContain("Agent-2"); - cut.Markup.ShouldContain("Site Alpha"); - cut.Markup.ShouldContain("Site Beta"); - cut.Markup.ShouldContain("1.2.3"); - cut.Markup.ShouldContain("Online"); + cut.Markup.ShouldContain("agent-1"); + cut.Markup.ShouldContain("10.0.0.5"); + // Statut traduit sur la carte (StatusChip) + cut.Markup.ShouldContain("En ligne"); + // KPI cards cut.Markup.ShouldContain("Agents"); cut.Markup.ShouldContain("En ligne"); });