|
| 1 | +class DiscardMatViewReportingOverduePatients < ActiveRecord::Migration[6.1] |
| 2 | + def up |
| 3 | + drop_view :reporting_overdue_patients, materialized: true |
| 4 | + |
| 5 | + execute <<~SQL |
| 6 | + CREATE VIEW public.reporting_overdue_patients AS SELECT * FROM simple_reporting.reporting_overdue_patients; |
| 7 | + SQL |
| 8 | + end |
| 9 | + |
| 10 | + def down |
| 11 | + drop_view :reporting_overdue_patients |
| 12 | + |
| 13 | + execute <<~SQL |
| 14 | + CREATE MATERIALIZED VIEW public.reporting_overdue_patients AS |
| 15 | + WITH patients_with_appointments AS ( |
| 16 | + SELECT DISTINCT ON (rps.patient_id, rps.month_date) |
| 17 | + rps.month_date, |
| 18 | + rps.patient_id, |
| 19 | + rps.hypertension AS hypertension, |
| 20 | + rps.diabetes AS diabetes, |
| 21 | + rps.htn_care_state, |
| 22 | + rps.month, |
| 23 | + rps.quarter, |
| 24 | + rps.year, |
| 25 | + rps.month_string, |
| 26 | + rps.quarter_string, |
| 27 | + rps.assigned_facility_id, |
| 28 | + rps.assigned_facility_slug, |
| 29 | + rps.assigned_facility_region_id, |
| 30 | + rps.assigned_block_slug, |
| 31 | + rps.assigned_block_region_id, |
| 32 | + rps.assigned_district_slug, |
| 33 | + rps.assigned_district_region_id, |
| 34 | + rps.assigned_state_slug, |
| 35 | + rps.assigned_state_region_id, |
| 36 | + rps.assigned_organization_slug, |
| 37 | + rps.assigned_organization_region_id, |
| 38 | + appointments.id AS previous_appointment_id, |
| 39 | + appointments.device_created_at AS previous_appointment_date, |
| 40 | + appointments.scheduled_date AS previous_appointment_schedule_date |
| 41 | + FROM |
| 42 | + reporting_patient_states rps |
| 43 | + LEFT JOIN appointments ON appointments.patient_id = rps.patient_id |
| 44 | + AND appointments.device_created_at < rps.month_date |
| 45 | + WHERE |
| 46 | + rps.status <> 'dead' |
| 47 | + AND rps.month_date > NOW() - INTERVAL '24 months' |
| 48 | + ORDER BY |
| 49 | + rps.patient_id, |
| 50 | + rps.month_date, |
| 51 | + appointments.device_created_at DESC |
| 52 | + ), |
| 53 | + patients_with_appointments_and_visits AS ( |
| 54 | + SELECT |
| 55 | + patients_with_appointments.*, |
| 56 | + visit_id, |
| 57 | + visited_at_after_appointment |
| 58 | + FROM |
| 59 | + patients_with_appointments |
| 60 | + LEFT JOIN lateral ( |
| 61 | + SELECT DISTINCT ON (patient_id) id AS visit_id, |
| 62 | + patient_id, |
| 63 | + recorded_at AS visited_at_after_appointment |
| 64 | + FROM |
| 65 | + blood_sugars |
| 66 | + WHERE |
| 67 | + deleted_at IS NULL |
| 68 | + AND patient_id = patients_with_appointments.patient_id |
| 69 | + AND patients_with_appointments.previous_appointment_date < blood_sugars.recorded_at |
| 70 | + AND blood_sugars.recorded_at < patients_with_appointments.month_date + INTERVAL '1 month' + INTERVAL '15 days' |
| 71 | + UNION ALL ( |
| 72 | + SELECT |
| 73 | + id AS visit_id, |
| 74 | + patient_id, |
| 75 | + recorded_at AS visited_at_after_appointment |
| 76 | + FROM |
| 77 | + blood_pressures |
| 78 | + WHERE |
| 79 | + deleted_at IS NULL |
| 80 | + AND patient_id = patients_with_appointments.patient_id |
| 81 | + AND patients_with_appointments.previous_appointment_date < blood_pressures.recorded_at |
| 82 | + AND blood_pressures.recorded_at < patients_with_appointments.month_date + INTERVAL '1 month' + INTERVAL '15 days' |
| 83 | + ) |
| 84 | + UNION ALL ( |
| 85 | + SELECT |
| 86 | + id AS visit_id, |
| 87 | + patient_id, |
| 88 | + device_created_at AS visited_at_after_appointment |
| 89 | + FROM |
| 90 | + appointments AS patients_with_appointments_visit |
| 91 | + WHERE |
| 92 | + deleted_at IS NULL |
| 93 | + AND patient_id = patients_with_appointments.patient_id |
| 94 | + AND patients_with_appointments.previous_appointment_date < patients_with_appointments_visit.device_created_at |
| 95 | + AND patients_with_appointments_visit.device_created_at < patients_with_appointments.month_date + INTERVAL '1 month' + INTERVAL '15 days' |
| 96 | + ) |
| 97 | + UNION ALL ( |
| 98 | + SELECT |
| 99 | + id AS visit_id, |
| 100 | + patient_id, |
| 101 | + device_created_at AS visited_at_after_appointment |
| 102 | + FROM |
| 103 | + prescription_drugs |
| 104 | + WHERE |
| 105 | + deleted_at IS NULL |
| 106 | + AND patient_id = patients_with_appointments.patient_id |
| 107 | + AND patients_with_appointments.previous_appointment_date < prescription_drugs.device_created_at |
| 108 | + AND prescription_drugs.device_created_at < patients_with_appointments.month_date + INTERVAL '1 month' + INTERVAL '15 days' |
| 109 | + ) |
| 110 | + ORDER BY |
| 111 | + patient_id, |
| 112 | + visited_at_after_appointment |
| 113 | + ) AS visits ON patients_with_appointments.patient_id = visits.patient_id |
| 114 | + ), |
| 115 | + patient_with_call_results AS ( |
| 116 | + SELECT |
| 117 | + DISTINCT ON ( |
| 118 | + patients_with_appointments_and_visits.patient_id, |
| 119 | + patients_with_appointments_and_visits.month_date |
| 120 | + ) patients_with_appointments_and_visits.*, |
| 121 | + previous_call_results.device_created_at AT TIME ZONE 'UTC' AT TIME ZONE 'UTC' AS previous_called_at, |
| 122 | + previous_call_results.result_type AS previous_call_result_type, |
| 123 | + previous_call_results.remove_reason AS previous_call_removed_from_overdue_list_reason, |
| 124 | + next_call_results.device_created_at AT TIME ZONE 'UTC' AT TIME ZONE 'UTC' AS next_called_at, |
| 125 | + next_call_results.result_type AS next_call_result_type, |
| 126 | + next_call_results.remove_reason as next_call_removed_from_overdue_list_reason, |
| 127 | + next_call_results.user_id AS called_by_user_id |
| 128 | + FROM |
| 129 | + patients_with_appointments_and_visits |
| 130 | + LEFT JOIN call_results previous_call_results ON patients_with_appointments_and_visits.patient_id = previous_call_results.patient_id |
| 131 | + AND previous_call_results.device_created_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')) < patients_with_appointments_and_visits.month_date |
| 132 | + AND previous_call_results.device_created_at > previous_appointment_schedule_date |
| 133 | + LEFT JOIN call_results next_call_results ON patients_with_appointments_and_visits.patient_id = next_call_results.patient_id |
| 134 | + AND next_call_results.device_created_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')) >= patients_with_appointments_and_visits.month_date |
| 135 | + AND next_call_results.device_created_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')) < patients_with_appointments_and_visits.month_date + INTERVAL '1 month' |
| 136 | + ORDER BY |
| 137 | + patients_with_appointments_and_visits.patient_id, |
| 138 | + patients_with_appointments_and_visits.month_date, |
| 139 | + next_call_results.device_created_at, |
| 140 | + previous_call_results.device_created_at DESC |
| 141 | + ), |
| 142 | + patient_with_call_results_and_phone AS ( |
| 143 | + SELECT |
| 144 | + DISTINCT ON ( |
| 145 | + patient_with_call_results.patient_id, |
| 146 | + patient_with_call_results.month_date |
| 147 | + ) patient_with_call_results.*, |
| 148 | + patient_phone_numbers.number AS patient_phone_number |
| 149 | + FROM |
| 150 | + patient_with_call_results |
| 151 | + LEFT JOIN patient_phone_numbers ON patient_phone_numbers.patient_id = patient_with_call_results.patient_id |
| 152 | + ORDER BY |
| 153 | + patient_with_call_results.patient_id, |
| 154 | + patient_with_call_results.month_date |
| 155 | + ) |
| 156 | + SELECT |
| 157 | + month_date, |
| 158 | + patient_id, |
| 159 | + hypertension, |
| 160 | + diabetes, |
| 161 | + htn_care_state, |
| 162 | + month, |
| 163 | + quarter, |
| 164 | + year, |
| 165 | + month_string, |
| 166 | + quarter_string, |
| 167 | + assigned_facility_id, |
| 168 | + assigned_facility_slug, |
| 169 | + assigned_facility_region_id, |
| 170 | + assigned_block_slug, |
| 171 | + assigned_block_region_id, |
| 172 | + assigned_district_slug, |
| 173 | + assigned_district_region_id, |
| 174 | + assigned_state_slug, |
| 175 | + assigned_state_region_id, |
| 176 | + assigned_organization_slug, |
| 177 | + assigned_organization_region_id, |
| 178 | + previous_appointment_id, |
| 179 | + previous_appointment_date, |
| 180 | + previous_appointment_schedule_date, |
| 181 | + visited_at_after_appointment, |
| 182 | + called_by_user_id, |
| 183 | + next_called_at, |
| 184 | + previous_called_at, |
| 185 | + next_call_result_type, |
| 186 | + next_call_removed_from_overdue_list_reason, |
| 187 | + previous_call_result_type, |
| 188 | + previous_call_removed_from_overdue_list_reason, |
| 189 | + CASE |
| 190 | + WHEN previous_appointment_id IS NULL THEN 'no' |
| 191 | + WHEN (previous_appointment_schedule_date >= month_date) THEN 'no' |
| 192 | + WHEN ( |
| 193 | + previous_appointment_schedule_date < month_date |
| 194 | + and visited_at_after_appointment < month_date |
| 195 | + ) THEN 'no' |
| 196 | + ELSE 'yes' |
| 197 | + END AS is_overdue, |
| 198 | + CASE |
| 199 | + WHEN next_called_at IS NULL THEN 'no' |
| 200 | + ELSE 'yes' |
| 201 | + END AS has_called, |
| 202 | + CASE |
| 203 | + WHEN visited_at_after_appointment IS NULL |
| 204 | + OR next_called_at IS NULL THEN 'no' |
| 205 | + WHEN visited_at_after_appointment > next_called_at + INTERVAL '15 days' THEN 'no' |
| 206 | + ELSE 'yes' |
| 207 | + END AS has_visited_following_call, |
| 208 | + CASE |
| 209 | + WHEN htn_care_state = 'lost_to_follow_up' THEN 'yes' |
| 210 | + ELSE 'no' |
| 211 | + END AS ltfu, |
| 212 | + CASE |
| 213 | + WHEN htn_care_state = 'under_care' THEN 'yes' |
| 214 | + ELSE 'no' |
| 215 | + END AS under_care, |
| 216 | + CASE |
| 217 | + WHEN patient_phone_number IS NULL THEN 'no' |
| 218 | + ELSE 'yes' |
| 219 | + END AS has_phone, |
| 220 | + CASE |
| 221 | + WHEN previous_call_result_type = 'removed_from_overdue_list' THEN 'yes' |
| 222 | + ELSE 'no' |
| 223 | + END AS removed_from_overdue_list, |
| 224 | + CASE |
| 225 | + WHEN next_call_result_type = 'removed_from_overdue_list' THEN 'yes' |
| 226 | + ELSE 'no' |
| 227 | + END AS removed_from_overdue_list_during_the_month |
| 228 | + FROM |
| 229 | + patient_with_call_results_and_phone |
| 230 | + WITH NO DATA; |
| 231 | + SQL |
| 232 | + |
| 233 | + execute <<~SQL |
| 234 | + CREATE INDEX overdue_patients_assigned_facility_region_id ON public.reporting_overdue_patients USING btree (assigned_facility_region_id); |
| 235 | + CREATE UNIQUE INDEX overdue_patients_month_date_patient_id ON public.reporting_overdue_patients USING btree (month_date, patient_id); |
| 236 | + SQL |
| 237 | + end |
| 238 | +end |
0 commit comments