Stop consumers from committing past failed offsets, harden Rabbit/DataLake/gateway backpressure, dispose JsonDocument/MinIO correctly, and batch expensive DB/HTTP paths. Track progress in docs/performance.
97 lines
3.4 KiB
C#
97 lines
3.4 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
public sealed class UnacknowledgedAlertsCheck
|
|
{
|
|
public static readonly ReconciliationCheckType CheckType = ReconciliationCheckType.UnacknowledgedCriticalAlert;
|
|
|
|
private readonly AppDbContext _db;
|
|
private readonly ReconciliationPublisher _publisher;
|
|
private readonly ReconciliationJobOptions _opts;
|
|
private readonly ILogger<UnacknowledgedAlertsCheck> _logger;
|
|
|
|
public UnacknowledgedAlertsCheck(
|
|
AppDbContext db,
|
|
ReconciliationPublisher publisher,
|
|
IOptions<ReconciliationJobOptions> opts,
|
|
ILogger<UnacknowledgedAlertsCheck> logger)
|
|
{
|
|
_db = db;
|
|
_publisher = publisher;
|
|
_opts = opts.Value;
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task<int> RunAsync(CancellationToken ct)
|
|
{
|
|
var cutoff = DateTimeOffset.UtcNow
|
|
.AddMinutes(-_opts.UnacknowledgedAlertThresholdMinutes);
|
|
|
|
var staleAlerts = await _db.ClinicalAlerts
|
|
.Where(a => a.Severity == AlertSeverity.Critical
|
|
&& a.Status == AlertStatus.Open
|
|
&& a.TriggeredAt < cutoff)
|
|
.Select(a => new { a.Id, a.EncounterId, a.PatientId, a.TriggeredAt, a.Details })
|
|
.ToListAsync(ct);
|
|
|
|
if (!staleAlerts.Any())
|
|
{
|
|
_logger.LogDebug("[RECONCILIATION] Check1 — no stale critical alerts");
|
|
return 0;
|
|
}
|
|
|
|
var byEncounter = staleAlerts
|
|
.GroupBy(a => new { a.EncounterId, a.PatientId })
|
|
.ToList();
|
|
|
|
var encounterIds = byEncounter.Select(g => g.Key.EncounterId).ToList();
|
|
var alreadyOpen = (await _db.ReconciliationAlerts
|
|
.AsNoTracking()
|
|
.Where(r => r.CheckType == CheckType
|
|
&& r.ResolvedAt == null
|
|
&& r.EncounterId != null
|
|
&& encounterIds.Contains(r.EncounterId.Value))
|
|
.Select(r => r.EncounterId!.Value)
|
|
.ToListAsync(ct))
|
|
.ToHashSet();
|
|
|
|
var created = new List<ReconciliationAlert>();
|
|
foreach (var group in byEncounter)
|
|
{
|
|
if (alreadyOpen.Contains(group.Key.EncounterId)) continue;
|
|
|
|
var alertIds = group.Select(a => a.Id).ToList();
|
|
var oldest = group.Min(a => a.TriggeredAt);
|
|
var details = $"{alertIds.Count} unacknowledged CRITICAL alert(s) for encounter "
|
|
+ $"{group.Key.EncounterId}. Oldest triggered at {oldest:u}. "
|
|
+ $"Alert IDs: {string.Join(", ", alertIds)}.";
|
|
|
|
var rec = new ReconciliationAlert
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
CheckType = CheckType,
|
|
EncounterId = group.Key.EncounterId,
|
|
PatientId = group.Key.PatientId,
|
|
Details = details,
|
|
CreatedAt = DateTimeOffset.UtcNow,
|
|
};
|
|
|
|
_db.ReconciliationAlerts.Add(rec);
|
|
created.Add(rec);
|
|
|
|
_logger.LogWarning(
|
|
"[RECONCILIATION] {CheckType} — encounter {EncounterId}: {Details}",
|
|
CheckType, group.Key.EncounterId, details);
|
|
}
|
|
|
|
if (created.Count > 0)
|
|
{
|
|
await _db.SaveChangesAsync(ct);
|
|
foreach (var rec in created)
|
|
await _publisher.PublishAsync(rec, ct);
|
|
}
|
|
|
|
return created.Count;
|
|
}
|
|
}
|