Skip to main content

snapshots

Reading and pruning the snapshots an EHR patient listing produces.

Only a COMPLETE listing run's snapshot is ever served. A PARTIAL run was cut short by a pagination failure, and a RUNNING one is still writing, so neither describes the EHR's population.

Module​

Functions​

in_failure_cooldown​

def in_failure_cooldown(    cache: CacheProtocol, listing_key: str, cooldown_seconds: float,) ‑> bool:

Whether this listing's newest attempt went unserved too recently to retry.

A pod asks for a refresh at every wave start, so without a cooldown a listing that keeps going wrong pages the whole EHR for hours, over and over, for nothing: load the site's vendor sees and one of the pod's EHR connection slots pays for, while the run that needed the listing waits behind it.

Both ways an attempt can end unserved count. FAILED is a write that could not be made; PARTIAL is paging the EHR dropped or refused part-way, which leaves the previous snapshot served just the same. A COMPLETE run is not an attempt to wait on: the snapshot it wrote is either fresh, or stale enough that the next wave should re-list at once.

Only the newest run counts. A listing that went wrong, then succeeded, then aged out of freshness is due again straight away.

Arguments

  • cache: The pod's background cache.
  • listing_key: The listing to check.
  • cooldown_seconds: How long to wait after an unserved attempt. Zero or less disables the cooldown.

Returns Whether the newest run failed or was cut short, and finished inside the cooldown.

latest_complete_listing​

def latest_complete_listing(    cache: CacheProtocol, listing_key: str,) ‑> EHRPatientListingSnapshot | None:

Return the most recent complete snapshot of listing_key.

Arguments

  • cache: The pod's background cache.
  • listing_key: The listing to look up.

Returns The snapshot, or None when no listing run has ever completed.

listing_max_age​

def listing_max_age() ‑> datetime.timedelta:

Return the age beyond which a snapshot is stale.

listing_progress​

def listing_progress(cache: CacheProtocol, listing_key: str) ‑> ListingProgress | None:

Return how far listing_key's running listing has got.

Reads the newest RUNNING run's snapshot, which the lister writes to in batches as it pages. An older RUNNING run is one a restart orphaned, and no longer grows.

Arguments

  • cache: The pod's background cache.
  • listing_key: The listing to look up.

Returns The run and the patient ID rows it has stored so far, or None when no listing run is running.

newest_listing_truncated​

def newest_listing_truncated(cache: CacheProtocol, listing_key: str) ‑> bool:

Return whether listing_key's newest finished listing run was cut short.

A RUNNING run has not finished, so it is skipped.

Arguments

  • cache: The pod's background cache.
  • listing_key: The listing to look up.

Returns Whether that run is PARTIAL; False when no run has finished.

prune_snapshots​

def prune_snapshots(cache: CacheProtocol, listing_key: str, keep: int = 2) ‑> int:

Delete every snapshot of listing_key but the newest keep complete ones.

Partial and failed runs' snapshots go too: neither is ever served. A RUNNING run's snapshot is left alone, since it may still be writing.

Arguments

  • cache: The pod's background cache.
  • listing_key: The listing to prune.
  • keep: How many complete snapshots to keep.

Returns How many patient ID rows were deleted.

Classes​

EHRPatientListingSnapshot​

class EHRPatientListingSnapshot(    listing_key: str,    run_id: str,    snapshot_key: str,    completed_at: datetime,    patients_listed: int,):

One complete listing of a pod's EHR.

Attributes

  • listing_key: The listing this snapshot belongs to.
  • run_id: The listing run that produced it.
  • snapshot_key: The ehr_patient_ids partition holding its patient IDs.
  • completed_at: When the listing run finished.
  • patients_listed: How many distinct patient IDs it holds.

Variables​

  • listing_key : str
  • patients_listed : int
  • run_id : str
  • snapshot_key : str

Methods​


age​

def age(self, now: datetime | None = None) ‑> datetime.timedelta:

Return how long ago this snapshot's listing run finished.

Arguments

  • now: The current time. Defaults to now, in UTC.

Returns The snapshot's age.

is_fresh​

def is_fresh(self, max_age: timedelta | None = None, now: datetime | None = None) ‑> bool:

Return whether this snapshot is younger than max_age.

Arguments

  • max_age: The oldest a snapshot may be and still be reused. Defaults to listing_max_age().
  • now: The current time. Defaults to now, in UTC.

Returns Whether the snapshot can be reused without listing again.

ListingProgress​

class ListingProgress(run_id: ForwardRef('str'), stored: ForwardRef('int')):

How far a running listing has got.

Attributes

  • run_id: The listing run. A different one is a restarted listing, which counts from zero again.
  • stored: The patient ID rows it has stored so far.

Variables​

  • run_id : str - Alias for field number 0
  • stored : int - Alias for field number 1