Skip to main content

task

Prefect task priming the EHR serving layer during the background flow.

Extracts patient demographic and code data from the EHR datasource and persists the combined records to the ehr_data cache table, in the same two modes v1 supports (see EHRQueryConfig.mode).

What differs from v1 is what happens when the EHR cannot be reached. v1 is a gate: it raises, and because the step is step 1 of a sequential step loop (flows/dag/executor.py) nothing downstream runs — no inference, no calculations, no criteria_matching, no pointer flip. v3 is a primer instead: it stores whatever it did fetch, reports how the run went on its result, and returns a real cache accessor either way, so downstream steps read this run's rows where they exist and earlier runs' rows where they do not.

Best-effort behaviour is unconditional here rather than a config flag, because _semantic_config hashes every config field including defaults (flows/dag/hashing.py): adding a flag would move the ehr_query partition for every template using the config, including the one that keeps fail-fast on v2 and whose ehr_data cache must survive. Which template gets which behaviour is answered by the version in the YAML.

Module

Functions

ehr_criteria_query_task

def ehr_criteria_query_task(    ehr_data_resource: EHRDataResource | None,    cache: CacheProtocol,    task_hash: str,    config: EHRQueryConfig | None = None,    datasource: BaseSource | None = None,    filenames: list[str] | None = None,    patient_ids: CacheAccessor | None = None,)> EHRQueryResult:

Prime the EHR cache for the current run, without failing the run.

Arguments

  • ehr_data_resource: Configured EHR resource (NextGen or FHIR R4). None means no EHR reached this pod's DAG setup: the run continues, nothing is queried, and the result reports EHRRunOutcome.NOT_CONFIGURED.
  • cache: Cache backend to write ehr_data rows to.
  • task_hash: Hash identifying the current task run; written into every row so re-runs upsert via session.merge.
  • config: Step config. config.mode selects between filename-driven and patient-ID-cache-driven lookups, config.fetch_appointments controls whether appointment and encounter history is fetched, and config.observation_codes/config.observation_categories fetch coded Observations matching those codes/categories (merged and deduplicated when both are set). If neither is set, every core FHIR observation-category is fetched by default — see EHRQueryConfig.fetch_options. When config itself is None the defaults are used so existing programmatic callers do not have to pass one.
  • datasource: Imaging datasource. Required in "filename" mode.
  • filenames: Imaging file IDs to process. Required in "filename" mode.
  • patient_ids: Cache accessor over the ehr_patient_ids table (typically wired from ehr_patient_lister.cache in the YAML). Required in "patient_id_cache" mode.

Returns The step result, always carrying a CacheAccessor over this run's ehr_data partition — including when no row was stored, since an unwired accessor costs criteria_matching its EHR columns entirely, and EHR criteria then read as scan evidence with provenance unknown rather than as UNKNOWN EHR evidence.

Raises

  • ValueError: If config.mode is not one this step supports, or the mode's required inputs are missing. A misconfigured step is not an EHR outage.