Backend
PY-2 livehealthapp finalization and Storage Manager, PY-3 crelio-app Storage Manager and Lambda-facing file-type helpers, persistence, indexing, and rendering.
Backend
This page consolidates server-side responsibilities: report conversion, presigned generation + reconcile, uploadFileTypeReportAPI branching (new vs legacy), mark-as-done, Elasticsearch and Pusher side effects, print/render special cases, and PY-3 Storage Manager + Lambda-authenticated file-type-report helpers.
Cross-links: Overview, Workflow Guide, Design Decisions.
Backend Technicalities
livehealthapp (PY-2)
1. Report conversion
File: apps/livehealthapp/labs/API.py
Function: convertReportToFileOrNormal
Responsibilities:
- Authorize conversion (permissions + radiology guard + session shape).
- Delete existing
ReportValuerows. - Toggle
fileInputReportand related flags (store_values_to_document_dbreset in this path). - Reset completion, approval, signing, partial sign, print-done fields.
- Trigger Pusher + patient report ES reindex + activity log.
2. File upload finalizer
File: apps/livehealthapp/reports/views.py
Function: uploadFileTypeReportAPI (+ save_file_type_report_to_report_value on the write path)
Responsibilities:
- Branch detection: if
initialFilePathis present and path-like → presigned-aware fast path (file already in storage). - Legacy branch: if
fileDatapresent → base64 decode path,restruct_uploaded_files,SmStore, then value write. - Update
ReportValue.valueto the stored path. - Mutate
LabReportRelationcompletion / approval fields (e.g.completedTests,isApproved, timestamps, actinglabUserId). commonPusherFunctionForLabReportRelation(or equivalent) for waiting-list freshness.- Patient report Elasticsearch reindex for search surfaces.
- Upload activity log entry.
Report mutations (presigned-aware branch - conceptual)
| Field | Effect |
|---|---|
ReportValue.value | File path string |
reportDate / lastUpdated | Updated |
isApproved | Reset toward “needs review” posture (implementation-specific constants) |
isPartialFill | Reset |
labUserId | Acting user |
completedTests | Driven to completed posture for the file-report case |
Exact field names should be verified in the implementation block inside uploadFileTypeReportAPI when debugging a specific deployment branch.
3. Legacy preprocessing
Same file: restruct_uploaded_files in reports/views.py
Role: normalize/validate uploads in legacy mode - images, damaged PDFs, EOF quirks, internal failure codes. Historically this is the kind of work the revamp tries to move off the synchronous PY-2 request path toward async / infra-assisted processing.
4. Storage Manager (PY-2)
File: apps/livehealthapp/labs/storage_manager.py
| Class / function | Responsibility |
|---|---|
SmGeneratePreSignedURL | Issue direct-upload contract + create Storage Manager log |
SmReconcilePreSignedURL | head_object-style verification + reconcile flags + size on log |
get_storage_paths | Path structure generation |
| Cleanup utilities | e.g. replace deleted file-type attachments with file_not_exists.pdf |
URL wiring: apps/livehealthapp/livehealthapp/urls.py (or equivalent project urls.py in your branch) - sm_generate_presigned_url/, sm_reconcile_presigned_url/, uploadFileTypeReport/, uploadFileWithHeader/.
5. Mark-as-done support
File: apps/livehealthapp/labs/newAPI.py
Function: mark_report_as_done_api
Marks sibling operational reports complete with business rules (e.g. at least one operational report must remain incomplete in the bill), updates ES, triggers hooks/Pusher as implemented.
6. Downstream rendering
| File | Why it matters |
|---|---|
templates/labTemplates/fileInputReport.jinja | Template helper for rendering file-input images in lab print HTML |
reports/v2_views/report_view.py | Some print flows convert image-backed file input values into generated PDF pages |
File-type reports are not “upload and forget”; print/download stacks branch on file paths vs parameter matrices.
crelio-app (PY-3)
1. Storage Manager APIs and core implementation
| Area | Link |
|---|---|
| HTTP views | apps/crelio-app/core/views/storage_manager/storage_manager_views.py |
| URL routing | apps/crelio-app/core/urls.py |
| Manager | apps/crelio-app/core/utils/storage_manager/manager.py |
| Path + validation utilities | apps/crelio-app/core/utils/storage_manager/utils.py |
Capabilities include presigned POST generation, presigned download, reconcile, canonical path generation, extension gates, soft-delete / cleanup behaviors.
2. Canonical path generation (documented shape)
<DeploymentZone>/<LabId>/<PatientId>/<FileCategory>/<cloudDocId>_<filename>.<extension>Example:
IN/123/456456/FileTypeReport/16915654908422848526992_foo.jpegBoth PY-2 and PY-3 utilities support caller-supplied cloud_path patterns; the temp vs concrete distinction matters when Lambda rewrites paths.
3. File-type-report Lambda-facing APIs
File: apps/crelio-app/report/views/file_type_report_lambda.py
Routes: see apps/crelio-app/report/urls.py
| Endpoint family | Responsibility |
|---|---|
report-value/file-type/get/storage-manager/path | Accept temporary path → return canonical concrete Storage Manager path |
report-value/file-type/update | Update report_results / lab report state with final file path + SM log path rewrite semantics |
FetchFileTypeReportPath (conceptual): looks up SM log for temp path, pulls file metadata, resolves S3 account details, emits canonical path. Important implementation detail from source analysis: extension normalization may force .pdf in the helper path - treat this as a normalization contract when reasoning about mixed image/PDF inputs on Lambda-shaped flows.
UpdateReportValueWithFilePath (conceptual): accepts filePath, temporary_filepath, labReportId; updates report_results.value, timestamps, completion flags; aligns SM log from temporary to final path.
4. Lambda authentication
File: apps/crelio-app/core/utils/aws/authenticate_lambda_requests.py
- Expects header
X-Lambda-Token - Validates against
settings.CRON_SIGNING_KEY(naming may vary by environment - confirm in deployment secrets)
5. File type reports beyond manual UI upload
Example: apps/crelio-app/interfacing/models/device.py - instrument/device flows can persist file_category="FileTypeReport" style outputs.
Format linkage: apps/crelio-app/report/views/report_format_link.py - format type 4 correlates with fileInput=1 semantics in that module’s conventions.
DB and architecture
Relational state, object storage, Storage Manager logs, and Elasticsearch documents together implement the feature; PY-2 remains the observed domain finalizer for the default frontend path.
Core entities
| Entity | Why it matters |
|---|---|
labReportRelation / LabReportRelation | fileInputReport, completedTests, approval / signing flags |
ReportValue / report_results | value holds the file path for file-type reports |
StorageManagerLog | Presigned lifecycle, sizes, paths, reconcile markers |
| Patient report ES document | Search + waiting-list propagation |
What gets persisted where
| Layer | Stored value |
|---|---|
| S3 | Binary PDF/image object |
| Report DB | Path in ReportValue.value or report_results.value |
| Report row | Completion / approval metadata |
| Storage Manager log | Upload metadata, category, paths, reconcile state, file size |
| Elasticsearch | Patient-report doc + SM log record |
File path semantics
| Path type | Typical example | Who creates it |
|---|---|---|
| Temporary caller-specified | FileTypeReports/IN/28/123456/ab12cd/report.pdf | Frontend |
| Canonical SM | IN/28/20798388/FileTypeReport/<cloudDocId>_report.pdf | Storage Manager / PY-3 helper |
| Compatible path | Human-stable equivalent without generated cloud id prefix | Storage Manager utilities |
| Behavior | Explanation |
|---|---|
| Requested path can be honored as-is | Caller-supplied cloud_path supported in SM utilities |
| Temp vs concrete can diverge | Basis for Lambda + PY-3 helper design |
| Deleted attachments normalized | Cleanup may rewrite to file_not_exists.pdf |
PY-3 path helper may force .pdf | Normalization / downstream PDF assumption in Lambda-assisted architecture |
Setup dependencies by layer
| Layer | Dependency | Why |
|---|---|---|
| Frontend | Session with lab context | SM + finalization are lab-scoped |
| Frontend | Report already in file mode | Wrong mode → wrong UX path |
| PY-2 | S3 account for FileTypeReport | presigned generation |
| PY-2 | extension allowlist | SM rejects unsupported types |
| PY-2 | SM ES logging | reconcile + observability |
| PY-2 | patient report ES | searchable completion state |
| PY-2 | Pusher / realtime | operator UI freshness |
| PY-3 | Lambda token auth | secured helper endpoints |
| AWS | S3 + optional event routing | direct upload + optional Lambda |
Architectural caveats (backend-focused)
- Observed FE still finalizes in PY-2 - presigned + reconcile can be PY-2 while PY-3 exposes parallel helpers.
- Lambda source not in repo snapshot - only contracts + auth + endpoints are verifiable here.
- Header/footer remains
uploadFileWithHeader/ base64 transport on PY-2. - Temporary path may be what PY-2 stores in the observed path until/unless a later job rewrites - contrast with PY-3 “temp → canonical” helper intent.
For rationale and decision framing, see Design Decisions.