|
| 1 | +module DrRai |
| 2 | + class TitrationQueryFactory < QueryFactory |
| 3 | + def inserter |
| 4 | + base_query do |
| 5 | + "insert into public.dr_rai_data_titrations (month_date, facility_name, follow_up_count, titrated_count, titration_rate)" |
| 6 | + end |
| 7 | + end |
| 8 | + |
| 9 | + def updater |
| 10 | + <<-SQL |
| 11 | + merge into public.dr_rai_data_titrations as existing |
| 12 | + using (#{base_query { "" }}) as incoming |
| 13 | + on existing.month_date = incoming.month_date and existing.facility_name = incoming.facility_name |
| 14 | + when not matched |
| 15 | + insert ( |
| 16 | + month_date, |
| 17 | + facility_name, |
| 18 | + follow_up_count, |
| 19 | + titrated_count, |
| 20 | + titration_rate, |
| 21 | + created_at, |
| 22 | + updated_at |
| 23 | + ) |
| 24 | + values ( |
| 25 | + incoming.month_date, |
| 26 | + incoming.facility_name, |
| 27 | + incoming.follow_up_count, |
| 28 | + incoming.titrated_count, |
| 29 | + incoming.titration_rate, |
| 30 | + now(), -- for created_at |
| 31 | + now(), -- for updated_at |
| 32 | + ) |
| 33 | + when matched and existing.titration_rate != incoming.titration_rate |
| 34 | + update |
| 35 | + set |
| 36 | + follow_up_count = incoming.follow_up_count, |
| 37 | + titrated_count = incoming.titrated_count, |
| 38 | + titration_rate = incoming.titration_rate; |
| 39 | + SQL |
| 40 | + end |
| 41 | + |
| 42 | + private |
| 43 | + |
| 44 | + def base_query |
| 45 | + <<~SQL |
| 46 | + with facility_titrations as ( |
| 47 | + select |
| 48 | + reporting_patient_states.month_date, |
| 49 | + facility_name, |
| 50 | + count(*) as follow_up_count, |
| 51 | + count(*) filter (where titrated) as titrated_count, |
| 52 | + count(*) filter (where titrated)::float / count(*) * 100 as titration_rate |
| 53 | + from reporting_patient_states |
| 54 | + inner join reporting_facilities |
| 55 | + on reporting_facilities.facility_id = reporting_patient_states.bp_facility_id |
| 56 | + where 1 = 1 |
| 57 | + and "public"."reporting_patient_states"."month_date" between date '#{from_date}' and date '#{to_date}' |
| 58 | + and reporting_patient_states.months_since_bp = 0 |
| 59 | + and reporting_patient_states.last_bp_state = 'uncontrolled' |
| 60 | + and reporting_patient_states.months_since_registration > 0 |
| 61 | + group by 1, 2 |
| 62 | + order by 1, 2 |
| 63 | + ), |
| 64 | +
|
| 65 | + selected_facility_titrations as ( |
| 66 | + select |
| 67 | + month_date, |
| 68 | + facility_name, |
| 69 | + follow_up_count, |
| 70 | + titrated_count, |
| 71 | + titration_rate |
| 72 | + from facility_titrations |
| 73 | + where facility_name in ( |
| 74 | + select facility_name |
| 75 | + from reporting_facilities |
| 76 | + ) |
| 77 | + ), |
| 78 | +
|
| 79 | + averages as ( |
| 80 | + select |
| 81 | + month_date, |
| 82 | + 'average' as facility_name, |
| 83 | + sum(follow_up_count) as follow_up_count, |
| 84 | + sum(titrated_count) as titrated_count, |
| 85 | + (sum(titrated_count)::float / sum(follow_up_count)) * 100 as titration_rate |
| 86 | + from facility_titrations |
| 87 | + group by month_date |
| 88 | + ) |
| 89 | +
|
| 90 | + #{yield} |
| 91 | + ( |
| 92 | + select * from selected_facility_titrations |
| 93 | + union all |
| 94 | + select * from averages |
| 95 | + ); |
| 96 | + SQL |
| 97 | + end |
| 98 | + end |
| 99 | +end |
0 commit comments