Files
vigilcare-records/docs/plans/phase-20-plan.md
T
Trent a17bc2b734
CI / changes (push) Successful in 17s
dotnet8-ci / build-and-test (push) Successful in 8m23s
CI / backend (push) Successful in 8m24s
node-ci / build (push) Failing after 2m1s
CI / frontend (push) Failing after 2m2s
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.
2026-11-30 23:55:56 +08:00

89 lines
3.9 KiB
Markdown

# Phase 20 — Memory Safety (MinIO + Unbounded Clinical Payloads)
## What exists
- [`DocumentStorageService.DownloadAsync`](../../VigilCareRecordsAPI/Services/DocumentStorageService.cs) buffers the full object and uses sync `CopyTo` in the MinIO callback; no `CancellationToken` on the interface.
- Upload copies the full stream then hashes; `EnsureBucketAsync` runs on every upload.
- [`GetDocument`](../../VigilCareRecordsAPI/Controllers/DigitizationBatchesController.cs) calls `ObjectExistsAsync` then downloads.
- [`DigitizationHistoryService.GetPatientHistoryAsync`](../../VigilCareRecordsAPI/Services/DigitizationHistoryService.cs) loads all batches + drafts + events (tracked).
- [`FhirService.GetPatientEverythingAsync`](../../VigilCareRecordsAPI/Services/FhirService.cs) loads all encounters and observations.
- [`WorkQueueService.GetOverviewAsync`](../../VigilCareRecordsAPI/Services/WorkQueueService.cs) materializes every pending `UpdatedAt`.
Requires [Phase 19](./phase-19-plan.md). Tracker: [performance-hardening-overview.md](./performance-hardening-overview.md).
## What needs to be built
Reduce peak RAM for document I/O and history/FHIR dumps. Keep returning `MemoryStream` from download for OCR compatibility; true HTTP pipe-through is out of scope unless required after measuring.
Do not start [Phase 21](./phase-21-plan.md) until success criteria below pass.
---
## Step 1 — Async MinIO download + CancellationToken
- Add `CancellationToken` to `IDocumentStorageService` download/upload (and call sites).
- In `DownloadAsync`, use async copy inside the MinIO callback (`CopyToAsync` + CT).
- Propagate CT from controllers / OCR hosted service where already available.
---
## Step 2 — Upload hashing and bucket ensure
- Hash while buffering once (single `MemoryStream` pass) where practical.
- Ensure bucket once at bootstrap ([`AppBootstrapHostedService`](../../VigilCareRecordsAPI/BackgroundServices/AppBootstrapHostedService.cs)) and drop per-upload `EnsureBucketAsync`, or cache a process-lifetime “bucket ready” flag.
---
## Step 3 — GetDocument without redundant StatObject
- Remove `ObjectExistsAsync` when missing objects can be expressed as 404 from download failure / MinIO not-found.
---
## Step 4 — Paginate patient digitization history
- Add `page` / `pageSize` (default 20, max 50) to history API + service.
- Use `AsNoTracking`; project summaries.
- Cap audit trail size per entry (e.g. last N events) or defer full trail so payload stays bounded.
- Update DTO / controller / web client callers if they assume an unpaged full list.
---
## Step 5 — FHIR `$everything` paging
- Honor `_count` (default 100, max 500) with Bundle `next` link.
- Do not load the entire observation history in one query/response.
---
## Step 6 — Work-queue overview SQL aggregates
- Replace pending `UpdatedAt` `ToListAsync` with EF aggregates for average age and oldest pending (avg / min `UpdatedAt`).
---
## Success criteria
- [x] Download path uses async copy and accepts CT
- [x] Bucket ensure is not invoked on every upload
- [x] History returns `Entries.Count ≤ pageSize` with accurate total count metadata
- [x] `$everything?_count=10` returns ≤ 10 resource entries (plus paging metadata as designed)
- [x] Overview no longer materializes all pending timestamps in memory
- [x] GetDocument / OCR download still work in integration tests
- [x] `dotnet test` passes; Phase 11 FHIR script still green if run
## Out of scope
- Tesseract engine reuse / Azure singleton (Phase 21)
- Fuzzy match / seed N+1 (Phase 22)
## Verification
1. `dotnet test VigilCareRecordsAPI.Tests/VigilCareRecordsAPI.Tests.csproj`
2. `./scripts/run-vigilcare-records-phase-11-verification.sh` (optional but recommended)
3. Update overview findings 1, 3, 4, 7 (storage CT), 12, 13 → Done when this phase merges
## Gate
Do not start [Phase 21](./phase-21-plan.md) until success criteria above are met.