server_knowledge
What has been learned about an EHR server, whichever backend talks to it.
The backend-neutral half of a backend's server knowledge: resource types the
server does not serve, searches given up on after timing out and when each is
tested again, whether the server's visual acuity is in documents rather than
Observations, HTTP 403 counts, and capability telemetry, with the findings
kept in a KnowledgeStore. A backend subclasses ServerKnowledge with what
only it can know, as FHIR R4 does with its CapabilityStatement allowlist and
search params (fhir_r4/capabilities.py).
Classes
ServerKnowledge
class ServerKnowledge( provider: str | None = None, *, clock: Callable[[], float] = <built-in function time>, store: KnowledgeStore | None = None,):What one EHR server has shown it does and does not serve.
The findings (resource types the server does not serve, searches given
up on after timing out) are kept in a KnowledgeStore. A store made for
the instance lasts as long as the instance; a store passed in may be
shared by several instances, which then agree on the findings, and may
outlive any one of them. A type found unsupported is acted on for
CONFIRMED_FINDING_LIFETIME. A search given up on is tested again every
so often (see FIRST_RECHECK_AFTER), since a timeout is no evidence
about what the server implements, so it is reported as a transient
failure.
It also counts HTTP 403 responses per resource type (forbidden). A 403
is a missing scope or permission, not a missing type, so it is reported
rather than kept as a finding. The counts are cumulative over the
instance's lifetime, which spans every step of a run that shares it; a
caller wanting one step's share takes the difference across it. The 403
counts, the timeout tallies and the tally of patients whose acuity was
only in documents are this instance's own.
For a provider that reads visual acuity from patients' documents, it marks
a server whose acuity is in those documents and not in Observations
(record_acuity_sources), so the acuity code searches can be skipped while
the mark stands (acuity_in_documents_only). The mark is a finding, so
every instance sharing the store acts on it, and it lapses after
CONFIRMED_FINDING_LIFETIME.
Each finding about the server is also sent as ehr_provider_capability
telemetry under provider, once per process per finding, by the
backend's _report.
Arguments
provider: The EHR provider the server belongs to, reported with each capability finding.clock: Seconds since the epoch, for when findings are next tested or lapse.store: Where findings are kept; a new in-memory store when not given.
Subclasses
Variables
store : KnowledgeStore- Where this instance keeps its findings.
unsupported : frozenset[str]- Resource types a live request revealed the server does not serve.
Methods
acuity_in_documents_only
def acuity_in_documents_only(self) ‑> bool:Whether the server is marked as serving acuity only in documents.
True while the mark record_acuity_sources puts stands, so the acuity
code searches can be skipped. Once it lapses the next patient's
Observations are searched again.
record_acuity_sources
def record_acuity_sources(self, *, in_observations: bool, in_documents: bool) ‑> None:Record where one patient's visual acuity was found.
Acuity in Observations shows the server serves it there: the tally
restarts and any mark is removed, which is reported. Acuity only in
documents adds one to the tally; at ACUITY_IN_DOCUMENTS_AFTER the
server is marked for CONFIRMED_FINDING_LIFETIME, the tally restarts,
and that is reported. While a mark stands the tally stays at zero: the
acuity code searches are skipped then, so a patient shows nothing about
them, and a mark that lapses or is cleared needs as many patients
again. Acuity in neither is no evidence either way.
Arguments
in_observations: Whether the patient'sObservations held a reading for either eye.in_documents: Whether the patient's documents held a reading for either eye.
record_forbidden
def record_forbidden(self, resource_type: str) ‑> bool:Count an HTTP 403 for resource_type.
Arguments
resource_type: The resource type the server refused.
Returns Whether this is the first 403 for the type on this instance, so once per type over this instance's lifetime rather than once per step.
record_responsive
def record_responsive(self, resource_type: str, filters: SearchFilters = ()) ‑> None:Clear the timeout tally for a search.
Call this for any outcome other than a timeout — a not-found answer, an error the server explains, an empty result — since each proves the server is still answering this search, which is evidence against the dead endpoint the breaker exists to detect. It clears only this search's tally: an answer to a different search for the same type says nothing about whether this one is answered.
Arguments
resource_type: The resource type the server answered for.filters: The search's filters (seeSearchFilters).
record_unresponsive
def record_unresponsive(self, resource_type: str, filters: SearchFilters = ()) ‑> None:Record a search that timed out.
Arguments
resource_type: The resource type whose search timed out.filters: The search's filters (seeSearchFilters).
record_unsupported
def record_unsupported(self, resource_type: str) ‑> None:Record that the server does not support resource_type.
Arguments
resource_type: The resource type revealed to be unsupported.
should_skip
def should_skip(self, resource_type: str) ‑> bool:Whether a request for resource_type can be skipped unsent.
True while a finding that the server does not serve the type stands.
Arguments
resource_type: The resource type about to be requested.
should_skip_unresponsive
def should_skip_unresponsive( self, resource_type: str, filters: SearchFilters = (),) ‑> bool:Whether a search is given up on, its recheck not yet due.
True once MAX_CONSECUTIVE_TIMEOUTS searches for the type with these
filters have timed out back-to-back, until the search's recheck is due
(see FIRST_RECHECK_AFTER). Unlike should_skip, this says nothing
about what the server implements — only that it is not answering this
search right now — so callers must report it as a transient failure.
Arguments
resource_type: The resource type about to be searched.filters: The search's filters (seeSearchFilters).