feature: Trend Detection & Alert Suppression Windows

This commit is contained in:
voltsrage
2026-06-18 21:18:13 +08:00
parent e6f7989298
commit 3c54feb38a
31 changed files with 2751 additions and 18 deletions
+39 -1
View File
@@ -1,11 +1,17 @@
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
public class AlertService : IAlertService
{
private readonly AppDbContext _db;
private readonly IServiceProvider _services;
public AlertService(AppDbContext db) => _db = db;
public AlertService(AppDbContext db, IServiceProvider services)
{
_db = db;
_services = services;
}
public async Task<PagedResult<ClinicalAlert>> ListByEncounterAsync(
Guid encounterId, AlertStatus? status, int page, int pageSize)
@@ -100,10 +106,42 @@ public class AlertService : IAlertService
CreatedAt = DateTimeOffset.UtcNow
});
if (alert.AlertType.IsSuppressible())
{
var suppression = _services.GetRequiredService<IAlertSuppressionService>();
var options = _services.GetRequiredService<IOptions<SuppressionOptions>>().Value;
var windowMinutes = await ResolveSuppressionWindowMinutesAsync(
alert.AlertType, options.DefaultWindowMinutes);
await suppression.SetSuppressionAsync(
alert.EncounterId,
alert.AlertType,
TimeSpan.FromMinutes(windowMinutes));
}
await _db.SaveChangesAsync();
return alert;
}
private async Task<int> ResolveSuppressionWindowMinutesAsync(
AlertType alertType, int defaultWindowMinutes)
{
if (alertType == AlertType.News2Warning)
return defaultWindowMinutes;
var observationCode = alertType.ObservationCodeForWarning();
if (observationCode is null)
return defaultWindowMinutes;
var overrideMinutes = await _db.AlertThresholds
.AsNoTracking()
.Where(t => t.ObservationCode == observationCode)
.Select(t => t.SuppressionWindowMinutes)
.FirstOrDefaultAsync();
return overrideMinutes ?? defaultWindowMinutes;
}
public async Task<ClinicalAlert> ResolveAsync(Guid id)
{
var alert = await _db.ClinicalAlerts.FindAsync(id);