diff --git a/Dockerfile b/Dockerfile index 71d909d..3b0db98 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,17 @@ # Build context is the repo root (see docker-compose.yml) so the sibling # packages Core/, Persistance/, Extraction/ and models/ -# can be copied. +# can be copied, and Transformation/ compiled. + +# dbt's manifest: the measures' definitions and the compiled SQL of their event +# models. The API serves the measures from it and the indicator cycle creates +# the views. Compiling needs no data; the SQL is SQLite's, the image's default +# engine. +FROM python:3.12-slim AS manifest +COPY --from=ghcr.io/astral-sh/uv:0.7.9 /uv /uvx /bin/ +WORKDIR /app/Transformation +COPY Transformation/ ./ +RUN uv sync && uv run --no-sync dbt compile --profiles-dir . --profile tea + FROM python:3.12-slim # Tesseract (with Spanish data) for scanned documents; pytesseract only wraps the binary. @@ -34,6 +45,11 @@ ENV MLFLOW_DISABLE_TELEMETRY=true DO_NOT_TRACK=true ENV WEBAPI_REPORT_DIR=/data/reportes ENV INDICADORES_DIR=/data/indicadores ENV MLFLOW_DISABLE_AGENT_HINT=1 +# Without it the measure routes (//indicadores, //fhir) aren't +# registered. On another engine, mount a manifest compiled with that dbt target +# and point this at it. +COPY --from=manifest /app/Transformation/target/manifest.json /app/Transformation/target/manifest.json +ENV DBT_MANIFEST=/app/Transformation/target/manifest.json RUN chmod +x /app/WebApi/docker-entrypoint.sh diff --git a/tests/test_fhir_tea.py b/tests/test_fhir_tea.py index 61533db..59bf569 100644 --- a/tests/test_fhir_tea.py +++ b/tests/test_fhir_tea.py @@ -167,8 +167,9 @@ def test_the_follow_up_filters(client, query, n): def test_includes_bring_the_patient_its_steps_and_its_study_eeg(client): """Pins down: `_include` adds each child's Patient (birth date to the month), CarePlan with its steps, M-CHAT, - confirmed diagnosis and study EEG as a DiagnosticReport with its quality observations; no metric observation while - the alterations are hidden. Fails if an included resource drops out or the alterations leak.""" + ADOS-2 (the list shows its CSS and module), confirmed diagnosis and study EEG as a DiagnosticReport with its quality + observations; no metric observation while the alterations are hidden. Fails if an included resource drops out or + the alterations leak.""" body = get(client, "/tea/fhir/EpisodeOfCare?date=le2025-12-31&status=finished&_include=*") included = resources(body, mode="include") kinds = {r["resourceType"] for r in included} @@ -177,6 +178,8 @@ def test_includes_bring_the_patient_its_steps_and_its_study_eeg(client): reports = [r for r in included if r["resourceType"] == "DiagnosticReport"] assert {r["conclusionCode"][0]["coding"][0]["code"] for r in reports} == {"BUENO"} assert all(len(r["result"]) == 5 for r in reports) + ados2 = [r for r in included if r["resourceType"] == "Observation" and r["code"]["coding"][0]["code"] == "ados2"] + assert ados2 and all(any(c["code"]["coding"][0]["code"] == "ados2.css" for c in r["component"]) for r in ados2) def test_the_alteration_filter_waits_for_the_validation(client, monkeypatch): diff --git a/tests/test_new_sources.py b/tests/test_new_sources.py index f647c74..d13c862 100644 --- a/tests/test_new_sources.py +++ b/tests/test_new_sources.py @@ -348,3 +348,41 @@ def test_embedded_image_in_the_report_maps_onto_the_target_dicom_column(client): [row] = mastografia_repository.list(pac_id_hospital="IMG-0001") assert "nĂ³dulo circunscrito" in row.mast_interpretacion assert row.mast_dicom_blob.startswith(b"\x89PNG") + + +def test_a_tea_order_keeps_the_pdf_its_sql_source_carries(client, tmp_path): + """Pins down: `ord_pdf_blob` travels with the order from a SQL source into `ordenes_estudio`, byte for + byte, and an order without a file stays without one. The source is a SQLite file in `tmp_path` holding a + two-row `ordenes_estudio`. Fails if the import schema drops the field or the commit rewrites the bytes.""" + report = text_pdf([["EVALUACION PSICOLOGICA", "ADOS-2 Modulo 2"]]) + url = f"sqlite:///{tmp_path / 'hospital.db'}" + table = sa.Table( + "ordenes_estudio", sa.MetaData(), + sa.Column("ord_id", sa.String(36), primary_key=True), + sa.Column("ord_id_hospital", sa.String(64)), + sa.Column("ord_tipo_estudio", sa.Text), + sa.Column("ord_pdf_blob", sa.LargeBinary), + ) + engine = sa.create_engine(url) + table.create(engine) + with engine.begin() as connection: + connection.execute(sa.insert(table), [ + {"ord_id": "ord-pdf-1", "ord_id_hospital": "ORD-PDF-1", "ord_tipo_estudio": "ADOS2", "ord_pdf_blob": report}, + {"ord_id": "ord-pdf-2", "ord_id_hospital": "ORD-PDF-2", "ord_tipo_estudio": "EEG", "ord_pdf_blob": None}, + ]) + engine.dispose() + source = { + "source_type": "sql", + "target": "tea.OrdenesEstudio", + "column_mapping": {column.name: column.name for column in table.columns}, + "payload": {"connection_string": url, "table": "ordenes_estudio"}, + } + + result = client.post("/imports/commit", json={"sources": [source], "on_conflict": "upsert"}).get_json()["results"][0] + + assert (result["created"], result["rejected"]) == (2, 0), result + + from persistance.tea import ordenes_estudio_repository + + assert ordenes_estudio_repository.get("ord-pdf-1").ord_pdf_blob == report + assert ordenes_estudio_repository.get("ord-pdf-2").ord_pdf_blob is None diff --git a/webapi/fhir/__init__.py b/webapi/fhir/__init__.py index eaeb72f..63b750f 100644 --- a/webapi/fhir/__init__.py +++ b/webapi/fhir/__init__.py @@ -342,13 +342,17 @@ def tea_outcome(status: int, code: str, error: Exception): def _included(tea, queries, e, simulated: bool) -> list[dict]: - """What an episode's search includes: its patient, its steps, its M-CHAT, its diagnosis and its study EEG.""" + """What an episode's search includes: its patient, its steps, its M-CHAT, its ADOS-2, its diagnosis and its study + EEG.""" from sqlalchemy import select - persons = tea._rows(queries, select(tea.PERSONS).where(tea.PERSONS.c.pac_id == e.child["pac_id"])) - resources = [tea.patient(e.child["pac_id"], persons[0]["fecha_nacimiento"] if persons else None, simulated), + pac_id = e.child["pac_id"] + persons = tea._rows(queries, select(tea.PERSONS).where(tea.PERSONS.c.pac_id == pac_id)) + ados2 = tea.ados2_observation(tea._rows(queries, select(tea.ADOS2).where(tea.ADOS2.c.pac_id == pac_id)), + e.pseudonym, simulated) + resources = [tea.patient(pac_id, persons[0]["fecha_nacimiento"] if persons else None, simulated), tea.care_plan(e, simulated)] - resources += [r for r in (tea.mchat_observation(e, simulated), tea.condition(e, simulated)) if r] + resources += [r for r in (tea.mchat_observation(e, simulated), ados2, tea.condition(e, simulated)) if r] if (eeg := e.study_eeg()) is not None: resources += tea.eeg_resources(eeg, e.pseudonym, e.metrics, [], simulated) return resources diff --git a/webapi/imports/schemas/tea/ordenes_estudio.py b/webapi/imports/schemas/tea/ordenes_estudio.py index 72c18ae..bd9d06e 100644 --- a/webapi/imports/schemas/tea/ordenes_estudio.py +++ b/webapi/imports/schemas/tea/ordenes_estudio.py @@ -15,3 +15,4 @@ class OrdenesEstudioImportSchema(ImportSchema): ord_id_hospital: OptionalStr = None ord_tipo_estudio: CatalogValue(_TIPO_ESTUDIO_CATALOG) = None ord_fecha: FlexDate = None + ord_pdf_blob: Optional[bytes] = None