FHIR out from the catalog, the FHIR door, ConceptMap routes; round-trip, new-label and Synthea gates
This commit is contained in:
+41
-4
@@ -5,8 +5,10 @@ from typing import Annotated, Any
|
||||
|
||||
from dagster_graphql import DagsterGraphQLClient, DagsterGraphQLClientError
|
||||
from fastapi import APIRouter, Depends, HTTPException, Path, Query, UploadFile
|
||||
from fhir.resources.R4B.conceptmap import ConceptMap
|
||||
from pydantic import BaseModel
|
||||
|
||||
from core import concept_map
|
||||
from core.pipeline import ID_PATTERN, ImportPath, Pipeline, Source, redacted, restored
|
||||
from core.schema import ENTITIES
|
||||
from persistance import config_log, nodes
|
||||
@@ -90,7 +92,11 @@ def pipeline(pipeline_id: PipelineId, version: int | None = None) -> SavedPipeli
|
||||
|
||||
@router.put("/pipelines/{pipeline_id}")
|
||||
def save_pipeline(pipeline_id: PipelineId, drawn: Pipeline) -> dict[str, int]:
|
||||
"""Saves a new version. Masked secrets sent back are kept as they were saved."""
|
||||
"""Saves a new version. Masked secrets sent back are kept as they were saved; every
|
||||
ConceptMap a node names must exist."""
|
||||
named = {n.concept_map for n in drawn.nodes if getattr(n, "concept_map", None)}
|
||||
if missing := sorted(m for m in named if not config_log.latest_version(m, "concept_maps")):
|
||||
raise HTTPException(422, detail=f"no concept maps {missing}")
|
||||
previous = config_log.get(pipeline_id)[1] if config_log.latest_version(pipeline_id) else None
|
||||
try:
|
||||
complete = restored(drawn, previous)
|
||||
@@ -99,6 +105,33 @@ def save_pipeline(pipeline_id: PipelineId, drawn: Pipeline) -> dict[str, int]:
|
||||
return {"version": config_log.save(pipeline_id, complete)}
|
||||
|
||||
|
||||
@router.get("/concept-maps")
|
||||
def concept_maps() -> Rows:
|
||||
"""Every ConceptMap a site saved: its id, latest version and when."""
|
||||
return config_log.listing("concept_maps")
|
||||
|
||||
|
||||
@router.get("/concept-maps/{map_id}")
|
||||
def get_concept_map(map_id: PipelineId) -> dict[str, Any]:
|
||||
"""A site's codes in platform terms (FHIR R4B ConceptMap), latest version."""
|
||||
if not config_log.latest_version(map_id, "concept_maps"):
|
||||
raise HTTPException(404, detail=f"no concept map {map_id}")
|
||||
return config_log.concept_map(map_id).model_dump(mode="json", exclude_none=True)
|
||||
|
||||
|
||||
@router.put("/concept-maps/{map_id}")
|
||||
def save_concept_map(map_id: PipelineId, doc: dict[str, Any]) -> dict[str, int]:
|
||||
"""Saves a new version of a ConceptMap. Its groups target the systems platform fields
|
||||
take codes from, and only codes those fields allow (core.concept_map)."""
|
||||
try:
|
||||
found = ConceptMap.model_validate({**doc, "resourceType": "ConceptMap"})
|
||||
except ValueError as e:
|
||||
raise HTTPException(422, detail=str(e)) from e
|
||||
if problems := concept_map.problems(found):
|
||||
raise HTTPException(422, detail=problems)
|
||||
return {"version": config_log.save(map_id, found, "concept_maps")}
|
||||
|
||||
|
||||
class RunRequest(BaseModel):
|
||||
version: int | None = None # default: the latest saved
|
||||
|
||||
@@ -112,14 +145,18 @@ def start_run(
|
||||
"""Starts the `ingest` job for this pipeline; follow it at /runs/{run_id}."""
|
||||
if not config_log.latest_version(pipeline_id):
|
||||
raise HTTPException(404, detail=f"no pipeline {pipeline_id}")
|
||||
config = {"pipeline": pipeline_id, "version": request.version}
|
||||
return {"run_id": submit(client, pipeline_id, request.version)}
|
||||
|
||||
|
||||
def submit(client: DagsterGraphQLClient, pipeline_id: str, version: int | None = None) -> str:
|
||||
"""Starts the `ingest` job for a saved pipeline (the latest version by default)."""
|
||||
config = {"pipeline": pipeline_id, "version": version}
|
||||
try:
|
||||
run_id = client.submit_job_execution(
|
||||
return client.submit_job_execution(
|
||||
"ingest", run_config={"ops": {"run_pipeline": {"config": config}}}
|
||||
)
|
||||
except DagsterGraphQLClientError as e:
|
||||
raise HTTPException(502, detail=str(e)) from e
|
||||
return {"run_id": run_id}
|
||||
|
||||
|
||||
@router.get("/pipelines/{pipeline_id}/nodes/{node_id}/rows")
|
||||
|
||||
Reference in New Issue
Block a user