Files
Heimdall/src/Heimdall.Web/Components/Pages/Dashboard.razor
T

400 lines
16 KiB
Plaintext
Raw Normal View History

@page "/"
@page "/dashboard"
@attribute [Authorize]
@layout MainLayout
@inject HeimdallApiClient Api
@inject AuthState Auth
@inject ISnackbar Snackbar
<MudText Typo="Typo.h1" Class="mb-1">
Dashboard
</MudText>
<MudText Typo="Typo.body1" Class="mb-2">
Bienvenue, <strong>@Auth.User?.DisplayName</strong>
</MudText>
<MudText Typo="Typo.body2" Color="Color.Secondary" Class="mb-6">
<MudIcon Icon="@Icons.Material.Filled.Update" Size="Size.Small" Class="mr-1" />
Dernière mise à jour : @_lastRefresh.ToString("T")
</MudText>
@* Stats Cards Row — tuiles uniformes (StatTile, hauteur 100%) *@
<MudGrid>
<MudItem xs="12" sm="6" lg="3" Class="d-flex">
<StatTile Label="Caméras"
Value="@(_stats?.TotalCameras.ToString() ?? "—")"
Icon="@Icons.Material.Filled.Videocam"
IconColor="Color.Primary"
Loading="@_loading">
<Footer>
<MudText Typo="Typo.caption" Color="Color.Success">@(_stats?.CamerasOnline ?? 0) en ligne</MudText>
<MudText Typo="Typo.caption" Color="Color.Error">@(_stats?.CamerasOffline ?? 0) hors ligne</MudText>
</Footer>
</StatTile>
</MudItem>
<MudItem xs="12" sm="6" lg="3" Class="d-flex">
<StatTile Label="Agents en ligne"
Value="@(_stats?.OnlineAgents.ToString() ?? "—")"
Icon="@Icons.Material.Filled.Memory"
IconColor="Color.Success"
Loading="@_loading">
<Footer>
<MudText Typo="Typo.caption" Color="Color.Secondary">@(_stats?.TotalAgents ?? 0) total</MudText>
</Footer>
</StatTile>
</MudItem>
<MudItem xs="12" sm="6" lg="3" Class="d-flex">
<StatTile Label="Événements (24h)"
Value="@(_eventCount24h.ToString())"
Icon="@Icons.Material.Filled.Notifications"
IconColor="Color.Warning"
Loading="@_loading">
<Footer>
<MudLink Href="/events" Typo="Typo.caption">Voir tous →</MudLink>
</Footer>
</StatTile>
</MudItem>
<MudItem xs="12" sm="6" lg="3" Class="d-flex">
<StatTile Label="Sites"
Value="@(_stats?.TotalSites.ToString() ?? "—")"
Icon="@Icons.Material.Filled.Assignment"
IconColor="Color.Info"
Loading="@_loading">
<Footer>
<MudLink Href="/sites" Typo="Typo.caption">Gérer →</MudLink>
</Footer>
</StatTile>
</MudItem>
</MudGrid>
@* Camera Status + Recent Events Row *@
<MudGrid Class="mt-4">
@* Camera Status Breakdown *@
<MudItem xs="12" md="6">
<SectionCard Title="État des caméras" Icon="@Icons.Material.Filled.Videocam">
@if (_cameraStatusData is not null)
{
<MudTable Items="@_cameraStatusData" Hover="false" Elevation="0">
<HeaderContent>
<MudTh>Statut</MudTh>
<MudTh>Nombre</MudTh>
<MudTh></MudTh>
</HeaderContent>
<RowTemplate>
<MudTd>
<MudText Color="@context.Color">@context.Label</MudText>
</MudTd>
<MudTd>
<MudText Typo="Typo.h6">@context.Count</MudText>
</MudTd>
<MudTd>
<MudProgressLinear Value="@context.Percent" Color="@context.Color" style="width:100px" />
</MudTd>
</RowTemplate>
</MudTable>
}
else { <MudSkeleton Width="100%" Height="100px" /> }
</SectionCard>
</MudItem>
@* Recent Events *@
<MudItem xs="12" md="6">
<SectionCard Title="Derniers événements" Icon="@Icons.Material.Filled.Notifications">
<ChildContent>
@if (_recentEventsLoading)
{
<MudSkeleton Width="100%" Height="40px" />
<MudSkeleton Width="100%" Height="40px" Class="mt-1" />
}
else if (_recentEvents is { Count: > 0 })
{
<MudTable Items="@_recentEvents" Hover="true" Elevation="0">
<HeaderContent>
<MudTh>Heure</MudTh>
<MudTh>Type</MudTh>
<MudTh>Confiance</MudTh>
</HeaderContent>
<RowTemplate>
<MudTd><MudText Typo="Typo.body2">@context.OccurredAt.ToLocalTime().ToString("HH:mm")</MudText></MudTd>
<MudTd><MudText Color="@GetEventColor(context.Type)">@FormatEventType(context.Type)</MudText></MudTd>
<MudTd><MudText>@($"{context.Confidence:P0}")</MudText></MudTd>
</RowTemplate>
</MudTable>
}
else
{
<MudText Typo="Typo.body2" Color="Color.Secondary">Aucun événement récent.</MudText>
}
</ChildContent>
<Actions>
<MudLink Href="/events" Typo="Typo.caption">Voir tout →</MudLink>
</Actions>
</SectionCard>
</MudItem>
</MudGrid>
@* Agent Workload + Quick Actions *@
<MudGrid Class="mt-4">
@* Agent Workload *@
<MudItem xs="12" md="6">
<SectionCard Title="Charge des agents" Icon="@Icons.Material.Filled.Memory">
@if (_agentsLoading)
{
<MudGrid>
@for (var i = 0; i < 2; i++)
{
<MudItem xs="12" sm="6">
<MudPaper Elevation="0" Style="border-radius: var(--hmd-radius-md);">
<MudSkeleton Variant="SkeletonVariant.Rectangular" Height="140px" />
</MudPaper>
</MudItem>
}
</MudGrid>
}
else if (_agents is { Count: > 0 })
{
<MudGrid>
@foreach (var agent in _agents)
{
<MudItem xs="12" sm="6" @key="agent.Id">
<AgentCard Agent="@agent" />
</MudItem>
}
</MudGrid>
<MudLink Href="/admin/agents" Typo="Typo.caption" Class="mt-2 d-block">Tous les agents →</MudLink>
}
else
{
<MudText Typo="Typo.body2" Color="Color.Secondary">Aucun agent enregistré.</MudText>
}
</SectionCard>
</MudItem>
@* Quick Actions *@
<MudItem xs="12" md="6">
<SectionCard Title="Actions rapides" Icon="@Icons.Material.Filled.FlashOn">
<MudGrid>
<MudItem xs="6">
<MudButton FullWidth="true" Variant="Variant.Outlined" Color="Color.Primary" Href="/events" Class="mb-4" Style="height:80px;">
<div class="d-flex flex-column align-center">
<MudIcon Icon="@Icons.Material.Filled.Notifications" />
<MudText Typo="Typo.body2">Événements</MudText>
</div>
</MudButton>
</MudItem>
<MudItem xs="6">
<MudButton FullWidth="true" Variant="Variant.Outlined" Color="Color.Secondary" Href="/sites" Class="mb-4" Style="height:80px;">
<div class="d-flex flex-column align-center">
<MudIcon Icon="@Icons.Material.Filled.Add" />
<MudText Typo="Typo.body2">Nouveau site</MudText>
</div>
</MudButton>
</MudItem>
<MudItem xs="6">
<MudButton FullWidth="true" Variant="Variant.Outlined" Color="Color.Warning" Href="/admin/agents" Class="mb-4" Style="height:80px;">
<div class="d-flex flex-column align-center">
<MudIcon Icon="@Icons.Material.Filled.Memory" />
<MudText Typo="Typo.body2">Agents</MudText>
</div>
</MudButton>
</MudItem>
<MudItem xs="6">
<MudButton FullWidth="true" Variant="Variant.Outlined" Color="Color.Info" Href="/audit-logs" Class="mb-4" Style="height:80px;">
<div class="d-flex flex-column align-center">
<MudIcon Icon="@Icons.Material.Filled.History" />
<MudText Typo="Typo.body2">Audit</MudText>
</div>
</MudButton>
</MudItem>
</MudGrid>
</SectionCard>
</MudItem>
</MudGrid>
@* Sites Table *@
<MudGrid Class="mt-4">
<MudItem xs="12">
<SectionCard Title="Sites" Icon="@Icons.Material.Filled.Business">
<ChildContent>
@if (_loading) { for (int i = 0; i < 3; i++) { <MudSkeleton Width="100%" Height="40px" Class="mt-1" /> } }
else if (_sites?.Count > 0)
{
<MudTable Items="@_sites" Hover="true" Striped="true" Elevation="0">
<HeaderContent>
<MudTh>Nom</MudTh>
<MudTh>Adresse</MudTh>
<MudTh>Statut</MudTh>
<MudTh>Créé le</MudTh>
</HeaderContent>
<RowTemplate>
<MudTd><MudLink Href="@($"/sites/{context.Id}")">@context.Name</MudLink></MudTd>
<MudTd>@(context.Address ?? "—")</MudTd>
<MudTd><MudText Color="@(context.Enabled ? Color.Success : Color.Default)">@(context.Enabled ? "Actif" : "Inactif")</MudText></MudTd>
<MudTd>@context.CreatedAt.ToString("g")</MudTd>
</RowTemplate>
</MudTable>
}
else
{
<div class="pa-8 text-center">
<MudIcon Icon="@Icons.Material.Filled.Business" Size="Size.Large" Color="Color.Secondary" />
<MudText Typo="Typo.h6" Color="Color.Secondary" Class="mt-4">Aucun site configuré</MudText>
<MudText Typo="Typo.body2" Color="Color.Secondary" Class="mb-4">
Commencez par créer un site pour déployer vos agents de surveillance.
</MudText>
<MudButton Variant="Variant.Filled" Color="Color.Primary" Href="/sites">
<MudIcon Icon="@Icons.Material.Filled.Add" Class="mr-1" /> Créer un site
</MudButton>
</div>
}
</ChildContent>
<Actions>
<MudTooltip Text="Rafraîchir">
<MudIconButton Icon="@Icons.Material.Filled.Refresh" Size="Size.Small" OnClick="@(async () => await LoadData())" aria-label="Actualiser le tableau de bord" />
</MudTooltip>
</Actions>
</SectionCard>
</MudItem>
</MudGrid>
@code {
private StatsResult? _stats;
private IReadOnlyList<SiteItem>? _sites;
private bool _loading = true;
private DateTime _lastRefresh;
private bool _recentEventsLoading;
private bool _agentsLoading;
private List<EventItem>? _recentEvents;
private int _eventCount24h;
private List<AgentItem>? _agents;
private List<CameraStatusEntry>? _cameraStatusData;
private readonly List<CameraStatusEntry> _defaultCameraStatus =
[
new("En ligne", 0, Color.Success, 0),
new("Hors ligne", 0, Color.Error, 0),
new("Désactivée", 0, Color.Default, 0),
];
protected override async Task OnInitializedAsync()
{
await LoadData();
}
// ─── Auto-refresh 15s (métriques agents) ─────────────────────────
// Démarre APRÈS le premier rendu (OnAfterRenderAsync) : une boucle
// infinie awaitée dans OnInitializedAsync bloque le rendu initial
// (SSR/prerender attend la fin du cycle de vie) → page lente.
private CancellationTokenSource? _refreshCts;
private bool _disposed;
private bool _autoRefreshStarted;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender && !_autoRefreshStarted)
{
_autoRefreshStarted = true;
await StartAutoRefresh();
}
}
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()
{
_loading = true;
try
{
// Stats
_stats = await Api.GetStatsAsync();
if (_stats is not null)
{
var total = _stats.CamerasOnline + _stats.CamerasOffline + (_stats.TotalCameras - _stats.CamerasOnline - _stats.CamerasOffline);
total = total > 0 ? total : 1;
_cameraStatusData =
[
new("En ligne", _stats.CamerasOnline, Color.Success, (int)(_stats.CamerasOnline * 100.0 / total)),
new("Hors ligne", _stats.CamerasOffline, Color.Error, (int)(_stats.CamerasOffline * 100.0 / total)),
new("Autre", _stats.TotalCameras - _stats.CamerasOnline - _stats.CamerasOffline, Color.Default, (int)((_stats.TotalCameras - _stats.CamerasOnline - _stats.CamerasOffline) * 100.0 / total)),
];
}
2026-07-17 17:22:33 +00:00
// Sites
var sitesResult = await Api.GetSitesAsync(1, 5);
_sites = sitesResult?.Items;
// Agents
try { _agentsLoading = true; var agentsPage = await Api.GetAllAgentsAsync(1, 5); _agents = agentsPage?.Items?.ToList(); }
catch { }
finally { _agentsLoading = false; }
// Recent events
try
{
_recentEventsLoading = true;
var eventsResult = await Api.GetEventsAsync(null, null, 1, 10);
_recentEvents = eventsResult?.Items ?? [];
_eventCount24h = eventsResult?.TotalCount ?? 0;
}
catch { }
finally { _recentEventsLoading = false; }
_lastRefresh = DateTime.Now;
}
catch { Snackbar.Add(UiErrors.OperationFailed(), Severity.Error); }
finally { _loading = false; }
}
private static string FormatEventType(string type) => type switch
{
"MotionDetected" => "Mouvement",
"PersonDetected" => "Personne",
"VehicleDetected" => "Véhicule",
"FaceRecognized" => "Visage",
"CameraOffline" => "Hors ligne",
_ => type
};
private static Color GetEventColor(string type) => type switch
{
"MotionDetected" => Color.Warning,
"PersonDetected" => Color.Error,
"VehicleDetected" => Color.Info,
"FaceRecognized" => Color.Success,
_ => Color.Default
};
private record CameraStatusEntry(string Label, int Count, Color Color, int Percent);
public void Dispose()
{
_disposed = true;
_refreshCts?.Cancel();
_refreshCts?.Dispose();
}
}