Harden API memory and CPU usage across Phases 19–22.
Bound list/PDF payloads, stream MinIO I/O with cancellation, paginate history and FHIR $everything, tighten OCR lifetimes/buffers, and add phase tracking docs.
This commit is contained in:
@@ -145,52 +145,111 @@ public class FhirService : IFhirService
|
||||
total, count, offset, "Observation");
|
||||
}
|
||||
|
||||
public async Task<Bundle?> GetPatientEverythingAsync(Guid patientId)
|
||||
public async Task<Bundle?> GetPatientEverythingAsync(
|
||||
Guid patientId, int count = 100, int offset = 0)
|
||||
{
|
||||
count = Math.Clamp(count, 1, 500);
|
||||
offset = Math.Max(0, offset);
|
||||
|
||||
var patient = await _db.Patients.AsNoTracking().FirstOrDefaultAsync(p => p.Id == patientId);
|
||||
if (patient is null) return null;
|
||||
|
||||
var encounters = await _db.Encounters.AsNoTracking()
|
||||
.Where(e => e.PatientId == patientId)
|
||||
.ToListAsync();
|
||||
|
||||
var observations = await _db.Observations.AsNoTracking()
|
||||
.Where(o => o.PatientId == patientId)
|
||||
.OrderByDescending(o => o.RecordedAt)
|
||||
.ToListAsync();
|
||||
var encounterCount = await _db.Encounters.AsNoTracking()
|
||||
.CountAsync(e => e.PatientId == patientId);
|
||||
var observationCount = await _db.Observations.AsNoTracking()
|
||||
.CountAsync(o => o.PatientId == patientId);
|
||||
var total = 1 + encounterCount + observationCount;
|
||||
|
||||
var bundle = new Bundle
|
||||
{
|
||||
Type = Bundle.BundleType.Searchset,
|
||||
Total = 1 + encounters.Count + observations.Count,
|
||||
Total = total,
|
||||
Meta = new Meta { LastUpdated = DateTimeOffset.UtcNow },
|
||||
};
|
||||
|
||||
bundle.Entry.Add(new Bundle.EntryComponent
|
||||
{
|
||||
FullUrl = $"{_options.BaseUrl}/Patient/{patient.Id}",
|
||||
Resource = PatientMapper.ToFhir(patient, _options.BaseUrl),
|
||||
Search = new Bundle.SearchComponent { Mode = Bundle.SearchEntryMode.Match },
|
||||
});
|
||||
// Virtual layout: index 0 = Patient, then encounters, then observations.
|
||||
var remaining = count;
|
||||
var cursor = offset;
|
||||
|
||||
foreach (var enc in encounters)
|
||||
if (cursor == 0 && remaining > 0)
|
||||
{
|
||||
bundle.Entry.Add(new Bundle.EntryComponent
|
||||
{
|
||||
FullUrl = $"{_options.BaseUrl}/Encounter/{enc.Id}",
|
||||
Resource = EncounterMapper.ToFhir(enc, _options.BaseUrl),
|
||||
Search = new Bundle.SearchComponent { Mode = Bundle.SearchEntryMode.Include },
|
||||
FullUrl = $"{_options.BaseUrl}/Patient/{patient.Id}",
|
||||
Resource = PatientMapper.ToFhir(patient, _options.BaseUrl),
|
||||
Search = new Bundle.SearchComponent { Mode = Bundle.SearchEntryMode.Match },
|
||||
});
|
||||
remaining--;
|
||||
cursor = 1;
|
||||
}
|
||||
|
||||
foreach (var obs in observations)
|
||||
if (remaining > 0 && cursor < 1 + encounterCount)
|
||||
{
|
||||
bundle.Entry.Add(new Bundle.EntryComponent
|
||||
var encSkip = Math.Max(0, cursor - 1);
|
||||
var encTake = Math.Min(remaining, encounterCount - encSkip);
|
||||
if (encTake > 0)
|
||||
{
|
||||
FullUrl = $"{_options.BaseUrl}/Observation/{obs.Id}",
|
||||
Resource = ObservationMapper.ToFhir(obs, _options.BaseUrl),
|
||||
Search = new Bundle.SearchComponent { Mode = Bundle.SearchEntryMode.Include },
|
||||
});
|
||||
var encounters = await _db.Encounters.AsNoTracking()
|
||||
.Where(e => e.PatientId == patientId)
|
||||
.OrderByDescending(e => e.AdmissionDate)
|
||||
.Skip(encSkip)
|
||||
.Take(encTake)
|
||||
.ToListAsync();
|
||||
|
||||
foreach (var enc in encounters)
|
||||
{
|
||||
bundle.Entry.Add(new Bundle.EntryComponent
|
||||
{
|
||||
FullUrl = $"{_options.BaseUrl}/Encounter/{enc.Id}",
|
||||
Resource = EncounterMapper.ToFhir(enc, _options.BaseUrl),
|
||||
Search = new Bundle.SearchComponent { Mode = Bundle.SearchEntryMode.Include },
|
||||
});
|
||||
}
|
||||
|
||||
remaining -= encounters.Count;
|
||||
cursor += encounters.Count;
|
||||
}
|
||||
}
|
||||
|
||||
if (remaining > 0 && cursor < total)
|
||||
{
|
||||
var obsSkip = Math.Max(0, cursor - 1 - encounterCount);
|
||||
var obsTake = Math.Min(remaining, observationCount - obsSkip);
|
||||
if (obsTake > 0)
|
||||
{
|
||||
var observations = await _db.Observations.AsNoTracking()
|
||||
.Where(o => o.PatientId == patientId)
|
||||
.OrderByDescending(o => o.RecordedAt)
|
||||
.Skip(obsSkip)
|
||||
.Take(obsTake)
|
||||
.ToListAsync();
|
||||
|
||||
foreach (var obs in observations)
|
||||
{
|
||||
bundle.Entry.Add(new Bundle.EntryComponent
|
||||
{
|
||||
FullUrl = $"{_options.BaseUrl}/Observation/{obs.Id}",
|
||||
Resource = ObservationMapper.ToFhir(obs, _options.BaseUrl),
|
||||
Search = new Bundle.SearchComponent { Mode = Bundle.SearchEntryMode.Include },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var selfUrl = $"{_options.BaseUrl}/Patient/{patientId}/$everything?_count={count}&_offset={offset}";
|
||||
bundle.Link.Add(new Bundle.LinkComponent { Relation = "self", Url = selfUrl });
|
||||
|
||||
if (offset + count < total)
|
||||
{
|
||||
var nextUrl = $"{_options.BaseUrl}/Patient/{patientId}/$everything?_count={count}&_offset={offset + count}";
|
||||
bundle.Link.Add(new Bundle.LinkComponent { Relation = "next", Url = nextUrl });
|
||||
}
|
||||
|
||||
if (offset > 0)
|
||||
{
|
||||
var prevOffset = Math.Max(0, offset - count);
|
||||
var prevUrl = $"{_options.BaseUrl}/Patient/{patientId}/$everything?_count={count}&_offset={prevOffset}";
|
||||
bundle.Link.Add(new Bundle.LinkComponent { Relation = "previous", Url = prevUrl });
|
||||
}
|
||||
|
||||
return bundle;
|
||||
|
||||
Reference in New Issue
Block a user