Files
Heimdall/src/Heimdall.Web/Components/Pages/Dashboard.razor
T
Arnaud Dartois 96af711eac
Heimdall Gitea / Build and tests (pull_request) Failing after 7m21s
Heimdall Gitea / Build and scan server image (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Skipped
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / Publish versioned release (pull_request) Skipped
feat(i18n): apply saved language to shell and profile
2026-10-25 04:19:19 +01:00

409 lines
17 KiB
Plaintext

@page "/"
@page "/dashboard"
@attribute [Authorize]
@layout MainLayout
@inject HeimdallApiClient Api
@inject AuthState Auth
@inject ISnackbar Snackbar
@inject UiLanguageService Language
@inject UiText Text
<MudText Typo="Typo.h1" Class="mb-1">
@Text["Dashboard"]
</MudText>
<MudText Typo="Typo.body1" Class="mb-2">
@Text["WelcomeUser"], <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" />
@Text["LastUpdated"] @_lastRefresh.ToString("T", Language.Culture)
</MudText>
@* Stats Cards Row — tuiles uniformes (StatTile, hauteur 100%) *@
<MudGrid>
<MudItem xs="12" sm="6" lg="3" Class="d-flex">
<StatTile Label="@Text["CameraCount"]"
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) @Text["Online"]</MudText>
<MudText Typo="Typo.caption" Color="Color.Error">@(_stats?.CamerasOffline ?? 0) @Text["Offline"]</MudText>
</Footer>
</StatTile>
</MudItem>
<MudItem xs="12" sm="6" lg="3" Class="d-flex">
<StatTile Label="@Text["OnlineAgents"]"
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) @Text["Total"]</MudText>
</Footer>
</StatTile>
</MudItem>
<MudItem xs="12" sm="6" lg="3" Class="d-flex">
<StatTile Label="@Text["Events24h"]"
Value="@(_eventCount24h.ToString())"
Icon="@Icons.Material.Filled.Notifications"
IconColor="Color.Warning"
Loading="@_loading">
<Footer>
<MudLink Href="/events" Typo="Typo.caption">@Text["ViewAll"] →</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">@Text["Manage"] →</MudLink>
</Footer>
</StatTile>
</MudItem>
</MudGrid>
@* Camera Status + Recent Events Row *@
<MudGrid Class="mt-4">
@* Camera Status Breakdown *@
<MudItem xs="12" md="6">
<SectionCard Title="@Text["CameraStatusSection"]" Icon="@Icons.Material.Filled.Videocam">
@if (_cameraStatusData is not null)
{
<MudTable Items="@_cameraStatusData" Hover="false" Elevation="0">
<HeaderContent>
<MudTh>@Text["Status"]</MudTh>
<MudTh>@Text["Count"]</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="@Text["RecentEvents"]" 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>@Text["Time"]</MudTh>
<MudTh>@Text["Type"]</MudTh>
<MudTh>@Text["Confidence"]</MudTh>
</HeaderContent>
<RowTemplate>
<MudTd><MudText Typo="Typo.body2">@context.OccurredAt.ToLocalTime().ToString("t", Language.Culture)</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">@Text["NoRecentEvents"]</MudText>
}
</ChildContent>
<Actions>
<MudLink Href="/events" Typo="Typo.caption">@Text["ViewAll"] →</MudLink>
</Actions>
</SectionCard>
</MudItem>
</MudGrid>
@* Agent Workload + Quick Actions *@
<MudGrid Class="mt-4">
@* Agent Workload *@
<MudItem xs="12" md="6">
<SectionCard Title="@Text["AgentLoad"]" 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">@Text["AllAgents"] →</MudLink>
}
else
{
<MudText Typo="Typo.body2" Color="Color.Secondary">@Text["NoAgentsRegistered"]</MudText>
}
</SectionCard>
</MudItem>
@* Quick Actions *@
<MudItem xs="12" md="6">
<SectionCard Title="@Text["QuickActions"]" 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">@Text["Events"]</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">@Text["NewSite"]</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">@Text["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">@Text["Audit"]</MudText>
</div>
</MudButton>
</MudItem>
</MudGrid>
</SectionCard>
</MudItem>
</MudGrid>
@* Sites Table *@
<MudGrid Class="mt-4">
<MudItem xs="12">
<SectionCard Title="@Text["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>@Text["Name"]</MudTh>
<MudTh>@Text["Address"]</MudTh>
<MudTh>@Text["Status"]</MudTh>
<MudTh>@Text["CreatedAt"]</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 ? Text["Active"] : Text["Inactive"])</MudText></MudTd>
<MudTd>@context.CreatedAt.ToString("g", Language.Culture)</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">@Text["NoSiteConfigured"]</MudText>
<MudText Typo="Typo.body2" Color="Color.Secondary" Class="mb-4">
@Text["CreateSitePrompt"]
</MudText>
<MudButton Variant="Variant.Filled" Color="Color.Primary" Href="/sites">
<MudIcon Icon="@Icons.Material.Filled.Add" Class="mr-1" /> @Text["CreateSite"]
</MudButton>
</div>
}
</ChildContent>
<Actions>
<MudTooltip Text="@Text["Refresh"]">
<MudIconButton Icon="@Icons.Material.Filled.Refresh" Size="Size.Small" OnClick="@(async () => await LoadData())" aria-label="@Text["RefreshDashboard"]" />
</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;
protected override async Task OnInitializedAsync()
{
Language.LanguageChanged += OnLanguageChanged;
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)
UpdateCameraStatusData();
// 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 string FormatEventType(string type) => type switch
{
"MotionDetected" => Text["MotionDetected"],
"PersonDetected" => Text["PersonDetected"],
"VehicleDetected" => Text["VehicleDetected"],
"FaceRecognized" => Text["FaceRecognized"],
"CameraOffline" => Text["CameraOffline"],
_ => 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);
private void UpdateCameraStatusData()
{
if (_stats is null) return;
var other = _stats.TotalCameras - _stats.CamerasOnline - _stats.CamerasOffline;
var total = Math.Max(_stats.TotalCameras, 1);
_cameraStatusData =
[
new(Text["Online"], _stats.CamerasOnline, Color.Success, (int)(_stats.CamerasOnline * 100.0 / total)),
new(Text["Offline"], _stats.CamerasOffline, Color.Error, (int)(_stats.CamerasOffline * 100.0 / total)),
new(Text["Other"], other, Color.Default, (int)(other * 100.0 / total)),
];
}
private void OnLanguageChanged()
{
UpdateCameraStatusData();
_ = InvokeAsync(StateHasChanged);
}
public void Dispose()
{
_disposed = true;
Language.LanguageChanged -= OnLanguageChanged;
_refreshCts?.Cancel();
_refreshCts?.Dispose();
}
}