"""The gold rows each domain's measures are evaluated over (core.measures), by the name a measure gives (`Measure.rows`).""" from sqlalchemy import inspect, text from persistance.destination import engine READS = { "cacu": { "samples": "select * from gold.cacu_samples", "sample_biopsies": """ select s.*, b.biopsy_id, b.report_date as biopsy_report_date, b.histology, b.severity from gold.cacu_samples as s left join gold.cacu_sample_biopsies as b on b.sample_id = s.sample_id """, "procedures": """ select p.*, s.result, s.hpv, s.hpv_16, s.hpv_18, s.screening_history, s.report_known_at as cytology_known_at, f.first_case, f.first_treatment, f.first_colposcopy from gold.cacu_procedures as p left join gold.cacu_samples as s on s.person_id = p.person_id and s.sample_date = p.sample_date left join gold.cacu_follow_up as f on f.person_id = p.person_id where p.type = :type """, "risk": """ select c.person_id, coalesce(r.index_date, c.index_date) as index_date, r.probability, r.scored_at as known_at from gold.cacu_cohort_surveillance as c join gold.cacu_risk as r on r.person_id = c.person_id where c.excluded_by is null and r.model = :model and r.horizon_years = 0 """, }, "cama": { "mammograms": "select * from gold.cama_mammograms", "biopsies": "select * from gold.cama_biopsies where report_date is not null", "mammogram_biopsies": "select * from gold.cama_mammogram_biopsies", }, "tea": { "screening": "select * from gold.tea_screening", "eegs": "select * from gold.tea_eegs", }, } def rows(domain: str, kind: str, **params) -> list[dict]: """The rows of one kind; none while their tables don't exist (no risk batch yet).""" with engine().connect() as con: if kind == "risk" and not inspect(con).has_table(f"{domain}_risk", schema="gold"): return [] return [dict(r) for r in con.execute(text(READS[domain][kind]), params).mappings()]