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:
@@ -146,32 +146,41 @@ public class DigitizationBatchesController : ControllerBase
|
||||
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetDocument(Guid id)
|
||||
{
|
||||
var batch = await _batches.GetByIdAsync(id);
|
||||
var batchMeta = await _db.DigitizationBatches
|
||||
.AsNoTracking()
|
||||
.Where(b => b.Id == id)
|
||||
.Select(b => new { b.DocumentRef })
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
if (batch.DocumentRef == "live-capture")
|
||||
if (batchMeta is null)
|
||||
return NotFound(ApiResponse<object>.Fail(404, "Batch not found.", "BATCH_NOT_FOUND"));
|
||||
|
||||
if (batchMeta.DocumentRef == "live-capture" || string.IsNullOrEmpty(batchMeta.DocumentRef))
|
||||
return NotFound(ApiResponse<object>.Fail(
|
||||
404, "This batch has no scanned document.", "NO_DOCUMENT"));
|
||||
|
||||
var scanned = await _db.ScannedDocuments
|
||||
var contentType = await _db.ScannedDocuments
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(d => d.BatchId == id);
|
||||
|
||||
var contentType = scanned?.ContentType ?? "application/octet-stream";
|
||||
.Where(d => d.BatchId == id)
|
||||
.Select(d => d.ContentType)
|
||||
.FirstOrDefaultAsync() ?? "application/octet-stream";
|
||||
|
||||
await RecordDocumentAccessAsync(id);
|
||||
|
||||
try
|
||||
{
|
||||
if (!await _storage.ObjectExistsAsync(batch.DocumentRef))
|
||||
return NotFound(ApiResponse<object>.Fail(
|
||||
404,
|
||||
"Scanned document is missing from storage. Re-upload the batch or restore the object.",
|
||||
"DOCUMENT_NOT_FOUND"));
|
||||
|
||||
var stream = await _storage.DownloadAsync(batch.DocumentRef);
|
||||
var stream = await _storage.DownloadAsync(
|
||||
batchMeta.DocumentRef, HttpContext.RequestAborted);
|
||||
Response.Headers.CacheControl = "private, max-age=300";
|
||||
return File(stream, contentType);
|
||||
}
|
||||
catch (Minio.Exceptions.ObjectNotFoundException)
|
||||
{
|
||||
return NotFound(ApiResponse<object>.Fail(
|
||||
404,
|
||||
"Scanned document is missing from storage. Re-upload the batch or restore the object.",
|
||||
"DOCUMENT_NOT_FOUND"));
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
return StatusCode(503, ApiResponse<object>.Fail(
|
||||
|
||||
@@ -54,12 +54,15 @@ public class FhirPatientController : ControllerBase
|
||||
[HttpGet("{id}/$everything")]
|
||||
[ProducesResponseType(typeof(Bundle), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(OperationOutcome), StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> Everything(string id)
|
||||
public async Task<IActionResult> Everything(
|
||||
string id,
|
||||
[FromQuery(Name = "_count")] int count = 100,
|
||||
[FromQuery(Name = "_offset")] int offset = 0)
|
||||
{
|
||||
var bundle = await _fhir.GetPatientEverythingAsync(Guid.Parse(id));
|
||||
var bundle = await _fhir.GetPatientEverythingAsync(Guid.Parse(id), count, offset);
|
||||
if (bundle is null)
|
||||
return NotFound(FhirErrorHelper.NotFound("Patient", id));
|
||||
|
||||
return Ok(bundle);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,9 +41,12 @@ public class PatientsController : ControllerBase
|
||||
[HttpGet("{patientId:guid}/digitization-history")]
|
||||
[ProducesResponseType(typeof(ApiResponse<PatientDigitizationHistoryResponse>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetDigitizationHistory(Guid patientId)
|
||||
public async Task<IActionResult> GetDigitizationHistory(
|
||||
Guid patientId,
|
||||
[FromQuery] int page = 1,
|
||||
[FromQuery] int pageSize = 20)
|
||||
{
|
||||
var history = await _history.GetPatientHistoryAsync(patientId);
|
||||
var history = await _history.GetPatientHistoryAsync(patientId, page, pageSize);
|
||||
return Ok(ApiResponse<PatientDigitizationHistoryResponse>.Ok(history));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user