260 lines
9.2 KiB
Plaintext
260 lines
9.2 KiB
Plaintext
@page "/admin/agents"
|
|
@attribute [Authorize(Policy = "Admin.Users")]
|
|
@layout MainLayout
|
|
@inject HeimdallApiClient Api
|
|
@inject ISnackbar Snackbar
|
|
|
|
<MudText Typo="Typo.h1" Class="mb-6">
|
|
<i class="fas fa-microchip mr-2"></i>
|
|
Agents
|
|
</MudText>
|
|
|
|
@if (_loading)
|
|
{
|
|
<LoadingSkeleton LineCount="6" />
|
|
}
|
|
else if (_error)
|
|
{
|
|
<MudAlert Severity="Severity.Error" Class="mb-4" Variant="Variant.Filled">
|
|
<MudText>Erreur lors du chargement des agents.</MudText>
|
|
<MudButton Variant="Variant.Text" Color="Color.Inherit" OnClick="@LoadData" Class="mt-2">
|
|
<MudIcon Icon="@Icons.Material.Filled.Refresh" Class="mr-1" Size="Size.Small" /> Réessayer
|
|
</MudButton>
|
|
</MudAlert>
|
|
}
|
|
else
|
|
{
|
|
@* ─── KPI Cards (StatTile, uniformes avec le Dashboard) ─── *@
|
|
<MudGrid Class="mb-4">
|
|
@foreach (var stat in _agentStats)
|
|
{
|
|
<MudItem xs="12" sm="6" md="3" @key="stat.Label" Class="d-flex">
|
|
<StatTile Label="@stat.Label"
|
|
Value="@stat.Value.ToString()"
|
|
Icon="@GetStatIcon(stat.StatType)"
|
|
IconColor="Color.Secondary"
|
|
ValueStyle="@($"color:{stat.Color}")" />
|
|
</MudItem>
|
|
}
|
|
</MudGrid>
|
|
|
|
@* ─── Filter Bar ─── *@
|
|
<FilterBar SearchPlaceholder="Rechercher un agent par nom, hostname ou IP..."
|
|
OnSearch="@(async () => { _page = 1; await LoadData(); })"
|
|
SearchChanged="@(async (s) => { _search = s; _page = 1; await LoadData(); })"
|
|
Reset="@(async () => { _search = ""; _page = 1; await LoadData(); })" />
|
|
|
|
@if (_agents is null || _agents.Count == 0)
|
|
{
|
|
<EmptyState Icon="@Icons.Material.Filled.Memory"
|
|
Title="Aucun agent déployé"
|
|
Description="Aucun agent n'est actuellement enregistré sur aucun site. Créez un site et générez un token d'enregistrement pour connecter un agent.">
|
|
<ActionContent>
|
|
<MudButton Variant="Variant.Outlined" Color="Color.Primary"
|
|
OnClick="@(() => Nav.NavigateTo("/sites"))">
|
|
<MudIcon Icon="@Icons.Material.Filled.Business" Class="mr-1" /> Voir les sites
|
|
</MudButton>
|
|
</ActionContent>
|
|
</EmptyState>
|
|
}
|
|
else
|
|
{
|
|
<MudContainer MaxWidth="MaxWidth.False" Class="px-0">
|
|
<MudGrid>
|
|
@foreach (var agent in _agents)
|
|
{
|
|
<MudItem xs="12" sm="6" md="4" xl="3" @key="agent.Id">
|
|
<div class="position-relative">
|
|
<AgentCard Agent="@agent" />
|
|
<div class="d-flex justify-end pa-1" style="margin-top:-8px;">
|
|
<MudTooltip Text="Supprimer cet agent">
|
|
<MudIconButton Icon="@Icons.Material.Filled.Delete" Size="Size.Small"
|
|
Color="Color.Error" aria-label="Supprimer l'agent"
|
|
OnClick="@(() => ConfirmDeleteAgent(agent.Id, agent.Name))" />
|
|
</MudTooltip>
|
|
</div>
|
|
</div>
|
|
</MudItem>
|
|
}
|
|
</MudGrid>
|
|
</MudContainer>
|
|
|
|
@* ─── Pagination ─── *@
|
|
<PaginationBar Page="@_page"
|
|
TotalPages="@_totalPages"
|
|
Total="@_total"
|
|
Previous="@(async () => await GoToPage(_page - 1))"
|
|
Next="@(async () => await GoToPage(_page + 1))" />
|
|
}
|
|
}
|
|
|
|
@* ─── Delete Confirmation Dialog ─── *@
|
|
<MudOverlay Visible="@_confirmDeleteShow" OnClick="@(() => _confirmDeleteShow = false)" />
|
|
<MudDialog @bind-Visible="_confirmDeleteShow">
|
|
<TitleContent>
|
|
<MudText Typo="Typo.h6">
|
|
<MudIcon Icon="@Icons.Material.Filled.Warning" Color="Color.Warning" Class="mr-2" />
|
|
Confirmer la suppression
|
|
</MudText>
|
|
</TitleContent>
|
|
<DialogContent>
|
|
<MudText>Supprimer l'agent <strong>@_confirmDeleteName</strong> ?</MudText>
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary" Class="mt-2">
|
|
Les caméras associées seront désassignées mais pas supprimées.
|
|
</MudText>
|
|
</DialogContent>
|
|
<DialogActions>
|
|
<MudButton Variant="Variant.Text" OnClick="@(() => _confirmDeleteShow = false)">Annuler</MudButton>
|
|
<MudButton Variant="Variant.Filled" Color="Color.Error"
|
|
OnClick="@ExecuteDeleteAgentAsync" Loading="@_deleting" Disabled="@_deleting">
|
|
Supprimer
|
|
</MudButton>
|
|
</DialogActions>
|
|
</MudDialog>
|
|
|
|
@code {
|
|
private IReadOnlyList<AgentItem>? _agents;
|
|
private List<AgentStatItem> _agentStats = [];
|
|
private bool _loading = true;
|
|
private bool _error;
|
|
private int _page = 1;
|
|
private int _totalPages = 1;
|
|
private int _total;
|
|
private string _search = "";
|
|
|
|
// Delete agent
|
|
private bool _confirmDeleteShow;
|
|
private Guid _confirmDeleteId;
|
|
private string _confirmDeleteName = "";
|
|
private bool _deleting;
|
|
|
|
[Inject] private NavigationManager Nav { get; set; } = null!;
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
await LoadData();
|
|
}
|
|
|
|
// ─── Auto-refresh 15s (métriques live) ─────────────────────────
|
|
// 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;
|
|
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()
|
|
{
|
|
_loading = true;
|
|
_error = false;
|
|
try
|
|
{
|
|
var agentsTask = Api.GetAllAgentsAsync(_page);
|
|
var statsTask = Api.GetAgentStatsAsync();
|
|
|
|
await Task.WhenAll(agentsTask, statsTask);
|
|
|
|
var result = await agentsTask;
|
|
if (result is not null)
|
|
{
|
|
_agents = result.Items;
|
|
_total = result.TotalCount;
|
|
_totalPages = Math.Max(1, (int)Math.Ceiling(result.TotalCount / (double)result.PageSize));
|
|
}
|
|
|
|
_agentStats = await statsTask;
|
|
}
|
|
catch {
|
|
_error = true;
|
|
Snackbar.Add(UiErrors.OperationFailed(), Severity.Error);
|
|
}
|
|
finally { _loading = false; }
|
|
}
|
|
|
|
private async Task GoToPage(int p)
|
|
{
|
|
_page = p;
|
|
await LoadData();
|
|
}
|
|
|
|
private static string GetStatIcon(string statType) => statType switch
|
|
{
|
|
"AgentsOnline" => Icons.Material.Filled.CheckCircle,
|
|
"AgentsOffline" => Icons.Material.Filled.Cancel,
|
|
"AgentsError" => Icons.Material.Filled.Warning,
|
|
_ => Icons.Material.Filled.Memory
|
|
};
|
|
|
|
public void Dispose()
|
|
{
|
|
_disposed = true;
|
|
_refreshCts?.Cancel();
|
|
_refreshCts?.Dispose();
|
|
}
|
|
|
|
// ─── Delete Agent ─────────────────────────────────────────────
|
|
|
|
private void ConfirmDeleteAgent(Guid id, string name)
|
|
{
|
|
_confirmDeleteId = id;
|
|
_confirmDeleteName = name;
|
|
_confirmDeleteShow = true;
|
|
}
|
|
|
|
private async Task ExecuteDeleteAgentAsync()
|
|
{
|
|
_deleting = true;
|
|
try
|
|
{
|
|
_confirmDeleteShow = false;
|
|
var error = await Api.DeleteAgentAsync(_confirmDeleteId);
|
|
if (error is null)
|
|
{
|
|
Snackbar.Add($"Agent \"{_confirmDeleteName}\" supprimé", Severity.Success);
|
|
await LoadData();
|
|
}
|
|
else
|
|
{
|
|
Snackbar.Add(error, Severity.Error);
|
|
}
|
|
}
|
|
catch { Snackbar.Add(UiErrors.OperationFailed(), Severity.Error); }
|
|
finally { _deleting = false; }
|
|
}
|
|
}
|