Files
Heimdall/src/Heimdall.Web/Components/AgentMetrics.razor
T

73 lines
2.6 KiB
Plaintext
Raw Normal View History

@* 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)
{
<MudText Typo="Typo.body2" Color="Color.Secondary">Aucune métrique disponible.</MudText>
return;
}
<MudGrid>
<MudItem xs="12" sm="6" md="3">
<MetricTile Label="CPU" Value="@FormatCpu()" Color="Color.Primary"
Progress="@CpuPercent" ProgressColor="Color.Primary" />
</MudItem>
<MudItem xs="12" sm="6" md="3">
<MetricTile Label="Mémoire" Value="@FormatMemory()" Color="Color.Info"
Progress="@MemoryPercent" ProgressColor="Color.Info" />
</MudItem>
<MudItem xs="12" sm="6" md="3">
<MetricTile Label="Disque" Value="@FormatDisk()" Color="Color.Warning"
Progress="@DiskPercent" ProgressColor="Color.Warning" />
</MudItem>
<MudItem xs="12" sm="6" md="3">
<MetricTile Label="Uptime" Value="@FormatUptime()" Color="Color.Success"
Icon="@Icons.Material.Filled.Schedule" />
</MudItem>
</MudGrid>
@code {
/// <summary>Agent whose live metrics are shown.</summary>
[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";
}