You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
initial commit for replacing Materialized reporting_patient_states wi… (#5703)
Replacing Materialized public.reporting_patient_states with a normal
view
**Story card:**
[sc-16562](https://app.shortcut.com/simpledotorg/story/16562/replace-old-mat-view-to-the-new-partitioned-table-reporting-patient-states)
## Because
We want to discard materialized reporting_patient_states
## This addresses
We create a normal view reporting_patient_states under public schema.
This will ensure that the existing dashboard and metabase reports don't
break.
In order to drop mat view reporting_patient_states, we need to first
drop the views which is dependent on reporting_patient_states, and
recreate them without data.
We will have to refresh those mat views after this is deployed.
## Test instructions
Clear the cache and restart the server. The dashboards should load fine.
Suite tests.
---------
Co-authored-by: Ayushi Agrawal <[email protected]>
timezone('UTC'::text, timezone('UTC'::text, p.recorded_at)) AS recorded_at,
3600
-
p.status,
3601
-
p.gender,
3602
-
p.age,
3603
-
timezone('UTC'::text, timezone('UTC'::text, p.age_updated_at)) AS age_updated_at,
3604
-
p.date_of_birth,
3605
-
date_part('year'::text, COALESCE(age((p.date_of_birth)::timestamp with time zone), (make_interval(years =>p.age) + age(p.age_updated_at)))) AS current_age,
(((cal.year- date_part('year'::text, timezone(( SELECT current_setting('TIMEZONE'::text) AS current_setting), timezone('UTC'::text, p.recorded_at)))) * (12)::double precision) + (cal.month- date_part('month'::text, timezone(( SELECT current_setting('TIMEZONE'::text) AS current_setting), timezone('UTC'::text, p.recorded_at))))) AS months_since_registration,
3663
-
(((cal.year- date_part('year'::text, timezone(( SELECT current_setting('TIMEZONE'::text) AS current_setting), timezone('UTC'::text, p.recorded_at)))) * (4)::double precision) + (cal.quarter- date_part('quarter'::text, timezone(( SELECT current_setting('TIMEZONE'::text) AS current_setting), timezone('UTC'::text, p.recorded_at))))) AS quarters_since_registration,
3664
-
visits.months_since_visit,
3665
-
visits.quarters_since_visit,
3666
-
bps.months_since_bp,
3667
-
bps.quarters_since_bp,
3668
-
bss.months_since_bs,
3669
-
bss.quarters_since_bs,
3670
-
CASE
3671
-
WHEN ((bps.systolic IS NULL) OR (bps.diastolic IS NULL)) THEN 'unknown'::text
3672
-
WHEN ((bps.systolic<140) AND (bps.diastolic<90)) THEN 'controlled'::text
3673
-
ELSE 'uncontrolled'::text
3674
-
END AS last_bp_state,
3675
-
CASE
3676
-
WHEN ((p.status)::text='dead'::text) THEN 'dead'::text
3677
-
WHEN (((((cal.year- date_part('year'::text, timezone(( SELECT current_setting('TIMEZONE'::text) AS current_setting), timezone('UTC'::text, p.recorded_at)))) * (12)::double precision) + (cal.month- date_part('month'::text, timezone(( SELECT current_setting('TIMEZONE'::text) AS current_setting), timezone('UTC'::text, p.recorded_at))))) < (12)::double precision) OR (visits.months_since_visit< (12)::double precision)) THEN 'under_care'::text
3678
-
ELSE 'lost_to_follow_up'::text
3679
-
END AS htn_care_state,
3680
-
CASE
3681
-
WHEN ((visits.months_since_visit>= (3)::double precision) OR (visits.months_since_visit IS NULL)) THEN 'missed_visit'::text
3682
-
WHEN ((bps.months_since_bp>= (3)::double precision) OR (bps.months_since_bp IS NULL)) THEN 'visited_no_bp'::text
3683
-
WHEN ((bps.systolic<140) AND (bps.diastolic<90)) THEN 'controlled'::text
3684
-
ELSE 'uncontrolled'::text
3685
-
END AS htn_treatment_outcome_in_last_3_months,
3686
-
CASE
3687
-
WHEN ((visits.months_since_visit>= (2)::double precision) OR (visits.months_since_visit IS NULL)) THEN 'missed_visit'::text
3688
-
WHEN ((bps.months_since_bp>= (2)::double precision) OR (bps.months_since_bp IS NULL)) THEN 'visited_no_bp'::text
3689
-
WHEN ((bps.systolic<140) AND (bps.diastolic<90)) THEN 'controlled'::text
3690
-
ELSE 'uncontrolled'::text
3691
-
END AS htn_treatment_outcome_in_last_2_months,
3692
-
CASE
3693
-
WHEN ((visits.quarters_since_visit>= (1)::double precision) OR (visits.quarters_since_visit IS NULL)) THEN 'missed_visit'::text
3694
-
WHEN ((bps.quarters_since_bp>= (1)::double precision) OR (bps.quarters_since_bp IS NULL)) THEN 'visited_no_bp'::text
3695
-
WHEN ((bps.systolic<140) AND (bps.diastolic<90)) THEN 'controlled'::text
3696
-
ELSE 'uncontrolled'::text
3697
-
END AS htn_treatment_outcome_in_quarter,
3698
-
CASE
3699
-
WHEN ((visits.months_since_visit>= (3)::double precision) OR (visits.months_since_visit IS NULL)) THEN 'missed_visit'::text
3700
-
WHEN ((bss.months_since_bs>= (3)::double precision) OR (bss.months_since_bs IS NULL)) THEN 'visited_no_bs'::text
3701
-
ELSE bss.blood_sugar_risk_state
3702
-
END AS diabetes_treatment_outcome_in_last_3_months,
3703
-
CASE
3704
-
WHEN ((visits.months_since_visit>= (2)::double precision) OR (visits.months_since_visit IS NULL)) THEN 'missed_visit'::text
3705
-
WHEN ((bss.months_since_bs>= (2)::double precision) OR (bss.months_since_bs IS NULL)) THEN 'visited_no_bs'::text
3706
-
ELSE bss.blood_sugar_risk_state
3707
-
END AS diabetes_treatment_outcome_in_last_2_months,
3708
-
CASE
3709
-
WHEN ((visits.quarters_since_visit>= (1)::double precision) OR (visits.quarters_since_visit IS NULL)) THEN 'missed_visit'::text
3710
-
WHEN ((bss.quarters_since_bs>= (1)::double precision) OR (bss.quarters_since_bs IS NULL)) THEN 'visited_no_bs'::text
3711
-
ELSE bss.blood_sugar_risk_state
3712
-
END AS diabetes_treatment_outcome_in_quarter,
3713
-
((current_meds.amlodipine>past_meds.amlodipine) OR (current_meds.telmisartan>past_meds.telmisartan) OR (current_meds.losartan>past_meds.losartan) OR (current_meds.atenolol>past_meds.atenolol) OR (current_meds.enalapril>past_meds.enalapril) OR (current_meds.chlorthalidone>past_meds.chlorthalidone) OR (current_meds.hydrochlorothiazide>past_meds.hydrochlorothiazide)) AS titrated
3714
-
FROM (((((((((public.patients p
3715
-
LEFT JOINpublic.reporting_months cal ON ((to_char(timezone(( SELECT current_setting('TIMEZONE'::text) AS current_setting), timezone('UTC'::text, p.recorded_at)), 'YYYY-MM'::text) <= to_char((cal.month_date)::timestamp with time zone, 'YYYY-MM'::text))))
3716
-
LEFT JOINpublic.reporting_patient_blood_pressures bps ON (((p.id=bps.patient_id) AND (cal.month=bps.month) AND (cal.year=bps.year))))
3717
-
LEFT JOINpublic.reporting_patient_blood_sugars bss ON (((p.id=bss.patient_id) AND (cal.month=bss.month) AND (cal.year=bss.year))))
3718
-
LEFT JOINpublic.reporting_patient_visits visits ON (((p.id=visits.patient_id) AND (cal.month=visits.month) AND (cal.year=visits.year))))
3719
-
LEFT JOINpublic.medical_histories mh ON (((p.id=mh.patient_id) AND (mh.deleted_at IS NULL))))
3720
-
LEFT JOINpublic.reporting_prescriptions current_meds ON (((current_meds.patient_id=p.id) AND (cal.month_date=current_meds.month_date))))
3721
-
LEFT JOINpublic.reporting_prescriptions past_meds ON (((past_meds.patient_id=p.id) AND (cal.month_date= (past_meds.month_date+'1 mon'::interval)))))
3722
-
JOINpublic.reporting_facilities registration_facility ON ((registration_facility.facility_id=p.registration_facility_id)))
3723
-
JOINpublic.reporting_facilities assigned_facility ON ((assigned_facility.facility_id=p.assigned_facility_id)))
COMMENT ONMATERIALIZED VIEW public.reporting_patient_states IS 'Monthly summary of a patient''s information and health indicators. This table has one row per patient, per month, from the month of the patient''s registration.';
3603
+
COMMENT ON VIEW public.reporting_patient_states IS 'Monthly summary of a patient''s information and health indicators. This table has one row per patient, per month, from the month of the patient''s registration.';
3734
3604
3735
3605
3736
3606
--
@@ -7843,28 +7713,6 @@ CREATE UNIQUE INDEX index_reporting_facility_appointment_scheduled_days ON publi
7843
7713
7844
7714
CREATEINDEXindex_reporting_patient_follow_ups_on_facility_idONpublic.reporting_patient_follow_ups USING btree (facility_id);
CREATEINDEXpatient_states_month_date_registration_facility_regionONpublic.reporting_patient_states USING btree (month_date, registration_facility_region_id);
0 commit comments