From 2b4e3c7f8e8652c8d003783640b8e2ad28378cf7 Mon Sep 17 00:00:00 2001 From: Ayushi Agrawal Date: Wed, 28 Jan 2026 17:30:16 +0530 Subject: [PATCH 1/4] removing reporting_facility_monthly_follow_ups_and_registration mat view --- ...lity_monthly_follow_up_and_registration.rb | 2 +- config/initializers/structure_dump.rb | 3 +- ...ty_monthly_follow_ups_and_registrations.rb | 143 ++++++++++++ db/structure.sql | 147 +----------- ...nthly_follow_ups_and_registrations_v01.sql | 160 ------------- db/views/reporting_patient_states_v06.sql | 217 ------------------ ...monthly_follow_up_and_registration_spec.rb | 14 +- 7 files changed, 162 insertions(+), 524 deletions(-) create mode 100644 db/migrate/20260128094448_discard_mat_view_reporting_facility_monthly_follow_ups_and_registrations.rb delete mode 100644 db/views/reporting_facility_monthly_follow_ups_and_registrations_v01.sql delete mode 100644 db/views/reporting_patient_states_v06.sql diff --git a/app/models/reports/facility_monthly_follow_up_and_registration.rb b/app/models/reports/facility_monthly_follow_up_and_registration.rb index c54e812a3c..377430a264 100644 --- a/app/models/reports/facility_monthly_follow_up_and_registration.rb +++ b/app/models/reports/facility_monthly_follow_up_and_registration.rb @@ -5,7 +5,7 @@ class FacilityMonthlyFollowUpAndRegistration < Reports::View belongs_to :facility def self.materialized? - true + false end def self.partitioned? diff --git a/config/initializers/structure_dump.rb b/config/initializers/structure_dump.rb index c601281df8..da510e27b7 100644 --- a/config/initializers/structure_dump.rb +++ b/config/initializers/structure_dump.rb @@ -1,3 +1,4 @@ ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags = [ - "--exclude-table=simple_reporting.reporting_patient_states_*" + "--exclude-table=simple_reporting.reporting_patient_states_*", + "--exclude-table=simple_reporting.reporting_facility_monthly_follow_ups_and_registrations_*" ] diff --git a/db/migrate/20260128094448_discard_mat_view_reporting_facility_monthly_follow_ups_and_registrations.rb b/db/migrate/20260128094448_discard_mat_view_reporting_facility_monthly_follow_ups_and_registrations.rb new file mode 100644 index 0000000000..6fe453ee78 --- /dev/null +++ b/db/migrate/20260128094448_discard_mat_view_reporting_facility_monthly_follow_ups_and_registrations.rb @@ -0,0 +1,143 @@ +class DiscardMatViewReportingFacilityMonthlyFollowUpsAndRegistrations < ActiveRecord::Migration[6.1] + def up + drop_view :reporting_facility_monthly_follow_ups_and_registrations, materialized: true + + execute <<~SQL + CREATE VIEW public.reporting_facility_monthly_follow_ups_and_registrations AS SELECT * FROM simple_reporting.reporting_facility_monthly_follow_ups_and_registrations; + SQL + end + + def down + drop_view :reporting_facility_monthly_follow_ups_and_registrations + + execute <<~SQL + CREATE MATERIALIZED VIEW public.reporting_facility_monthly_follow_ups_and_registrations AS + WITH monthly_registration_patient_states AS ( + SELECT reporting_patient_states.registration_facility_id AS facility_id, + reporting_patient_states.month_date, + reporting_patient_states.gender, + reporting_patient_states.hypertension, + reporting_patient_states.diabetes + FROM public.reporting_patient_states + WHERE (reporting_patient_states.months_since_registration = (0)::double precision) + ), registered_patients AS ( + SELECT monthly_registration_patient_states.facility_id, + monthly_registration_patient_states.month_date, + count(*) AS monthly_registrations_all, + count(*) FILTER (WHERE (monthly_registration_patient_states.hypertension = 'yes'::text)) AS monthly_registrations_htn_all, + count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'female'::text))) AS monthly_registrations_htn_female, + count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'male'::text))) AS monthly_registrations_htn_male, + count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'transgender'::text))) AS monthly_registrations_htn_transgender, + count(*) FILTER (WHERE (monthly_registration_patient_states.diabetes = 'yes'::text)) AS monthly_registrations_dm_all, + count(*) FILTER (WHERE ((monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'female'::text))) AS monthly_registrations_dm_female, + count(*) FILTER (WHERE ((monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'male'::text))) AS monthly_registrations_dm_male, + count(*) FILTER (WHERE ((monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'transgender'::text))) AS monthly_registrations_dm_transgender, + count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) OR (monthly_registration_patient_states.diabetes = 'yes'::text))) AS monthly_registrations_htn_or_dm, + count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND (monthly_registration_patient_states.diabetes = 'yes'::text))) AS monthly_registrations_htn_and_dm, + count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND (monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'female'::text))) AS monthly_registrations_htn_and_dm_female, + count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND (monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'male'::text))) AS monthly_registrations_htn_and_dm_male, + count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND (monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'transgender'::text))) AS monthly_registrations_htn_and_dm_transgender, + (count(*) FILTER (WHERE (monthly_registration_patient_states.hypertension = 'yes'::text)) - count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND (monthly_registration_patient_states.diabetes = 'yes'::text)))) AS monthly_registrations_htn_only, + (count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'female'::text))) - count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND (monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'female'::text)))) AS monthly_registrations_htn_only_female, + (count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'male'::text))) - count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND (monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'male'::text)))) AS monthly_registrations_htn_only_male, + (count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'transgender'::text))) - count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND (monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'transgender'::text)))) AS monthly_registrations_htn_only_transgender, + (count(*) FILTER (WHERE (monthly_registration_patient_states.diabetes = 'yes'::text)) - count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND (monthly_registration_patient_states.diabetes = 'yes'::text)))) AS monthly_registrations_dm_only, + (count(*) FILTER (WHERE ((monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'female'::text))) - count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND (monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'female'::text)))) AS monthly_registrations_dm_only_female, + (count(*) FILTER (WHERE ((monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'male'::text))) - count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND (monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'male'::text)))) AS monthly_registrations_dm_only_male, + (count(*) FILTER (WHERE ((monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'transgender'::text))) - count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND (monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'transgender'::text)))) AS monthly_registrations_dm_only_transgender + FROM monthly_registration_patient_states + GROUP BY monthly_registration_patient_states.facility_id, monthly_registration_patient_states.month_date + ), follow_ups AS ( + SELECT reporting_patient_follow_ups.facility_id, + reporting_patient_follow_ups.month_date, + count(DISTINCT reporting_patient_follow_ups.patient_id) AS monthly_follow_ups_all, + count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE (reporting_patient_follow_ups.hypertension = 'yes'::text)) AS monthly_follow_ups_htn_all, + count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'female'::public.gender_enum))) AS monthly_follow_ups_htn_female, + count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'male'::public.gender_enum))) AS monthly_follow_ups_htn_male, + count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'transgender'::public.gender_enum))) AS monthly_follow_ups_htn_transgender, + count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE (reporting_patient_follow_ups.diabetes = 'yes'::text)) AS monthly_follow_ups_dm_all, + count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'female'::public.gender_enum))) AS monthly_follow_ups_dm_female, + count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'male'::public.gender_enum))) AS monthly_follow_ups_dm_male, + count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'transgender'::public.gender_enum))) AS monthly_follow_ups_dm_transgender, + count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) OR (reporting_patient_follow_ups.diabetes = 'yes'::text))) AS monthly_follow_ups_htn_or_dm, + count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.diabetes = 'yes'::text))) AS monthly_follow_ups_htn_and_dm, + count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'female'::public.gender_enum))) AS monthly_follow_ups_htn_and_dm_female, + count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'male'::public.gender_enum))) AS monthly_follow_ups_htn_and_dm_male, + count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'transgender'::public.gender_enum))) AS monthly_follow_ups_htn_and_dm_transgender, + (count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE (reporting_patient_follow_ups.hypertension = 'yes'::text)) - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.diabetes = 'yes'::text)))) AS monthly_follow_ups_htn_only, + (count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'female'::public.gender_enum))) - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'female'::public.gender_enum)))) AS monthly_follow_ups_htn_only_female, + (count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'male'::public.gender_enum))) - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'male'::public.gender_enum)))) AS monthly_follow_ups_htn_only_male, + (count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'transgender'::public.gender_enum))) - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'transgender'::public.gender_enum)))) AS monthly_follow_ups_htn_only_transgender, + (count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE (reporting_patient_follow_ups.diabetes = 'yes'::text)) - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.diabetes = 'yes'::text)))) AS monthly_follow_ups_dm_only, + (count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'female'::public.gender_enum))) - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'female'::public.gender_enum)))) AS monthly_follow_ups_dm_only_female, + (count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'male'::public.gender_enum))) - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'male'::public.gender_enum)))) AS monthly_follow_ups_dm_only_male, + (count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'transgender'::public.gender_enum))) - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'transgender'::public.gender_enum)))) AS monthly_follow_ups_dm_only_transgender + FROM public.reporting_patient_follow_ups + GROUP BY reporting_patient_follow_ups.facility_id, reporting_patient_follow_ups.month_date + ) + SELECT rf.facility_region_slug, + rf.facility_id, + rf.facility_region_id, + rf.block_region_id, + rf.district_region_id, + rf.state_region_id, + cal.month_date, + COALESCE(registered_patients.monthly_registrations_all, (0)::bigint) AS monthly_registrations_all, + COALESCE(registered_patients.monthly_registrations_htn_all, (0)::bigint) AS monthly_registrations_htn_all, + COALESCE(registered_patients.monthly_registrations_htn_male, (0)::bigint) AS monthly_registrations_htn_male, + COALESCE(registered_patients.monthly_registrations_htn_female, (0)::bigint) AS monthly_registrations_htn_female, + COALESCE(registered_patients.monthly_registrations_htn_transgender, (0)::bigint) AS monthly_registrations_htn_transgender, + COALESCE(registered_patients.monthly_registrations_dm_all, (0)::bigint) AS monthly_registrations_dm_all, + COALESCE(registered_patients.monthly_registrations_dm_male, (0)::bigint) AS monthly_registrations_dm_male, + COALESCE(registered_patients.monthly_registrations_dm_female, (0)::bigint) AS monthly_registrations_dm_female, + COALESCE(registered_patients.monthly_registrations_dm_transgender, (0)::bigint) AS monthly_registrations_dm_transgender, + COALESCE(follow_ups.monthly_follow_ups_all, (0)::bigint) AS monthly_follow_ups_all, + COALESCE(follow_ups.monthly_follow_ups_htn_all, (0)::bigint) AS monthly_follow_ups_htn_all, + COALESCE(follow_ups.monthly_follow_ups_htn_female, (0)::bigint) AS monthly_follow_ups_htn_female, + COALESCE(follow_ups.monthly_follow_ups_htn_male, (0)::bigint) AS monthly_follow_ups_htn_male, + COALESCE(follow_ups.monthly_follow_ups_htn_transgender, (0)::bigint) AS monthly_follow_ups_htn_transgender, + COALESCE(follow_ups.monthly_follow_ups_dm_all, (0)::bigint) AS monthly_follow_ups_dm_all, + COALESCE(follow_ups.monthly_follow_ups_dm_female, (0)::bigint) AS monthly_follow_ups_dm_female, + COALESCE(follow_ups.monthly_follow_ups_dm_male, (0)::bigint) AS monthly_follow_ups_dm_male, + COALESCE(follow_ups.monthly_follow_ups_dm_transgender, (0)::bigint) AS monthly_follow_ups_dm_transgender, + COALESCE(registered_patients.monthly_registrations_htn_or_dm, (0)::bigint) AS monthly_registrations_htn_or_dm, + COALESCE(registered_patients.monthly_registrations_htn_only, (0)::bigint) AS monthly_registrations_htn_only, + COALESCE(registered_patients.monthly_registrations_htn_only_male, (0)::bigint) AS monthly_registrations_htn_only_male, + COALESCE(registered_patients.monthly_registrations_htn_only_female, (0)::bigint) AS monthly_registrations_htn_only_female, + COALESCE(registered_patients.monthly_registrations_htn_only_transgender, (0)::bigint) AS monthly_registrations_htn_only_transgender, + COALESCE(registered_patients.monthly_registrations_dm_only, (0)::bigint) AS monthly_registrations_dm_only, + COALESCE(registered_patients.monthly_registrations_dm_only_male, (0)::bigint) AS monthly_registrations_dm_only_male, + COALESCE(registered_patients.monthly_registrations_dm_only_female, (0)::bigint) AS monthly_registrations_dm_only_female, + COALESCE(registered_patients.monthly_registrations_dm_only_transgender, (0)::bigint) AS monthly_registrations_dm_only_transgender, + COALESCE(registered_patients.monthly_registrations_htn_and_dm, (0)::bigint) AS monthly_registrations_htn_and_dm, + COALESCE(registered_patients.monthly_registrations_htn_and_dm_male, (0)::bigint) AS monthly_registrations_htn_and_dm_male, + COALESCE(registered_patients.monthly_registrations_htn_and_dm_female, (0)::bigint) AS monthly_registrations_htn_and_dm_female, + COALESCE(registered_patients.monthly_registrations_htn_and_dm_transgender, (0)::bigint) AS monthly_registrations_htn_and_dm_transgender, + COALESCE(follow_ups.monthly_follow_ups_htn_or_dm, (0)::bigint) AS monthly_follow_ups_htn_or_dm, + COALESCE(follow_ups.monthly_follow_ups_htn_only, (0)::bigint) AS monthly_follow_ups_htn_only, + COALESCE(follow_ups.monthly_follow_ups_htn_only_female, (0)::bigint) AS monthly_follow_ups_htn_only_female, + COALESCE(follow_ups.monthly_follow_ups_htn_only_male, (0)::bigint) AS monthly_follow_ups_htn_only_male, + COALESCE(follow_ups.monthly_follow_ups_htn_only_transgender, (0)::bigint) AS monthly_follow_ups_htn_only_transgender, + COALESCE(follow_ups.monthly_follow_ups_dm_only, (0)::bigint) AS monthly_follow_ups_dm_only, + COALESCE(follow_ups.monthly_follow_ups_dm_only_female, (0)::bigint) AS monthly_follow_ups_dm_only_female, + COALESCE(follow_ups.monthly_follow_ups_dm_only_male, (0)::bigint) AS monthly_follow_ups_dm_only_male, + COALESCE(follow_ups.monthly_follow_ups_dm_only_transgender, (0)::bigint) AS monthly_follow_ups_dm_only_transgender, + COALESCE(follow_ups.monthly_follow_ups_htn_and_dm, (0)::bigint) AS monthly_follow_ups_htn_and_dm, + COALESCE(follow_ups.monthly_follow_ups_htn_and_dm_male, (0)::bigint) AS monthly_follow_ups_htn_and_dm_male, + COALESCE(follow_ups.monthly_follow_ups_htn_and_dm_female, (0)::bigint) AS monthly_follow_ups_htn_and_dm_female, + COALESCE(follow_ups.monthly_follow_ups_htn_and_dm_transgender, (0)::bigint) AS monthly_follow_ups_htn_and_dm_transgender + FROM (((public.reporting_facilities rf + JOIN public.reporting_months cal ON (true)) + LEFT JOIN registered_patients ON (((registered_patients.month_date = cal.month_date) AND (registered_patients.facility_id = rf.facility_id)))) + LEFT JOIN follow_ups ON (((follow_ups.month_date = cal.month_date) AND (follow_ups.facility_id = rf.facility_id)))) + ORDER BY cal.month_date DESC + WITH NO DATA; + SQL + + execute <<~SQL + CREATE INDEX facility_monthly_fr_facility_id ON public.reporting_facility_monthly_follow_ups_and_registrations USING btree (facility_id); + CREATE INDEX facility_monthly_fr_facility_region_id ON public.reporting_facility_monthly_follow_ups_and_registrations USING btree (facility_region_id); + CREATE UNIQUE INDEX facility_monthly_fr_month_date_facility_region_id ON public.reporting_facility_monthly_follow_ups_and_registrations USING btree (month_date, facility_region_id); + SQL + end +end diff --git a/db/structure.sql b/db/structure.sql index be2205108e..921856ce3c 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -4483,130 +4483,10 @@ COMMENT ON COLUMN public.reporting_patient_states.titrated IS 'True, if the pati -- --- Name: reporting_facility_monthly_follow_ups_and_registrations; Type: MATERIALIZED VIEW; Schema: public; Owner: - +-- Name: reporting_facility_monthly_follow_ups_and_registrations; Type: VIEW; Schema: public; Owner: - -- -CREATE MATERIALIZED VIEW public.reporting_facility_monthly_follow_ups_and_registrations AS - WITH monthly_registration_patient_states AS ( - SELECT reporting_patient_states.registration_facility_id AS facility_id, - reporting_patient_states.month_date, - reporting_patient_states.gender, - reporting_patient_states.hypertension, - reporting_patient_states.diabetes - FROM public.reporting_patient_states - WHERE (reporting_patient_states.months_since_registration = (0)::double precision) - ), registered_patients AS ( - SELECT monthly_registration_patient_states.facility_id, - monthly_registration_patient_states.month_date, - count(*) AS monthly_registrations_all, - count(*) FILTER (WHERE (monthly_registration_patient_states.hypertension = 'yes'::text)) AS monthly_registrations_htn_all, - count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'female'::text))) AS monthly_registrations_htn_female, - count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'male'::text))) AS monthly_registrations_htn_male, - count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'transgender'::text))) AS monthly_registrations_htn_transgender, - count(*) FILTER (WHERE (monthly_registration_patient_states.diabetes = 'yes'::text)) AS monthly_registrations_dm_all, - count(*) FILTER (WHERE ((monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'female'::text))) AS monthly_registrations_dm_female, - count(*) FILTER (WHERE ((monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'male'::text))) AS monthly_registrations_dm_male, - count(*) FILTER (WHERE ((monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'transgender'::text))) AS monthly_registrations_dm_transgender, - count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) OR (monthly_registration_patient_states.diabetes = 'yes'::text))) AS monthly_registrations_htn_or_dm, - count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND (monthly_registration_patient_states.diabetes = 'yes'::text))) AS monthly_registrations_htn_and_dm, - count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND (monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'female'::text))) AS monthly_registrations_htn_and_dm_female, - count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND (monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'male'::text))) AS monthly_registrations_htn_and_dm_male, - count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND (monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'transgender'::text))) AS monthly_registrations_htn_and_dm_transgender, - (count(*) FILTER (WHERE (monthly_registration_patient_states.hypertension = 'yes'::text)) - count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND (monthly_registration_patient_states.diabetes = 'yes'::text)))) AS monthly_registrations_htn_only, - (count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'female'::text))) - count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND (monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'female'::text)))) AS monthly_registrations_htn_only_female, - (count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'male'::text))) - count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND (monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'male'::text)))) AS monthly_registrations_htn_only_male, - (count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'transgender'::text))) - count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND (monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'transgender'::text)))) AS monthly_registrations_htn_only_transgender, - (count(*) FILTER (WHERE (monthly_registration_patient_states.diabetes = 'yes'::text)) - count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND (monthly_registration_patient_states.diabetes = 'yes'::text)))) AS monthly_registrations_dm_only, - (count(*) FILTER (WHERE ((monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'female'::text))) - count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND (monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'female'::text)))) AS monthly_registrations_dm_only_female, - (count(*) FILTER (WHERE ((monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'male'::text))) - count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND (monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'male'::text)))) AS monthly_registrations_dm_only_male, - (count(*) FILTER (WHERE ((monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'transgender'::text))) - count(*) FILTER (WHERE ((monthly_registration_patient_states.hypertension = 'yes'::text) AND (monthly_registration_patient_states.diabetes = 'yes'::text) AND ((monthly_registration_patient_states.gender)::text = 'transgender'::text)))) AS monthly_registrations_dm_only_transgender - FROM monthly_registration_patient_states - GROUP BY monthly_registration_patient_states.facility_id, monthly_registration_patient_states.month_date - ), follow_ups AS ( - SELECT reporting_patient_follow_ups.facility_id, - reporting_patient_follow_ups.month_date, - count(DISTINCT reporting_patient_follow_ups.patient_id) AS monthly_follow_ups_all, - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE (reporting_patient_follow_ups.hypertension = 'yes'::text)) AS monthly_follow_ups_htn_all, - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'female'::public.gender_enum))) AS monthly_follow_ups_htn_female, - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'male'::public.gender_enum))) AS monthly_follow_ups_htn_male, - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'transgender'::public.gender_enum))) AS monthly_follow_ups_htn_transgender, - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE (reporting_patient_follow_ups.diabetes = 'yes'::text)) AS monthly_follow_ups_dm_all, - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'female'::public.gender_enum))) AS monthly_follow_ups_dm_female, - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'male'::public.gender_enum))) AS monthly_follow_ups_dm_male, - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'transgender'::public.gender_enum))) AS monthly_follow_ups_dm_transgender, - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) OR (reporting_patient_follow_ups.diabetes = 'yes'::text))) AS monthly_follow_ups_htn_or_dm, - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.diabetes = 'yes'::text))) AS monthly_follow_ups_htn_and_dm, - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'female'::public.gender_enum))) AS monthly_follow_ups_htn_and_dm_female, - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'male'::public.gender_enum))) AS monthly_follow_ups_htn_and_dm_male, - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'transgender'::public.gender_enum))) AS monthly_follow_ups_htn_and_dm_transgender, - (count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE (reporting_patient_follow_ups.hypertension = 'yes'::text)) - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.diabetes = 'yes'::text)))) AS monthly_follow_ups_htn_only, - (count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'female'::public.gender_enum))) - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'female'::public.gender_enum)))) AS monthly_follow_ups_htn_only_female, - (count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'male'::public.gender_enum))) - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'male'::public.gender_enum)))) AS monthly_follow_ups_htn_only_male, - (count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'transgender'::public.gender_enum))) - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'transgender'::public.gender_enum)))) AS monthly_follow_ups_htn_only_transgender, - (count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE (reporting_patient_follow_ups.diabetes = 'yes'::text)) - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.diabetes = 'yes'::text)))) AS monthly_follow_ups_dm_only, - (count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'female'::public.gender_enum))) - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'female'::public.gender_enum)))) AS monthly_follow_ups_dm_only_female, - (count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'male'::public.gender_enum))) - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'male'::public.gender_enum)))) AS monthly_follow_ups_dm_only_male, - (count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'transgender'::public.gender_enum))) - count(DISTINCT reporting_patient_follow_ups.patient_id) FILTER (WHERE ((reporting_patient_follow_ups.hypertension = 'yes'::text) AND (reporting_patient_follow_ups.diabetes = 'yes'::text) AND (reporting_patient_follow_ups.patient_gender = 'transgender'::public.gender_enum)))) AS monthly_follow_ups_dm_only_transgender - FROM public.reporting_patient_follow_ups - GROUP BY reporting_patient_follow_ups.facility_id, reporting_patient_follow_ups.month_date - ) - SELECT rf.facility_region_slug, - rf.facility_id, - rf.facility_region_id, - rf.block_region_id, - rf.district_region_id, - rf.state_region_id, - cal.month_date, - COALESCE(registered_patients.monthly_registrations_all, (0)::bigint) AS monthly_registrations_all, - COALESCE(registered_patients.monthly_registrations_htn_all, (0)::bigint) AS monthly_registrations_htn_all, - COALESCE(registered_patients.monthly_registrations_htn_male, (0)::bigint) AS monthly_registrations_htn_male, - COALESCE(registered_patients.monthly_registrations_htn_female, (0)::bigint) AS monthly_registrations_htn_female, - COALESCE(registered_patients.monthly_registrations_htn_transgender, (0)::bigint) AS monthly_registrations_htn_transgender, - COALESCE(registered_patients.monthly_registrations_dm_all, (0)::bigint) AS monthly_registrations_dm_all, - COALESCE(registered_patients.monthly_registrations_dm_male, (0)::bigint) AS monthly_registrations_dm_male, - COALESCE(registered_patients.monthly_registrations_dm_female, (0)::bigint) AS monthly_registrations_dm_female, - COALESCE(registered_patients.monthly_registrations_dm_transgender, (0)::bigint) AS monthly_registrations_dm_transgender, - COALESCE(follow_ups.monthly_follow_ups_all, (0)::bigint) AS monthly_follow_ups_all, - COALESCE(follow_ups.monthly_follow_ups_htn_all, (0)::bigint) AS monthly_follow_ups_htn_all, - COALESCE(follow_ups.monthly_follow_ups_htn_female, (0)::bigint) AS monthly_follow_ups_htn_female, - COALESCE(follow_ups.monthly_follow_ups_htn_male, (0)::bigint) AS monthly_follow_ups_htn_male, - COALESCE(follow_ups.monthly_follow_ups_htn_transgender, (0)::bigint) AS monthly_follow_ups_htn_transgender, - COALESCE(follow_ups.monthly_follow_ups_dm_all, (0)::bigint) AS monthly_follow_ups_dm_all, - COALESCE(follow_ups.monthly_follow_ups_dm_female, (0)::bigint) AS monthly_follow_ups_dm_female, - COALESCE(follow_ups.monthly_follow_ups_dm_male, (0)::bigint) AS monthly_follow_ups_dm_male, - COALESCE(follow_ups.monthly_follow_ups_dm_transgender, (0)::bigint) AS monthly_follow_ups_dm_transgender, - COALESCE(registered_patients.monthly_registrations_htn_or_dm, (0)::bigint) AS monthly_registrations_htn_or_dm, - COALESCE(registered_patients.monthly_registrations_htn_only, (0)::bigint) AS monthly_registrations_htn_only, - COALESCE(registered_patients.monthly_registrations_htn_only_male, (0)::bigint) AS monthly_registrations_htn_only_male, - COALESCE(registered_patients.monthly_registrations_htn_only_female, (0)::bigint) AS monthly_registrations_htn_only_female, - COALESCE(registered_patients.monthly_registrations_htn_only_transgender, (0)::bigint) AS monthly_registrations_htn_only_transgender, - COALESCE(registered_patients.monthly_registrations_dm_only, (0)::bigint) AS monthly_registrations_dm_only, - COALESCE(registered_patients.monthly_registrations_dm_only_male, (0)::bigint) AS monthly_registrations_dm_only_male, - COALESCE(registered_patients.monthly_registrations_dm_only_female, (0)::bigint) AS monthly_registrations_dm_only_female, - COALESCE(registered_patients.monthly_registrations_dm_only_transgender, (0)::bigint) AS monthly_registrations_dm_only_transgender, - COALESCE(registered_patients.monthly_registrations_htn_and_dm, (0)::bigint) AS monthly_registrations_htn_and_dm, - COALESCE(registered_patients.monthly_registrations_htn_and_dm_male, (0)::bigint) AS monthly_registrations_htn_and_dm_male, - COALESCE(registered_patients.monthly_registrations_htn_and_dm_female, (0)::bigint) AS monthly_registrations_htn_and_dm_female, - COALESCE(registered_patients.monthly_registrations_htn_and_dm_transgender, (0)::bigint) AS monthly_registrations_htn_and_dm_transgender, - COALESCE(follow_ups.monthly_follow_ups_htn_or_dm, (0)::bigint) AS monthly_follow_ups_htn_or_dm, - COALESCE(follow_ups.monthly_follow_ups_htn_only, (0)::bigint) AS monthly_follow_ups_htn_only, - COALESCE(follow_ups.monthly_follow_ups_htn_only_female, (0)::bigint) AS monthly_follow_ups_htn_only_female, - COALESCE(follow_ups.monthly_follow_ups_htn_only_male, (0)::bigint) AS monthly_follow_ups_htn_only_male, - COALESCE(follow_ups.monthly_follow_ups_htn_only_transgender, (0)::bigint) AS monthly_follow_ups_htn_only_transgender, - COALESCE(follow_ups.monthly_follow_ups_dm_only, (0)::bigint) AS monthly_follow_ups_dm_only, - COALESCE(follow_ups.monthly_follow_ups_dm_only_female, (0)::bigint) AS monthly_follow_ups_dm_only_female, - COALESCE(follow_ups.monthly_follow_ups_dm_only_male, (0)::bigint) AS monthly_follow_ups_dm_only_male, - COALESCE(follow_ups.monthly_follow_ups_dm_only_transgender, (0)::bigint) AS monthly_follow_ups_dm_only_transgender, - COALESCE(follow_ups.monthly_follow_ups_htn_and_dm, (0)::bigint) AS monthly_follow_ups_htn_and_dm, - COALESCE(follow_ups.monthly_follow_ups_htn_and_dm_male, (0)::bigint) AS monthly_follow_ups_htn_and_dm_male, - COALESCE(follow_ups.monthly_follow_ups_htn_and_dm_female, (0)::bigint) AS monthly_follow_ups_htn_and_dm_female, - COALESCE(follow_ups.monthly_follow_ups_htn_and_dm_transgender, (0)::bigint) AS monthly_follow_ups_htn_and_dm_transgender - FROM (((public.reporting_facilities rf - JOIN public.reporting_months cal ON (true)) - LEFT JOIN registered_patients ON (((registered_patients.month_date = cal.month_date) AND (registered_patients.facility_id = rf.facility_id)))) - LEFT JOIN follow_ups ON (((follow_ups.month_date = cal.month_date) AND (follow_ups.facility_id = rf.facility_id)))) - ORDER BY cal.month_date DESC - WITH NO DATA; +CREATE VIEW public.reporting_facility_monthly_follow_ups_and_registrations AS SELECT * FROM simple_reporting.reporting_facility_monthly_follow_ups_and_registrations; -- @@ -6866,26 +6746,6 @@ CREATE UNIQUE INDEX clean_medicine_to_dosages__unique_name_and_dosage ON public. CREATE UNIQUE INDEX cphc_facility_mappings_unique_cphc_record ON public.cphc_facility_mappings USING btree (cphc_state_id, cphc_state_name, cphc_district_id, cphc_district_name, cphc_taluka_id, cphc_taluka_name, cphc_phc_id, cphc_phc_name, cphc_subcenter_id, cphc_subcenter_name, cphc_village_id, cphc_village_name); --- --- Name: facility_monthly_fr_facility_id; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX facility_monthly_fr_facility_id ON public.reporting_facility_monthly_follow_ups_and_registrations USING btree (facility_id); - - --- --- Name: facility_monthly_fr_facility_region_id; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX facility_monthly_fr_facility_region_id ON public.reporting_facility_monthly_follow_ups_and_registrations USING btree (facility_region_id); - - --- --- Name: facility_monthly_fr_month_date_facility_region_id; Type: INDEX; Schema: public; Owner: - --- - -CREATE UNIQUE INDEX facility_monthly_fr_month_date_facility_region_id ON public.reporting_facility_monthly_follow_ups_and_registrations USING btree (month_date, facility_region_id); - -- -- Name: facility_monthly_fr_facility_id; Type: INDEX; Schema: simple_reporting; Owner: - -- @@ -8941,5 +8801,6 @@ INSERT INTO "schema_migrations" (version) VALUES ('20251210061204'), ('20251211154907'), ('20251215113615'), -('20251219061210'); +('20251219061210'), +('20260128094448'); diff --git a/db/views/reporting_facility_monthly_follow_ups_and_registrations_v01.sql b/db/views/reporting_facility_monthly_follow_ups_and_registrations_v01.sql deleted file mode 100644 index 1131a32f6f..0000000000 --- a/db/views/reporting_facility_monthly_follow_ups_and_registrations_v01.sql +++ /dev/null @@ -1,160 +0,0 @@ -WITH monthly_registration_patient_states AS -(SELECT - registration_facility_id AS facility_id, - month_date, - gender, - hypertension, - diabetes -FROM reporting_patient_states -WHERE months_since_registration = 0 -), - registered_patients AS - (SELECT - facility_id, - month_date, - - count(*) AS monthly_registrations_all, - count(*) FILTER (WHERE hypertension = 'yes') AS monthly_registrations_htn_all, - count(*) FILTER (WHERE hypertension = 'yes' and gender = 'female') AS monthly_registrations_htn_female, - count(*) FILTER (WHERE hypertension = 'yes' and gender = 'male') AS monthly_registrations_htn_male, - count(*) FILTER (WHERE hypertension = 'yes' and gender = 'transgender') AS monthly_registrations_htn_transgender, - count(*) FILTER (WHERE diabetes = 'yes') AS monthly_registrations_dm_all, - count(*) FILTER (WHERE diabetes = 'yes' and gender = 'female') AS monthly_registrations_dm_female, - count(*) FILTER (WHERE diabetes = 'yes' and gender = 'male') AS monthly_registrations_dm_male, - count(*) FILTER (WHERE diabetes = 'yes' and gender = 'transgender') AS monthly_registrations_dm_transgender, - - count(*) FILTER (WHERE hypertension = 'yes' or diabetes = 'yes') AS monthly_registrations_htn_or_dm, - count(*) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes') AS monthly_registrations_htn_and_dm, - count(*) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and gender = 'female') AS monthly_registrations_htn_and_dm_female, - count(*) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and gender = 'male') AS monthly_registrations_htn_and_dm_male, - count(*) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and gender = 'transgender') AS monthly_registrations_htn_and_dm_transgender, - - count(*) FILTER (WHERE hypertension = 'yes') - - count(*) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes') AS monthly_registrations_htn_only, - count(*) FILTER (WHERE hypertension = 'yes' and gender = 'female') - - count(*) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and gender = 'female') AS monthly_registrations_htn_only_female, - count(*) FILTER (WHERE hypertension = 'yes' and gender = 'male') - - count(*) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and gender = 'male') AS monthly_registrations_htn_only_male, - count(*) FILTER (WHERE hypertension = 'yes' and gender = 'transgender') - - count(*) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and gender = 'transgender')AS monthly_registrations_htn_only_transgender, - - count(*) FILTER (WHERE diabetes = 'yes') - - count(*) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes') AS monthly_registrations_dm_only, - count(*) FILTER (WHERE diabetes = 'yes' and gender = 'female') - - count(*) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and gender = 'female') AS monthly_registrations_dm_only_female, - count(*) FILTER (WHERE diabetes = 'yes' and gender = 'male') - - count(*) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and gender = 'male') AS monthly_registrations_dm_only_male, - count(*) FILTER (WHERE diabetes = 'yes' and gender = 'transgender') - - count(*) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and gender = 'transgender')AS monthly_registrations_dm_only_transgender - FROM monthly_registration_patient_states - GROUP BY facility_id, month_date - ), - follow_ups AS - (SELECT - facility_id, - month_date, - - count(distinct(patient_id)) AS monthly_follow_ups_all, - count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes') AS monthly_follow_ups_htn_all, - count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and patient_gender = 'female') AS monthly_follow_ups_htn_female, - count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and patient_gender = 'male') AS monthly_follow_ups_htn_male, - count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and patient_gender = 'transgender') AS monthly_follow_ups_htn_transgender, - count(distinct(patient_id)) FILTER (WHERE diabetes = 'yes') AS monthly_follow_ups_dm_all, - count(distinct(patient_id)) FILTER (WHERE diabetes = 'yes' and patient_gender = 'female') AS monthly_follow_ups_dm_female, - count(distinct(patient_id)) FILTER (WHERE diabetes = 'yes' and patient_gender = 'male') AS monthly_follow_ups_dm_male, - count(distinct(patient_id)) FILTER (WHERE diabetes = 'yes' and patient_gender = 'transgender') AS monthly_follow_ups_dm_transgender, - - count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' or diabetes = 'yes') AS monthly_follow_ups_htn_or_dm, - count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes') AS monthly_follow_ups_htn_and_dm, - count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and patient_gender = 'female') AS monthly_follow_ups_htn_and_dm_female, - count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and patient_gender = 'male') AS monthly_follow_ups_htn_and_dm_male, - count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and patient_gender = 'transgender') AS monthly_follow_ups_htn_and_dm_transgender, - - count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes') - - count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes') AS monthly_follow_ups_htn_only, - count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and patient_gender = 'female') - - count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and patient_gender = 'female') AS monthly_follow_ups_htn_only_female, - count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and patient_gender = 'male') - - count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and patient_gender = 'male') AS monthly_follow_ups_htn_only_male, - count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and patient_gender = 'transgender') - - count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and patient_gender = 'transgender')AS monthly_follow_ups_htn_only_transgender, - - count(distinct(patient_id)) FILTER (WHERE diabetes = 'yes') - - count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes') AS monthly_follow_ups_dm_only, - count(distinct(patient_id)) FILTER (WHERE diabetes = 'yes' and patient_gender = 'female') - - count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and patient_gender = 'female') AS monthly_follow_ups_dm_only_female, - count(distinct(patient_id)) FILTER (WHERE diabetes = 'yes' and patient_gender = 'male') - - count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and patient_gender = 'male') AS monthly_follow_ups_dm_only_male, - count(distinct(patient_id)) FILTER (WHERE diabetes = 'yes' and patient_gender = 'transgender') - - count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and patient_gender = 'transgender')AS monthly_follow_ups_dm_only_transgender - FROM reporting_patient_follow_ups - GROUP BY facility_id, month_date - ) -SELECT - rf.facility_region_slug, - rf.facility_id, - rf.facility_region_id, - rf.block_region_id, - rf.district_region_id, - rf.state_region_id, - - cal.month_date, - - coalesce(registered_patients.monthly_registrations_all,0) AS monthly_registrations_all, - coalesce(registered_patients.monthly_registrations_htn_all,0) AS monthly_registrations_htn_all, - coalesce(registered_patients.monthly_registrations_htn_male,0) AS monthly_registrations_htn_male, - coalesce(registered_patients.monthly_registrations_htn_female,0) AS monthly_registrations_htn_female, - coalesce(registered_patients.monthly_registrations_htn_transgender,0) AS monthly_registrations_htn_transgender, - coalesce(registered_patients.monthly_registrations_dm_all,0) AS monthly_registrations_dm_all, - coalesce(registered_patients.monthly_registrations_dm_male,0) AS monthly_registrations_dm_male, - coalesce(registered_patients.monthly_registrations_dm_female,0) AS monthly_registrations_dm_female, - coalesce(registered_patients.monthly_registrations_dm_transgender,0) AS monthly_registrations_dm_transgender, - - coalesce(follow_ups.monthly_follow_ups_all,0) AS monthly_follow_ups_all, - coalesce(follow_ups.monthly_follow_ups_htn_all,0) AS monthly_follow_ups_htn_all, - coalesce(follow_ups.monthly_follow_ups_htn_female,0) AS monthly_follow_ups_htn_female, - coalesce(follow_ups.monthly_follow_ups_htn_male,0) AS monthly_follow_ups_htn_male, - coalesce(follow_ups.monthly_follow_ups_htn_transgender,0) AS monthly_follow_ups_htn_transgender, - coalesce(follow_ups.monthly_follow_ups_dm_all,0) AS monthly_follow_ups_dm_all, - coalesce(follow_ups.monthly_follow_ups_dm_female,0) AS monthly_follow_ups_dm_female, - coalesce(follow_ups.monthly_follow_ups_dm_male,0) AS monthly_follow_ups_dm_male, - coalesce(follow_ups.monthly_follow_ups_dm_transgender,0) AS monthly_follow_ups_dm_transgender, - - coalesce(registered_patients.monthly_registrations_htn_or_dm, 0) AS monthly_registrations_htn_or_dm, - coalesce(registered_patients.monthly_registrations_htn_only, 0) AS monthly_registrations_htn_only, - coalesce(registered_patients.monthly_registrations_htn_only_male, 0) AS monthly_registrations_htn_only_male, - coalesce(registered_patients.monthly_registrations_htn_only_female, 0) AS monthly_registrations_htn_only_female, - coalesce(registered_patients.monthly_registrations_htn_only_transgender, 0) AS monthly_registrations_htn_only_transgender, - coalesce(registered_patients.monthly_registrations_dm_only, 0) AS monthly_registrations_dm_only, - coalesce(registered_patients.monthly_registrations_dm_only_male, 0) AS monthly_registrations_dm_only_male, - coalesce(registered_patients.monthly_registrations_dm_only_female, 0) AS monthly_registrations_dm_only_female, - coalesce(registered_patients.monthly_registrations_dm_only_transgender, 0) AS monthly_registrations_dm_only_transgender, - coalesce(registered_patients.monthly_registrations_htn_and_dm, 0) AS monthly_registrations_htn_and_dm, - coalesce(registered_patients.monthly_registrations_htn_and_dm_male, 0) AS monthly_registrations_htn_and_dm_male, - coalesce(registered_patients.monthly_registrations_htn_and_dm_female, 0) AS monthly_registrations_htn_and_dm_female, - coalesce(registered_patients.monthly_registrations_htn_and_dm_transgender, 0) AS monthly_registrations_htn_and_dm_transgender, - - coalesce(follow_ups.monthly_follow_ups_htn_or_dm, 0) AS monthly_follow_ups_htn_or_dm, - coalesce(follow_ups.monthly_follow_ups_htn_only, 0) AS monthly_follow_ups_htn_only, - coalesce(follow_ups.monthly_follow_ups_htn_only_female, 0) AS monthly_follow_ups_htn_only_female, - coalesce(follow_ups.monthly_follow_ups_htn_only_male, 0) AS monthly_follow_ups_htn_only_male, - coalesce(follow_ups.monthly_follow_ups_htn_only_transgender, 0) AS monthly_follow_ups_htn_only_transgender, - coalesce(follow_ups.monthly_follow_ups_dm_only, 0) AS monthly_follow_ups_dm_only, - coalesce(follow_ups.monthly_follow_ups_dm_only_female, 0) AS monthly_follow_ups_dm_only_female, - coalesce(follow_ups.monthly_follow_ups_dm_only_male, 0) AS monthly_follow_ups_dm_only_male, - coalesce(follow_ups.monthly_follow_ups_dm_only_transgender, 0) AS monthly_follow_ups_dm_only_transgender, - coalesce(follow_ups.monthly_follow_ups_htn_and_dm, 0) AS monthly_follow_ups_htn_and_dm, - coalesce(follow_ups.monthly_follow_ups_htn_and_dm_male, 0) AS monthly_follow_ups_htn_and_dm_male, - coalesce(follow_ups.monthly_follow_ups_htn_and_dm_female, 0) AS monthly_follow_ups_htn_and_dm_female, - coalesce(follow_ups.monthly_follow_ups_htn_and_dm_transgender, 0) AS monthly_follow_ups_htn_and_dm_transgender -FROM reporting_facilities rf -INNER JOIN reporting_months cal --- ensure a row for every facility and month combination - ON TRUE -LEFT OUTER JOIN registered_patients - ON registered_patients.month_date = cal.month_date - AND registered_patients.facility_id = rf.facility_id -LEFT OUTER JOIN follow_ups - ON follow_ups.month_date = cal.month_date - AND follow_ups.facility_id = rf.facility_id -ORDER BY cal.month_date desc diff --git a/db/views/reporting_patient_states_v06.sql b/db/views/reporting_patient_states_v06.sql deleted file mode 100644 index 75ca4156db..0000000000 --- a/db/views/reporting_patient_states_v06.sql +++ /dev/null @@ -1,217 +0,0 @@ -SELECT - DISTINCT ON (p.id, month_date) - ------------------------------------------------------------ - -- basic patient identifiers - p.id as patient_id, - p.recorded_at AT TIME ZONE 'UTC' AT TIME ZONE 'UTC' as recorded_at, - p.status, - p.gender, - p.age, - p.age_updated_at AT TIME ZONE 'UTC' AT TIME ZONE 'UTC' AS age_updated_at, - p.date_of_birth, - EXTRACT(YEAR - FROM COALESCE( - age(p.date_of_birth), - make_interval(years => p.age) + age(p.age_updated_at) - ) - ) AS current_age, - - ------------------------------------------------------------ - -- data for the month of - cal.month_date, - cal.month, - cal.quarter, - cal.year, - cal.month_string, - cal.quarter_string, - - ------------------------------------------------------------ - -- medical history - mh.hypertension as hypertension, - mh.prior_heart_attack as prior_heart_attack, - mh.prior_stroke as prior_stroke, - mh.chronic_kidney_disease as chronic_kidney_disease, - mh.receiving_treatment_for_hypertension as receiving_treatment_for_hypertension, - mh.diabetes as diabetes, - - ------------------------------------------------------------ - -- information on assigned facility and parent regions - p.assigned_facility_id AS assigned_facility_id, - assigned_facility.facility_size as assigned_facility_size, - assigned_facility.facility_type as assigned_facility_type, - assigned_facility.facility_region_slug as assigned_facility_slug, - assigned_facility.facility_region_id as assigned_facility_region_id, - assigned_facility.block_slug as assigned_block_slug, - assigned_facility.block_region_id as assigned_block_region_id, - assigned_facility.district_slug as assigned_district_slug, - assigned_facility.district_region_id as assigned_district_region_id, - assigned_facility.state_slug as assigned_state_slug, - assigned_facility.state_region_id as assigned_state_region_id, - assigned_facility.organization_slug as assigned_organization_slug, - assigned_facility.organization_region_id as assigned_organization_region_id, - - ------------------------------------------------------------ - -- information on registration facility and parent regions - p.registration_facility_id AS registration_facility_id, - registration_facility.facility_size as registration_facility_size, - registration_facility.facility_type as registration_facility_type, - registration_facility.facility_region_slug as registration_facility_slug, - registration_facility.facility_region_id as registration_facility_region_id, - registration_facility.block_slug as registration_block_slug, - registration_facility.block_region_id as registration_block_region_id, - registration_facility.district_slug as registration_district_slug, - registration_facility.district_region_id as registration_district_region_id, - registration_facility.state_slug as registration_state_slug, - registration_facility.state_region_id as registration_state_region_id, - registration_facility.organization_slug as registration_organization_slug, - registration_facility.organization_region_id as registration_organization_region_id, - - ------------------------------------------------------------ - -- details of the visit: latest BP, BS, encounter, prescription drug and appointment - bps.blood_pressure_id as blood_pressure_id, - bps.blood_pressure_facility_id AS bp_facility_id, - bps.blood_pressure_recorded_at AS bp_recorded_at, - bps.systolic, - bps.diastolic, - - bss.blood_sugar_id as blood_sugar_id, - bss.blood_sugar_facility_id as bs_facility_id, - bss.blood_sugar_recorded_at as bs_recorded_at, - bss.blood_sugar_type as blood_sugar_type, - bss.blood_sugar_value as blood_sugar_value, - bss.blood_sugar_risk_state as blood_sugar_risk_state, - - visits.encounter_id AS encounter_id, - visits.encounter_recorded_at AS encounter_recorded_at, - - visits.prescription_drug_id AS prescription_drug_id, - visits.prescription_drug_recorded_at AS prescription_drug_recorded_at, - - visits.appointment_id AS appointment_id, - visits.appointment_recorded_at AS appointment_recorded_at, - - visits.visited_facility_ids as visited_facility_ids, - ------------------------------------------------------------ - -- relative time calculations - - (cal.year - DATE_PART('year', p.recorded_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')))) * 12 + - (cal.month - DATE_PART('month', p.recorded_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')))) - AS months_since_registration, - - (cal.year - DATE_PART('year', p.recorded_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')))) * 4 + - (cal.quarter - DATE_PART('quarter', p.recorded_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')))) - AS quarters_since_registration, - - visits.months_since_visit AS months_since_visit, - visits.quarters_since_visit AS quarters_since_visit, - bps.months_since_bp AS months_since_bp, - bps.quarters_since_bp AS quarters_since_bp, - bss.months_since_bs AS months_since_bs, - bss.quarters_since_bs AS quarters_since_bs, - - ------------------------------------------------------------ - -- indicators - CASE - WHEN (bps.systolic IS NULL OR bps.diastolic IS NULL) THEN 'unknown' - WHEN (bps.systolic < 140 AND bps.diastolic < 90) THEN 'controlled' - ELSE 'uncontrolled' - END - AS last_bp_state, - - CASE - WHEN p.status = 'dead' THEN 'dead' - WHEN ( - -- months_since_registration - (cal.year - DATE_PART('year', p.recorded_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')))) * 12 + - (cal.month - DATE_PART('month', p.recorded_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')))) < 12 - - OR - - visits.months_since_visit < 12 - ) THEN 'under_care' - ELSE 'lost_to_follow_up' - END - AS htn_care_state, - - CASE - WHEN (visits.months_since_visit >= 3 OR visits.months_since_visit is NULL) THEN 'missed_visit' - WHEN (bps.months_since_bp >= 3 OR bps.months_since_bp is NULL) THEN 'visited_no_bp' - WHEN (bps.systolic < 140 AND bps.diastolic < 90) THEN 'controlled' - ELSE 'uncontrolled' - END - AS htn_treatment_outcome_in_last_3_months, - - CASE - WHEN (visits.months_since_visit >= 2 OR visits.months_since_visit is NULL) THEN 'missed_visit' - WHEN (bps.months_since_bp >= 2 OR bps.months_since_bp is NULL) THEN 'visited_no_bp' - WHEN (bps.systolic < 140 AND bps.diastolic < 90) THEN 'controlled' - ELSE 'uncontrolled' - END - AS htn_treatment_outcome_in_last_2_months, - - CASE - WHEN (visits.quarters_since_visit >= 1 OR visits.quarters_since_visit is NULL) THEN 'missed_visit' - WHEN (bps.quarters_since_bp >= 1 OR bps.quarters_since_bp is NULL) THEN 'visited_no_bp' - WHEN (bps.systolic < 140 AND bps.diastolic < 90) THEN 'controlled' - ELSE 'uncontrolled' - END - AS htn_treatment_outcome_in_quarter, - - CASE - WHEN (visits.months_since_visit >= 3 OR visits.months_since_visit is NULL) THEN 'missed_visit' - WHEN (bss.months_since_bs >= 3 OR bss.months_since_bs is NULL) THEN 'visited_no_bs' - ELSE bss.blood_sugar_risk_state - END - AS diabetes_treatment_outcome_in_last_3_months, - - CASE - WHEN (visits.months_since_visit >= 2 OR visits.months_since_visit is NULL) THEN 'missed_visit' - WHEN (bss.months_since_bs >= 2 OR bss.months_since_bs is NULL) THEN 'visited_no_bs' - ELSE bss.blood_sugar_risk_state - END - AS diabetes_treatment_outcome_in_last_2_months, - - CASE - WHEN (visits.quarters_since_visit >= 1 OR visits.quarters_since_visit is NULL) THEN 'missed_visit' - WHEN (bss.quarters_since_bs >= 1 OR bss.quarters_since_bs is NULL) THEN 'visited_no_bs' - ELSE bss.blood_sugar_risk_state - END - AS diabetes_treatment_outcome_in_quarter, - - (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 - -FROM patients p -LEFT OUTER JOIN reporting_months cal - ON to_char(p.recorded_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')), 'YYYY-MM') <= to_char(cal.month_date, 'YYYY-MM') - --- Only fetch BPs and visits that happened on or before the selected calendar month --- We use year and month comparisons to avoid timezone errors -LEFT OUTER JOIN reporting_patient_blood_pressures bps - ON p.id = bps.patient_id AND cal.month = bps.month AND cal.year = bps.year -LEFT OUTER JOIN reporting_patient_blood_sugars bss - ON p.id = bss.patient_id AND cal.month = bss.month AND cal.year = bss.year -LEFT OUTER JOIN reporting_patient_visits visits - ON p.id = visits.patient_id AND cal.month = visits.month AND cal.year = visits.year -LEFT OUTER JOIN medical_histories mh - ON p.id = mh.patient_id - AND mh.deleted_at IS NULL -LEFT OUTER JOIN reporting_prescriptions current_meds - ON current_meds.patient_id = p.id - AND cal.month_date = current_meds.month_date -LEFT OUTER JOIN reporting_prescriptions past_meds - ON past_meds.patient_id = p.id - AND cal.month_date = (past_meds.month_date + '1 month'::interval) -INNER JOIN reporting_facilities registration_facility - ON registration_facility.facility_id = p.registration_facility_id -INNER JOIN reporting_facilities assigned_facility - ON assigned_facility.facility_id = p.assigned_facility_id -WHERE p.deleted_at IS NULL -ORDER BY - p.id, - cal.month_date ASC diff --git a/spec/models/reports/facility_monthly_follow_up_and_registration_spec.rb b/spec/models/reports/facility_monthly_follow_up_and_registration_spec.rb index ecca964479..93ea1492db 100644 --- a/spec/models/reports/facility_monthly_follow_up_and_registration_spec.rb +++ b/spec/models/reports/facility_monthly_follow_up_and_registration_spec.rb @@ -14,7 +14,7 @@ patient = create(:patient, recorded_at: six_months_ago) create(:blood_pressure, patient: patient, user: user, facility: facility, recorded_at: six_months_ago) patient.discard - RefreshReportingViews.call + described_class.partitioned_refresh(six_months_ago.to_date.beginning_of_month) result = described_class.find_by(facility: facility, month_date: six_months_ago.to_date) expect(result.monthly_registrations_htn_or_dm).to eq(0) expect(result.monthly_follow_ups_htn_or_dm).to eq(0) @@ -26,7 +26,7 @@ six_months_ago = june_2021[:six_months_ago] patient = create(:patient, :without_medical_history, recorded_at: six_months_ago) create(:blood_pressure, patient: patient, user: user, facility: facility, recorded_at: six_months_ago) - RefreshReportingViews.call + described_class.partitioned_refresh(six_months_ago.to_date.beginning_of_month) result = described_class.find_by(facility: facility, month_date: six_months_ago.to_date) expect(result.monthly_registrations_htn_or_dm).to eq(0) expect(result.monthly_follow_ups_htn_or_dm).to eq(0) @@ -42,6 +42,7 @@ create_list(:patient, 3, :hypertension, recorded_at: two_years_ago, gender: :male, registration_user: user, registration_facility: facility) create_list(:patient, 1, :hypertension_and_diabetes, recorded_at: two_years_ago, gender: :transgender, registration_user: user, registration_facility: facility) allow(Reports::PatientState).to receive(:get_refresh_months).and_return(ReportingHelpers.get_refresh_months_between_dates(two_years_ago.to_date, two_years_ago.to_date + 3.year)) + allow(described_class).to receive(:get_refresh_months).and_return(ReportingHelpers.get_refresh_months_between_dates(two_years_ago.to_date, two_years_ago.to_date + 3.year)) refresh_views total = described_class.totals(facility) expect(total.monthly_registrations_htn_or_dm).to eq(8) @@ -69,6 +70,7 @@ create(:blood_pressure, patient: patient_2, user: user, facility: facility, recorded_at: six_months_ago) create(:blood_pressure, patient: patient_2, user: user_2, facility: facility, recorded_at: six_months_ago.advance(days: 3)) create(:blood_pressure, patient: patient_3, user: user_2, facility: facility, recorded_at: six_months_ago.advance(days: 3)) + allow(described_class).to receive(:get_refresh_months).and_return(ReportingHelpers.get_refresh_months_between_dates(two_years_ago.to_date, two_years_ago.to_date + 3.year)) refresh_views total = described_class.totals(facility) expect(total.monthly_follow_ups_htn_or_dm).to eq(3) @@ -95,6 +97,7 @@ create(:blood_pressure, patient: patient_2, user: user_2, facility: facility, recorded_at: six_months_ago) allow(Reports::PatientState).to receive(:get_refresh_months).and_return(ReportingHelpers.get_refresh_months_between_dates(two_years_ago.to_date, two_years_ago.to_date + 3.year)) + allow(described_class).to receive(:get_refresh_months).and_return(ReportingHelpers.get_refresh_months_between_dates(two_years_ago.to_date, two_years_ago.to_date + 3.year)) RefreshReportingViews.call two_years_ago_expected = { @@ -211,6 +214,7 @@ create(:blood_pressure, patient: patient_2, user: user_2, facility: facility, recorded_at: six_months_ago.advance(days: 3)) create(:blood_pressure, patient: patient_3, user: user_2, facility: facility, recorded_at: six_months_ago.advance(days: 3)) allow(Reports::PatientState).to receive(:get_refresh_months).and_return(ReportingHelpers.get_refresh_months_between_dates(two_years_ago.to_date, two_years_ago.to_date + 3.year)) + allow(described_class).to receive(:get_refresh_months).and_return(ReportingHelpers.get_refresh_months_between_dates(two_years_ago.to_date, two_years_ago.to_date + 3.year)) refresh_views total = described_class.totals(facility) dashboard_data_six_months_ago = Reports::FacilityState.find_by(facility_id: facility.id, month_date: six_months_ago.to_date.to_s) @@ -231,4 +235,10 @@ expect(described_class.partitioned?).to be(true) end end + + describe "#materialized" do + it "returns false" do + expect(described_class.materialized?).to be(false) + end + end end From 53a2d3cc8124262aa279ff30872c80a3d987709a Mon Sep 17 00:00:00 2001 From: Ayushi Agrawal Date: Wed, 28 Jan 2026 18:51:03 +0530 Subject: [PATCH 2/4] fixing spec failure --- .../facility_yearly_follow_ups_and_registrations_query.rb | 2 +- .../facility_yearly_follow_ups_and_registrations_query_spec.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/queries/facility_yearly_follow_ups_and_registrations_query.rb b/app/queries/facility_yearly_follow_ups_and_registrations_query.rb index 97976391b6..f03eece14c 100644 --- a/app/queries/facility_yearly_follow_ups_and_registrations_query.rb +++ b/app/queries/facility_yearly_follow_ups_and_registrations_query.rb @@ -18,7 +18,7 @@ def initialize(region, current_user) end def call - records = Reports::FacilityMonthlyFollowUpAndRegistration.for_region(@region) + records = Reports::FacilityMonthlyFollowUpAndRegistration.for_region(@region).order(month_date: :desc) records_per_period = records.group_by { |facility| year(facility.month_date) } diff --git a/spec/queries/facility_yearly_follow_ups_and_registrations_query_spec.rb b/spec/queries/facility_yearly_follow_ups_and_registrations_query_spec.rb index 9478a0c8f9..d196001690 100644 --- a/spec/queries/facility_yearly_follow_ups_and_registrations_query_spec.rb +++ b/spec/queries/facility_yearly_follow_ups_and_registrations_query_spec.rb @@ -88,6 +88,7 @@ create(:blood_pressure, patient: patient_2, user: user, facility: facility, recorded_at: 5.months.ago) create(:blood_pressure, patient: patient_2, user: user, facility: facility, recorded_at: 6.months.ago) create(:blood_pressure, patient: patient_3, user: user, facility: facility, recorded_at: 6.months.ago) + allow(Reports::FacilityMonthlyFollowUpAndRegistration).to receive(:get_refresh_months).and_return(ReportingHelpers.get_refresh_months_between_dates(Date.new(2017, 1, 1), Date.today.beginning_of_month)) refresh_views last_year = if Date.today.month >= 1 && Date.today.month < 4 From 8f1327d04728b2e213a3208b66578f667bc67693 Mon Sep 17 00:00:00 2001 From: Ayushi Agrawal Date: Fri, 30 Jan 2026 15:42:14 +0530 Subject: [PATCH 3/4] PR feedbacks --- config/initializers/structure_dump.rb | 4 ++-- lib/tasks/reporting.rake | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/config/initializers/structure_dump.rb b/config/initializers/structure_dump.rb index da510e27b7..36a1830a9c 100644 --- a/config/initializers/structure_dump.rb +++ b/config/initializers/structure_dump.rb @@ -1,4 +1,4 @@ ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags = [ - "--exclude-table=simple_reporting.reporting_patient_states_*", - "--exclude-table=simple_reporting.reporting_facility_monthly_follow_ups_and_registrations_*" + "--exclude-table=simple_reporting.*_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]", # 8-digit date (e.g. reporting_patient_states_20180101) + "--exclude-table=simple_reporting.*_[0-9][0-9][0-9][0-9][0-9][0-9][0-9]" # 7-digit truncated (e.g. long table name + _2018010) ] diff --git a/lib/tasks/reporting.rake b/lib/tasks/reporting.rake index c5a6ac9251..a51fc7071e 100644 --- a/lib/tasks/reporting.rake +++ b/lib/tasks/reporting.rake @@ -33,7 +33,6 @@ namespace :reporting do reporting_facility_appointment_scheduled_days reporting_facility_states reporting_facility_daily_follow_ups_and_registrations - reporting_facility_monthly_follow_ups_and_registrations ].freeze views_to_refresh = if args[:views].present? From 3bb5b40aee5bab01fb4289cd898d57467fc4adba Mon Sep 17 00:00:00 2001 From: Ayushi Agrawal Date: Tue, 3 Feb 2026 14:58:17 +0530 Subject: [PATCH 4/4] standardrb fixes --- config/initializers/structure_dump.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/initializers/structure_dump.rb b/config/initializers/structure_dump.rb index 36a1830a9c..c95f2c5a25 100644 --- a/config/initializers/structure_dump.rb +++ b/config/initializers/structure_dump.rb @@ -1,4 +1,4 @@ ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags = [ - "--exclude-table=simple_reporting.*_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]", # 8-digit date (e.g. reporting_patient_states_20180101) + "--exclude-table=simple_reporting.*_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]", # 8-digit date (e.g. reporting_patient_states_20180101) "--exclude-table=simple_reporting.*_[0-9][0-9][0-9][0-9][0-9][0-9][0-9]" # 7-digit truncated (e.g. long table name + _2018010) ]