-
Notifications
You must be signed in to change notification settings - Fork 0
api fixes #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
api fixes #12
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| namespace Onevo.Api.Auth; | ||
|
|
||
| /// <summary>Shared service-key validation for cloud-ai and connector bootstrap endpoints.</summary> | ||
| public static class ServiceAuth | ||
| { | ||
| public const string DefaultBootstrapKey = "dev-connector-bootstrap-key"; | ||
|
|
||
| /// <summary>Key used by edge connectors to register (bootstrap only).</summary> | ||
| public static string ConnectorBootstrapKey(IConfiguration cfg) | ||
| => cfg["Seed:ConnectorBootstrapKey"] ?? DefaultBootstrapKey; | ||
|
|
||
| /// <summary>Key used by the cloud-ai worker for ingest and zone reads.</summary> | ||
| public static string CloudAiServiceKey(IConfiguration cfg) | ||
| => cfg["CloudAi:ServiceKey"] | ||
| ?? cfg["Seed:CloudAiServiceKey"] | ||
| ?? ConnectorBootstrapKey(cfg); | ||
|
|
||
| public static bool ValidateCloudAiKey(IConfiguration cfg, string? provided) | ||
| => !string.IsNullOrEmpty(provided) && provided == CloudAiServiceKey(cfg); | ||
|
|
||
| public static bool ValidateBootstrapKey(IConfiguration cfg, string? provided) | ||
| => !string.IsNullOrEmpty(provided) && provided == ConnectorBootstrapKey(cfg); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| using Microsoft.AspNetCore.Authorization; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Onevo.Api.Auth; | ||
| using Onevo.Api.Contracts; | ||
| using Onevo.Api.Data; | ||
| using Onevo.Api.Domain; | ||
|
|
||
| namespace Onevo.Api.Controllers; | ||
|
|
||
| [ApiController] | ||
| [Authorize] | ||
| [Route("api/analytics")] | ||
| public class AnalyticsController : ControllerBase | ||
| { | ||
| private readonly OnevoDbContext _db; | ||
|
|
||
| public AnalyticsController(OnevoDbContext db) => _db = db; | ||
|
|
||
| [HttpGet("summary")] | ||
| public async Task<ActionResult<AnalyticsSummaryResponse>> Summary([FromQuery] Guid? storeId) | ||
| { | ||
| if (storeId is not null && !TenantAccess.CanAccessStore(User, storeId.Value)) | ||
| return Forbid(); | ||
|
|
||
| var alerts = TenantAccess.ScopeAlerts(_db.Alerts, User); | ||
| IQueryable<Clip> clips = from c in _db.Clips | ||
| join cam in TenantAccess.ScopeCameras(_db.Cameras, User) | ||
| on c.CameraId equals cam.Id | ||
| select c; | ||
|
|
||
| if (storeId is not null) | ||
| { | ||
| alerts = alerts.Where(a => a.StoreId == storeId); | ||
| clips = from c in clips | ||
| join cam in _db.Cameras on c.CameraId equals cam.Id | ||
| where cam.StoreId == storeId | ||
| select c; | ||
| } | ||
|
|
||
| var alertRows = await alerts.AsNoTracking().ToListAsync(); | ||
| var clipCount = await clips.CountAsync(); | ||
| var analyzedClips = await clips.CountAsync(c => c.Status == ClipStatus.Analyzed); | ||
|
|
||
| return Ok(new AnalyticsSummaryResponse( | ||
| TotalAlerts: alertRows.Count, | ||
| PendingAlerts: alertRows.Count(a => a.Status == AlertStatus.PendingReview), | ||
| HighRiskAlerts: alertRows.Count(a => a.RiskLevel == RiskLevel.High), | ||
| MediumRiskAlerts: alertRows.Count(a => a.RiskLevel == RiskLevel.Medium), | ||
| FalsePositives: alertRows.Count(a => a.Status == AlertStatus.FalsePositive), | ||
| TotalClips: clipCount, | ||
| AnalyzedClips: analyzedClips, | ||
| AlertsByType: alertRows | ||
| .GroupBy(a => a.AlertType) | ||
| .ToDictionary(g => g.Key, g => g.Count()) | ||
| )); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -246,6 +246,78 @@ join store in _db.Stores on cam.StoreId equals store.Id | |
| [Authorize(Roles = "Admin,Manager")] | ||
| [HttpDelete("{id:guid}")] | ||
| public async Task<IActionResult> Delete(Guid id) | ||
| { | ||
| var result = await TryDeleteClipAsync(id); | ||
| return result switch | ||
| { | ||
| DeleteClipResult.Deleted => Ok(new { ok = true, clipId = id }), | ||
| DeleteClipResult.NotFound => NotFound(), | ||
| DeleteClipResult.SkippedConfirmed => Conflict(new { error = "Cannot delete clip linked to a confirmed alert" }), | ||
| _ => NotFound(), | ||
| }; | ||
| } | ||
|
|
||
| [Authorize(Roles = "Admin,Manager")] | ||
| [HttpPost("bulk-delete")] | ||
| public async Task<ActionResult<BulkDeleteResponse>> BulkDelete(BulkDeleteRequest req) | ||
| { | ||
| if (req.DeleteAllInStore) | ||
| { | ||
| if (req.StoreId is null) | ||
| return BadRequest(new { error = "storeId is required for delete all" }); | ||
| if (!TenantAccess.CanAccessStore(User, req.StoreId.Value)) | ||
| return Forbid(); | ||
| } | ||
|
|
||
| var targetIds = await ResolveClipDeleteIdsAsync(req); | ||
| if (targetIds is null) | ||
| return BadRequest(new { error = "Provide ids or set deleteAllInStore with storeId" }); | ||
|
|
||
| var deleted = 0; | ||
| var skipped = 0; | ||
| var errors = new List<string>(); | ||
|
|
||
| foreach (var id in targetIds.Distinct()) | ||
| { | ||
| var result = await TryDeleteClipAsync(id); | ||
| switch (result) | ||
| { | ||
| case DeleteClipResult.Deleted: | ||
| deleted++; | ||
| break; | ||
| case DeleteClipResult.SkippedConfirmed: | ||
| skipped++; | ||
| break; | ||
| case DeleteClipResult.NotFound: | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return Ok(new BulkDeleteResponse(deleted, skipped, errors)); | ||
| } | ||
|
Comment on lines
+276
to
+297
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 7. Bulk clip delete hides failures ClipsController bulk-delete returns an errors array but never populates it and does not isolate per-clip exceptions, so a single S3/DB failure can abort the whole request with a 500. The response also omits any accounting for NotFound clip IDs, making bulk results hard to interpret. Agent Prompt
|
||
|
|
||
| private async Task<List<Guid>?> ResolveClipDeleteIdsAsync(BulkDeleteRequest req) | ||
| { | ||
| if (req.DeleteAllInStore) | ||
| { | ||
| if (req.StoreId is null) return null; | ||
| if (!TenantAccess.CanAccessStore(User, req.StoreId.Value)) return null; | ||
|
|
||
| return await ( | ||
| from clip in _db.Clips | ||
| join cam in TenantAccess.ScopeCameras(_db.Cameras, User) on clip.CameraId equals cam.Id | ||
| where cam.StoreId == req.StoreId | ||
| select clip.Id | ||
| ).ToListAsync(); | ||
| } | ||
|
|
||
| if (req.Ids is { Count: > 0 }) return req.Ids; | ||
| return null; | ||
| } | ||
|
|
||
| private enum DeleteClipResult { Deleted, NotFound, SkippedConfirmed } | ||
|
|
||
| private async Task<DeleteClipResult> TryDeleteClipAsync(Guid id) | ||
| { | ||
| var row = await ( | ||
| from clip in _db.Clips | ||
|
|
@@ -254,14 +326,14 @@ join cam in TenantAccess.ScopeCameras(_db.Cameras, User) on clip.CameraId equals | |
| select new { clip, cam } | ||
| ).FirstOrDefaultAsync(); | ||
|
|
||
| if (row is null) return NotFound(); | ||
| if (row is null) return DeleteClipResult.NotFound; | ||
|
|
||
| var alert = await _db.Alerts | ||
| .Include(a => a.Reviews) | ||
| .FirstOrDefaultAsync(a => a.ClipId == id); | ||
|
|
||
| if (alert is not null && alert.Status == AlertStatus.Confirmed) | ||
| return Conflict(new { error = "Cannot delete clip linked to a confirmed alert" }); | ||
| return DeleteClipResult.SkippedConfirmed; | ||
|
|
||
| if (!string.IsNullOrEmpty(row.clip.ObjectKey)) | ||
| await _s3.DeleteAsync(row.clip.ObjectKey); | ||
|
|
@@ -279,6 +351,6 @@ join cam in TenantAccess.ScopeCameras(_db.Cameras, User) on clip.CameraId equals | |
|
|
||
| _db.Clips.Remove(row.clip); | ||
| await _db.SaveChangesAsync(); | ||
| return Ok(new { ok = true, clipId = id }); | ||
| return DeleteClipResult.Deleted; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
6. Analytics loads alerts in-memory
🐞 Bug➹ PerformanceAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools