@page "/" @page "/dashboard" @attribute [Authorize] @layout MainLayout @inject HeimdallApiClient Api @inject AuthState Auth @inject ISnackbar Snackbar Dashboard Bienvenue, @Auth.User?.DisplayName Dernière mise à jour : @_lastRefresh.ToString("T") @* Stats Cards Row — tuiles uniformes (StatTile, hauteur 100%) *@
@(_stats?.CamerasOnline ?? 0) en ligne @(_stats?.CamerasOffline ?? 0) hors ligne
@(_stats?.TotalAgents ?? 0) total
Voir tous →
@* Camera Status + Recent Events Row *@ @* Camera Status Breakdown *@ @if (_cameraStatusData is not null) { Statut Nombre @context.Label @context.Count } else { } @* Recent Events *@ @if (_recentEventsLoading) { } else if (_recentEvents is { Count: > 0 }) { Heure Type Confiance @context.OccurredAt.ToLocalTime().ToString("HH:mm") @FormatEventType(context.Type) @($"{context.Confidence:P0}") } else { Aucun événement récent. } Voir tout → @* Agent Workload + Quick Actions *@ @* Agent Workload *@ @if (_agentsLoading) { @for (var i = 0; i < 2; i++) { } } else if (_agents is { Count: > 0 }) { @foreach (var agent in _agents) { } Tous les agents → } else { Aucun agent enregistré. } @* Quick Actions *@
Événements
Nouveau site
Agents
Audit
@* Sites Table *@ @if (_loading) { for (int i = 0; i < 3; i++) { } } else if (_sites?.Count > 0) { Nom Adresse Statut Créé le @context.Name @(context.Address ?? "—") @(context.Enabled ? "Actif" : "Inactif") @context.CreatedAt.ToString("g") } else {
Aucun site configuré Commencez par créer un site pour déployer vos agents de surveillance. Créer un site
}
@code { private StatsResult? _stats; private IReadOnlyList? _sites; private bool _loading = true; private DateTime _lastRefresh; private bool _recentEventsLoading; private bool _agentsLoading; private List? _recentEvents; private int _eventCount24h; private List? _agents; private List? _cameraStatusData; private readonly List _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)), ]; } // 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(); } }