lookup_tally
Per-run tally of EHR lookup outcomes, for the site log and Datadog.
One outcome is kept per patient: a built match beats everything, an
unrecordable answer (the EHR returned the patient but no record could be built)
beats any failure, and among failures the step's existing precedence applies
(outranks: a non-answer outranks an answer, otherwise the first reason wins),
so the counts agree with the reasons stored on the negative ehr_data rows.
NO PHI leaves this module. Searched names are reduced to fixed name-shape flags as they arrive and are not kept; the summary and its log line carry counts, exception class names, configured values and fixed flag names.
Recording an outcome never raises: the tally is telemetry, and a value it cannot read is skipped rather than allowed to fail the step's per-patient loop.
Module
Functions
format_shape_flags
def format_shape_flags(given: object, family: object) ‑> str:The shape flags of a name as sorted comma-joined text, or "none".
Arguments
given: The given name searched on.family: The family name searched on.
Returns Text safe for the site log.
has_digit_in_name
def has_digit_in_name(given: str | None, family: str | None) ‑> bool:Whether a scan name holds a digit, and so reads as device text.
Real names almost never contain digits; labels typed into a device's patient-name field (a room, a test run) usually do.
Arguments
given: The given name from the scan.family: The family name from the scan.
Returns True when either component contains a digit.
name_shape_flags
def name_shape_flags(given: object, family: object) ‑> frozenset[str]:The shape flags a searched name carries.
Arguments
given: The given name searched on. A value that is not a string reads as empty.family: The family name searched on. A value that is not a string reads as empty.
Returns The names of the flags that apply. Never the name itself.
Classes
EHRLookupTally
class EHRLookupTally():Collects one lookup outcome per patient over a run.
Methods
failed
def failed( self, key: str, reason: EHRUnavailableReason, *, error: BaseException | None = None, given: object = None, family: object = None,) ‑> None:Record a failed lookup for this patient.
Arguments
key: The patient'sBitfountPatientID, or the query key when none is derivable.reason: Why the lookup failed.error: The exception raised, when there was one. Only its class name is kept.given: The given name searched on, for name searches. Reduced to shape flags here and not kept; a missing value reads as empty.family: The family name searched on, for name searches. Reduced to shape flags here and not kept; a missing value reads as empty.
fallback_ambiguous
def fallback_ambiguous(self, key: str, strategy: str) ‑> bool:Record a fallback search that matched more than one patient.
The fallback could not say which patient is meant, so it is treated as
finding nobody: the patient keeps the name search's not-found, and is
not counted in ambiguous_match. Counted once per patient and
strategy.
Arguments
key: The patient'sBitfountPatientID, or the query key when none is derivable.strategy: The fallback search that was ambiguous.
Returns True the first time a fallback is ambiguous for this patient.
fallback_errored
def fallback_errored(self, key: str, strategy: str) ‑> bool:Record a fallback search that failed with an error.
Any error but an authentication one, which fails the lookup instead.
Treated as the fallback finding nobody: the patient keeps the name
search's not-found, and is not counted in fetch_failed. Counted once
per patient and strategy.
Arguments
key: The patient'sBitfountPatientID, or the query key when none is derivable.strategy: The fallback search that failed.
Returns True the first time a fallback fails for this patient.
identity_rejected
def identity_rejected(self, key: str, strategy: str) ‑> bool:Record a fallback hit rejected for not matching the scan's identity.
Counted once per patient and strategy, however many of the patient's scan rows were searched, and whether or not another strategy later found them.
Arguments
key: The patient'sBitfountPatientID, or the query key when none is derivable.strategy: The fallback search whose hit was rejected.
Returns True the first time any fallback's hit is rejected for this patient, so the caller can warn once per patient.
matched
def matched(self, key: str, strategy: str = 'name_dob') ‑> None:Record that a lookup built a usable record for this patient.
Called once the record exists, not when the EHR answers: a lookup whose
record could not be built is recorded through unrecordable, and a
record holding demographics and no clinical data (error set to
FETCH_FAILED) through failed. drop_ambiguous_patients runs later,
at store time, and is not reflected here: a patient counted as matched
may still be withheld from the cache as ambiguous.
Arguments
key: The patient'sBitfountPatientID, or the query key when none is derivable.strategy: The search that found them. The first one recorded for a patient is kept.
name_dob_identity
def name_dob_identity(self, key: str, outcome: str) ‑> bool:Record how a name and date of birth search's hit compares with the scan.
For the hits the lookup uses, which no check rejects: the outcome is counted, not enforced. Counted once per patient, the first outcome recorded being kept.
Arguments
key: The patient'sBitfountPatientID, or the query key when none is derivable.outcome: The identity check's outcome category.
Returns True the first time an outcome is recorded for this patient.
summary
def summary( self, records: Iterable[EHRDataRecord], *, backend: str | None, provider: str | None, forbidden: Mapping[str, int], skipped_incomplete_name: int = 0, skipped_unparseable_dob: int = 0, skipped_non_patient_name: int = 0, groups_unsupported: Iterable[str] = (),) ‑> tuple[EHRLookupSummary, str]:Summarise the run, as a telemetry model and a site log line.
Arguments
records: The records this run built, for their fetch states.backend: The EHR querier type.provider: The configured EHR provider.forbidden: HTTP 403 responses per FHIR resource type during this step invocation.skipped_incomplete_name: Rows whose name lacked a part.skipped_unparseable_dob: Rows whose date of birth did not parse.skipped_non_patient_name: Rows whose name read as device text.groups_unsupported: Fetch groups the backend reported unsupported.
Returns The summary and a one-line description built from the same counts.
unrecordable
def unrecordable(self, key: str) ‑> None:Record that the EHR returned this patient but no record was built.
For example, no BitfountPatientID was derivable, so there was no
cache key to store under. Counted in queried but in neither matched
nor any failure bucket. A built match for the same key replaces it; it
replaces any failure for the key, since the EHR did find the patient.
Arguments
key: The patient'sBitfountPatientID, or the query key when none is derivable.