From 0042bf6abc09637f5ba5ddd3b80b29360ba8578f Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Tue, 20 Oct 2026 07:32:56 +0200 Subject: [PATCH] feat(gallery): add snapshot filters and paging --- docs/audits/2026-10-20-audit-complet.md | 4 ++ .../Queries/GetEvents/GetEventsHandler.cs | 6 ++- .../Queries/GetEvents/GetEventsQuery.cs | 3 +- .../Components/Pages/SnapshotsPage.razor | 54 ++++++++++++++----- .../Controllers/EventsController.cs | 3 +- .../Services/HeimdallApiClient.cs | 3 +- .../Handlers/Events/GetEventsHandlerTests.cs | 26 +++++++++ 7 files changed, 82 insertions(+), 17 deletions(-) diff --git a/docs/audits/2026-10-20-audit-complet.md b/docs/audits/2026-10-20-audit-complet.md index 9b32fc5..e34b90f 100644 --- a/docs/audits/2026-10-20-audit-complet.md +++ b/docs/audits/2026-10-20-audit-complet.md @@ -385,3 +385,7 @@ Ces liens correspondent aux fichiers inspectés ; les numéros désignent le dé | OPS-04 | [src/Heimdall.Infrastructure/BackgroundJobs/JobSetup.cs:64](../../src/Heimdall.Infrastructure/BackgroundJobs/JobSetup.cs#L64) | | OPS-05 | [src/Heimdall.Web/Controllers/UpdatesController.cs:59](../../src/Heimdall.Web/Controllers/UpdatesController.cs#L59) | | UI-01 | [src/Heimdall.Web/Components/Pages/SnapshotsPage.razor:44](../../src/Heimdall.Web/Components/Pages/SnapshotsPage.razor#L44) | + +## Suivi de correction — lot 4 (20 octobre 2026) + +La sécurité des chemins locaux (SEC-08) est déjà livrée dans la PR #2 fusionnée. Cette PR améliore la galerie : elle utilise les filtres snapshot, caméra/libellé et type côté API, avec pagination serveur, et conserve l’image originale en fallback si la miniature manque. Le build Web passe sans avertissement ni erreur et les tests ciblés du handler passent (4/4). La capture/lecture complète reste à réaliser ; son flux attend le contrôle d’accès livré dans la PR #2. diff --git a/src/Heimdall.Application/Events/Queries/GetEvents/GetEventsHandler.cs b/src/Heimdall.Application/Events/Queries/GetEvents/GetEventsHandler.cs index 476f85f..0250622 100644 --- a/src/Heimdall.Application/Events/Queries/GetEvents/GetEventsHandler.cs +++ b/src/Heimdall.Application/Events/Queries/GetEvents/GetEventsHandler.cs @@ -53,10 +53,14 @@ public class GetEventsHandler : IRequestHandler e.Confidence >= request.MinConfidence.Value); + if (request.HasSnapshot) + query = query.Where(e => e.SnapshotUrl != null); + if (!string.IsNullOrWhiteSpace(request.Search)) { var search = request.Search.Trim(); - query = query.Where(e => (e.Label ?? "").Contains(search, StringComparison.OrdinalIgnoreCase)); + query = query.Where(e => (e.Label ?? "").Contains(search, StringComparison.OrdinalIgnoreCase) + || e.Camera.Name.Contains(search, StringComparison.OrdinalIgnoreCase)); } var total = await query.CountAsync(ct).ConfigureAwait(false); diff --git a/src/Heimdall.Application/Events/Queries/GetEvents/GetEventsQuery.cs b/src/Heimdall.Application/Events/Queries/GetEvents/GetEventsQuery.cs index f0bc2fe..39bc76d 100644 --- a/src/Heimdall.Application/Events/Queries/GetEvents/GetEventsQuery.cs +++ b/src/Heimdall.Application/Events/Queries/GetEvents/GetEventsQuery.cs @@ -15,4 +15,5 @@ public record GetEventsQuery( int Page = 1, int PageSize = 50, string? Search = null, - Guid? UserId = null) : IRequest>>; + Guid? UserId = null, + bool HasSnapshot = false) : IRequest>>; diff --git a/src/Heimdall.Web/Components/Pages/SnapshotsPage.razor b/src/Heimdall.Web/Components/Pages/SnapshotsPage.razor index b801c62..9a8d1ca 100644 --- a/src/Heimdall.Web/Components/Pages/SnapshotsPage.razor +++ b/src/Heimdall.Web/Components/Pages/SnapshotsPage.razor @@ -1,4 +1,5 @@ @page "/snapshots" +@using Heimdall.Domain.Enums @attribute [Authorize] @layout MainLayout @inject HeimdallApiClient Api @@ -12,9 +13,19 @@ - - + + Mouvement + Personne + Véhicule + Personne inconnue + Visage reconnu + Plaque détectée + Caméra hors ligne + Caméra en ligne + + Charger @@ -41,7 +52,7 @@ else @key="ev.Id">
- Snapshot @FormatEventType(ev.Type) · @ev.Confidence.ToString("P0") @@ -53,6 +64,11 @@ else }
+
+ Précédent + Page @_page / @_totalPages · @_total snapshot(s) + Suivant +
} @* Lightbox toujours rendu : la visibilité est pilotée uniquement par @bind-Visible. @@ -86,7 +102,12 @@ else @code { private string _search = ""; + private string? _type; private List? _snapshots; + private int _page = 1; + private int _totalPages = 1; + private int _total; + private const int PageSize = 48; private bool _loading; private bool _showLightbox; private EventItem? _lightboxEvent; @@ -107,17 +128,12 @@ else try { var ct = _cts.Token; - var result = await Api.GetEventsAsync(null, null, 1, 100); + var result = await Api.GetEventsAsync(null, null, _page, PageSize, + string.IsNullOrWhiteSpace(_search) ? null : _search, _type, hasSnapshot: true); ct.ThrowIfCancellationRequested(); - var items = result?.Items ?? []; - if (!string.IsNullOrWhiteSpace(_search)) - { - var q = _search.ToLowerInvariant(); - items = items.Where(e => - (e.Label?.ToLowerInvariant().Contains(q) ?? false) || - e.Type.ToLowerInvariant().Contains(q)).ToList(); - } - _snapshots = items.Where(e => e.SnapshotUrl is not null).ToList(); + _snapshots = result?.Items ?? []; + _total = result?.TotalCount ?? 0; + _totalPages = Math.Max(1, (int)Math.Ceiling(_total / (double)PageSize)); } catch (OperationCanceledException) when (_cts.IsCancellationRequested) { } catch { Snackbar.Add(UiErrors.OperationFailed(), Severity.Error); } @@ -126,6 +142,18 @@ else public void Dispose() => _cts.Cancel(); + private async Task GoToPage(int page) + { + _page = page; + await LoadSnapshots(); + } + + private async Task ApplyFilters() + { + _page = 1; + await LoadSnapshots(); + } + private void OpenLightbox(EventItem ev) { _lightboxEvent = ev; diff --git a/src/Heimdall.Web/Controllers/EventsController.cs b/src/Heimdall.Web/Controllers/EventsController.cs index caff904..30b2fc3 100644 --- a/src/Heimdall.Web/Controllers/EventsController.cs +++ b/src/Heimdall.Web/Controllers/EventsController.cs @@ -68,6 +68,7 @@ public class EventsController : ControllerBase [FromQuery] DateTime? from, [FromQuery] DateTime? to, [FromQuery] double? minConfidence, + [FromQuery] bool hasSnapshot = false, [FromQuery] int page = 1, [FromQuery] int pageSize = 50, CancellationToken ct = default) @@ -77,7 +78,7 @@ public class EventsController : ControllerBase parsedType = t; var userId = GetUserId(); - var result = await _mediator.Send(new GetEventsQuery(cameraId, siteId, parsedType, from, to, minConfidence, page, pageSize, q, userId), ct); + var result = await _mediator.Send(new GetEventsQuery(cameraId, siteId, parsedType, from, to, minConfidence, page, pageSize, q, userId, hasSnapshot), ct); return Ok(result.Data); } diff --git a/src/Heimdall.Web/Services/HeimdallApiClient.cs b/src/Heimdall.Web/Services/HeimdallApiClient.cs index cc9039d..920faa9 100644 --- a/src/Heimdall.Web/Services/HeimdallApiClient.cs +++ b/src/Heimdall.Web/Services/HeimdallApiClient.cs @@ -828,12 +828,13 @@ public class HeimdallApiClient // ─── Events ──────────────────────────────────────────────────────── public async Task GetEventsAsync(Guid? cameraId, Guid? siteId, int page = 1, int pageSize = 50, - string? q = null, string? type = null, DateTime? from = null, DateTime? to = null) + string? q = null, string? type = null, DateTime? from = null, DateTime? to = null, bool hasSnapshot = false) { var client = CreateClient(); var query = $"page={page}&pageSize={pageSize}"; if (cameraId.HasValue) query += $"&cameraId={cameraId}"; if (siteId.HasValue) query += $"&siteId={siteId}"; + if (hasSnapshot) query += "&hasSnapshot=true"; if (!string.IsNullOrWhiteSpace(q)) query += $"&q={Uri.EscapeDataString(q)}"; if (!string.IsNullOrWhiteSpace(type)) query += $"&type={Uri.EscapeDataString(type)}"; if (from.HasValue) query += $"&from={Uri.EscapeDataString(from.Value.ToString("O", System.Globalization.CultureInfo.InvariantCulture))}"; diff --git a/tests/Heimdall.Application.Tests/Handlers/Events/GetEventsHandlerTests.cs b/tests/Heimdall.Application.Tests/Handlers/Events/GetEventsHandlerTests.cs index c26b8f2..ed3f2b4 100644 --- a/tests/Heimdall.Application.Tests/Handlers/Events/GetEventsHandlerTests.cs +++ b/tests/Heimdall.Application.Tests/Handlers/Events/GetEventsHandlerTests.cs @@ -93,4 +93,30 @@ public class GetEventsHandlerTests result.Data!.Items.Count.ShouldBe(1); result.Data.Items[0].Label.ShouldBe("Jean Dupont"); } + + [Fact] + public async Task Get_events_should_search_camera_name_and_filter_to_snapshots_before_paging() + { + var site = new Site("Test", "Paris", "Europe/Paris"); + var camera = new Camera(site.Id, "North Gate"); + var withSnapshot = new CameraEvent(camera.Id, EventType.PersonDetected, 0.9, DateTime.UtcNow); + withSnapshot.SetSnapshot("snapshot.jpg", "thumb.jpg"); + withSnapshot.SetCamera(camera); + var withoutSnapshot = new CameraEvent(camera.Id, EventType.MotionDetected, 0.8, DateTime.UtcNow); + withoutSnapshot.SetCamera(camera); + + var context = Substitute.For(); + context.CameraEvents.Returns(MockDbSetHelper.CreateFor([withSnapshot, withoutSnapshot])); + var cameraAccess = Substitute.For(); + cameraAccess.GetUserAccessibleCameraIdsAsync(Arg.Any(), Arg.Any()) + .Returns([camera.Id]); + var handler = new GetEventsHandler(context, cameraAccess); + + var result = await handler.Handle(new GetEventsQuery(null, null, null, null, null, null, + Page: 1, PageSize: 50, Search: "NORTH GATE", HasSnapshot: true), CancellationToken.None); + + result.IsSuccess.ShouldBeTrue(); + result.Data!.TotalCount.ShouldBe(1); + result.Data.Items.ShouldHaveSingleItem().SnapshotUrl.ShouldBe("snapshot.jpg"); + } }