Skip to content

Commit 81c113f

Browse files
aagrawalrtslAyushi Agrawal
andauthored
removing materialized reporting_facility_appointment_scheduled_days (#5816)
**Story card:** [SIMPLEBACK-127](https://rtsl.atlassian.net/browse/SIMPLEBACK-127) ## Because We want to discard materialized reporting_facility_appointment_scheduled_days ## This addresses We create a normal view reporting_facility_appointment_scheduled_days under public schema. This will ensure that the existing dashboard and metabase reports don't break. ## Test instructions Clear the cache and restart the server. The dashboards should load fine. Suite tests. --------- Co-authored-by: Ayushi Agrawal <ayushiagrawal@RTSL-P172G93770.local>
1 parent f0c8515 commit 81c113f

13 files changed

Lines changed: 123 additions & 176 deletions

app/models/reports/facility_appointment_scheduled_days.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class FacilityAppointmentScheduledDays < Reports::View
44
belongs_to :facility
55

66
def self.materialized?
7-
true
7+
false
88
end
99

1010
def self.partitioned?
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
class DiscardMatViewReportingFacilityAppointmentScheduledDays < ActiveRecord::Migration[6.1]
2+
def up
3+
drop_view :reporting_facility_appointment_scheduled_days, materialized: true
4+
5+
execute <<~SQL
6+
CREATE VIEW public.reporting_facility_appointment_scheduled_days AS SELECT * FROM simple_reporting.reporting_facility_appointment_scheduled_days;
7+
SQL
8+
end
9+
10+
def down
11+
drop_view :reporting_facility_appointment_scheduled_days
12+
13+
execute <<~SQL
14+
CREATE MATERIALIZED VIEW public.reporting_facility_appointment_scheduled_days AS
15+
WITH latest_medical_histories AS (
16+
SELECT DISTINCT ON (patient_id) mh.*
17+
FROM medical_histories mh
18+
WHERE mh.deleted_at IS NULL
19+
ORDER BY patient_id, mh.device_created_at DESC
20+
),
21+
latest_appointments_per_patient_per_month AS (
22+
SELECT DISTINCT ON (patient_id, month_date) a.*,
23+
lmh.hypertension,
24+
lmh.diabetes,
25+
to_char(a.device_created_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')), 'YYYY-MM-01')::date month_date
26+
FROM appointments a
27+
INNER JOIN patients p ON p.id = a.patient_id
28+
INNER JOIN latest_medical_histories lmh ON lmh.patient_id = a.patient_id
29+
WHERE a.scheduled_date >= date_trunc('day', a.device_created_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')))
30+
AND a.device_created_at >= date_trunc('month', (now() AT TIME ZONE 'UTC') - INTERVAL '6 months')
31+
AND date_trunc('month', a.device_created_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')))
32+
> date_trunc('month', p.recorded_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')))
33+
AND p.deleted_at IS NULL AND p.diagnosed_confirmed_at IS NOT NULL
34+
AND a.deleted_at IS NULL
35+
AND (lmh.hypertension = 'yes' OR lmh.diabetes = 'yes')
36+
ORDER BY a.patient_id, month_date, a.device_created_at desc
37+
),
38+
scheduled_days_distribution AS (
39+
SELECT month_date,
40+
width_bucket(
41+
extract('days' FROM (scheduled_date - date_trunc('day', device_created_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')))))::integer,
42+
array[0, 15, 32, 63]
43+
) bucket,
44+
COUNT(*) number_of_appointments,
45+
hypertension,
46+
diabetes,
47+
creation_facility_id facility_id
48+
FROM latest_appointments_per_patient_per_month
49+
GROUP BY bucket, creation_facility_id, month_date, hypertension, diabetes
50+
)
51+
SELECT facility_id,
52+
month_date,
53+
(SUM(number_of_appointments) FILTER (WHERE bucket = 1 AND hypertension = 'yes'))::integer AS htn_appts_scheduled_0_to_14_days,
54+
(SUM(number_of_appointments) FILTER (WHERE bucket = 2 AND hypertension = 'yes'))::integer AS htn_appts_scheduled_15_to_31_days,
55+
(SUM(number_of_appointments) FILTER (WHERE bucket = 3 AND hypertension = 'yes'))::integer AS htn_appts_scheduled_32_to_62_days,
56+
(SUM(number_of_appointments) FILTER (WHERE bucket = 4 AND hypertension = 'yes'))::integer AS htn_appts_scheduled_more_than_62_days,
57+
(SUM(number_of_appointments) FILTER (WHERE hypertension = 'yes'))::integer htn_total_appts_scheduled,
58+
59+
(SUM(number_of_appointments) FILTER (WHERE bucket = 1 AND diabetes = 'yes'))::integer AS diabetes_appts_scheduled_0_to_14_days,
60+
(SUM(number_of_appointments) FILTER (WHERE bucket = 2 AND diabetes = 'yes'))::integer AS diabetes_appts_scheduled_15_to_31_days,
61+
(SUM(number_of_appointments) FILTER (WHERE bucket = 3 AND diabetes = 'yes'))::integer AS diabetes_appts_scheduled_32_to_62_days,
62+
(SUM(number_of_appointments) FILTER (WHERE bucket = 4 AND diabetes = 'yes'))::integer AS diabetes_appts_scheduled_more_than_62_days,
63+
(SUM(number_of_appointments) FILTER (WHERE diabetes = 'yes'))::integer diabetes_total_appts_scheduled
64+
FROM scheduled_days_distribution
65+
GROUP BY facility_id, month_date
66+
WITH NO DATA;
67+
SQL
68+
69+
execute <<~SQL
70+
CREATE UNIQUE INDEX index_reporting_facility_appointment_scheduled_days ON public.reporting_facility_appointment_scheduled_days USING btree (month_date, facility_id);
71+
SQL
72+
end
73+
end

db/structure.sql

Lines changed: 4 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4296,63 +4296,10 @@ COMMENT ON COLUMN public.reporting_facilities.organization_slug IS 'Human readab
42964296

42974297

42984298
--
4299-
-- Name: reporting_facility_appointment_scheduled_days; Type: MATERIALIZED VIEW; Schema: public; Owner: -
4299+
-- Name: reporting_facility_appointment_scheduled_days; Type: VIEW; Schema: public; Owner: -
43004300
--
43014301

4302-
CREATE MATERIALIZED VIEW public.reporting_facility_appointment_scheduled_days AS
4303-
WITH latest_medical_histories AS (
4304-
SELECT DISTINCT ON (patient_id) mh.*
4305-
FROM public.medical_histories mh
4306-
WHERE mh.deleted_at IS NULL
4307-
ORDER BY patient_id, mh.device_created_at DESC
4308-
),
4309-
latest_appointments_per_patient_per_month AS (
4310-
SELECT DISTINCT ON (patient_id, month_date) a.*,
4311-
lmh.hypertension,
4312-
lmh.diabetes,
4313-
to_char(a.device_created_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')), 'YYYY-MM-01')::date month_date
4314-
FROM public.appointments a
4315-
INNER JOIN public.patients p ON p.id = a.patient_id
4316-
INNER JOIN latest_medical_histories lmh ON lmh.patient_id = a.patient_id
4317-
WHERE a.scheduled_date >= date_trunc('day', a.device_created_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')))
4318-
AND a.device_created_at >= date_trunc('month', (now() AT TIME ZONE 'UTC') - INTERVAL '6 months')
4319-
AND date_trunc('month', a.device_created_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')))
4320-
> date_trunc('month', p.recorded_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')))
4321-
AND p.deleted_at IS NULL AND p.diagnosed_confirmed_at IS NOT NULL
4322-
AND a.deleted_at IS NULL
4323-
AND (lmh.hypertension = 'yes' OR lmh.diabetes = 'yes')
4324-
ORDER BY a.patient_id, month_date, a.device_created_at desc
4325-
),
4326-
scheduled_days_distribution AS (
4327-
SELECT month_date,
4328-
width_bucket(
4329-
extract('days' FROM (scheduled_date - date_trunc('day', device_created_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')))))::integer,
4330-
array[0, 15, 32, 63]
4331-
) bucket,
4332-
COUNT(*) number_of_appointments,
4333-
hypertension,
4334-
diabetes,
4335-
creation_facility_id facility_id
4336-
FROM latest_appointments_per_patient_per_month
4337-
GROUP BY bucket, creation_facility_id, month_date, hypertension, diabetes)
4338-
4339-
SELECT facility_id,
4340-
month_date,
4341-
(SUM(number_of_appointments) FILTER (WHERE bucket = 1 AND hypertension = 'yes'))::integer AS htn_appts_scheduled_0_to_14_days,
4342-
(SUM(number_of_appointments) FILTER (WHERE bucket = 2 AND hypertension = 'yes'))::integer AS htn_appts_scheduled_15_to_31_days,
4343-
(SUM(number_of_appointments) FILTER (WHERE bucket = 3 AND hypertension = 'yes'))::integer AS htn_appts_scheduled_32_to_62_days,
4344-
(SUM(number_of_appointments) FILTER (WHERE bucket = 4 AND hypertension = 'yes'))::integer AS htn_appts_scheduled_more_than_62_days,
4345-
(SUM(number_of_appointments) FILTER (WHERE hypertension = 'yes'))::integer htn_total_appts_scheduled,
4346-
4347-
(SUM(number_of_appointments) FILTER (WHERE bucket = 1 AND diabetes = 'yes'))::integer AS diabetes_appts_scheduled_0_to_14_days,
4348-
(SUM(number_of_appointments) FILTER (WHERE bucket = 2 AND diabetes = 'yes'))::integer AS diabetes_appts_scheduled_15_to_31_days,
4349-
(SUM(number_of_appointments) FILTER (WHERE bucket = 3 AND diabetes = 'yes'))::integer AS diabetes_appts_scheduled_32_to_62_days,
4350-
(SUM(number_of_appointments) FILTER (WHERE bucket = 4 AND diabetes = 'yes'))::integer AS diabetes_appts_scheduled_more_than_62_days,
4351-
(SUM(number_of_appointments) FILTER (WHERE diabetes = 'yes'))::integer diabetes_total_appts_scheduled
4352-
FROM scheduled_days_distribution
4353-
GROUP BY facility_id, month_date
4354-
WITH NO DATA;
4355-
4302+
CREATE VIEW public.reporting_facility_appointment_scheduled_days AS SELECT * FROM simple_reporting.reporting_facility_appointment_scheduled_days;
43564303

43574304
--
43584305
-- Name: reporting_facility_daily_follow_ups_and_registrations; Type: MATERIALIZED VIEW; Schema: public; Owner: -
@@ -8584,13 +8531,6 @@ CREATE UNIQUE INDEX index_regions_on_unique_path ON public.regions USING btree (
85848531
CREATE INDEX index_reminder_templates_on_treatment_group_id ON public.reminder_templates USING btree (treatment_group_id);
85858532

85868533

8587-
--
8588-
-- Name: index_reporting_facility_appointment_scheduled_days; Type: INDEX; Schema: public; Owner: -
8589-
--
8590-
8591-
CREATE UNIQUE INDEX index_reporting_facility_appointment_scheduled_days ON public.reporting_facility_appointment_scheduled_days USING btree (month_date, facility_id);
8592-
8593-
85948534
--
85958535
-- Name: index_reporting_patient_follow_ups_on_facility_id; Type: INDEX; Schema: public; Owner: -
85968536
--
@@ -9574,6 +9514,7 @@ INSERT INTO "schema_migrations" (version) VALUES
95749514
('20260513141718'),
95759515
('20260515103653'),
95769516
('20260521101357'),
9577-
('20260528053923');
9517+
('20260528053923'),
9518+
('20260610061836');
95789519

95799520

db/views/reporting_facility_appointment_scheduled_days_v04.sql

Lines changed: 0 additions & 50 deletions
This file was deleted.

db/views/reporting_facility_appointment_scheduled_days_v05.sql

Lines changed: 0 additions & 51 deletions
This file was deleted.

spec/exporters/monthly_district_data/diabetes_data_exporter_spec.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
require "rails_helper"
22

33
describe MonthlyDistrictData::DiabetesDataExporter do
4+
def stub_get_refresh_months_for(*classes)
5+
classes.each do |klass|
6+
allow(klass).to receive(:get_refresh_months)
7+
.and_return(ReportingHelpers.get_refresh_months_between_dates(2.years.ago.to_date, Date.today))
8+
end
9+
end
10+
411
around do |example|
512
# This is in the style of ReportingHelpers::freeze_time_for_reporting_specs.
613
# Since FacilityAppointmentScheduledDays only keeps the last 6 months of data, the date cannot be a
@@ -36,8 +43,7 @@
3643
create(:appointment, facility: @facility1, scheduled_date: Date.today, device_created_at: 63.days.ago, patient: create(:patient, :diabetes, recorded_at: 1.year.ago))
3744

3845
@months = @period.downto(5).reverse.map(&:to_s)
39-
allow(Reports::PatientState).to receive(:get_refresh_months).and_return(ReportingHelpers.get_refresh_months_between_dates(2.years.ago.to_date, Date.today))
40-
allow(Reports::FacilityState).to receive(:get_refresh_months).and_return(ReportingHelpers.get_refresh_months_between_dates(2.years.ago.to_date, Date.today))
46+
stub_get_refresh_months_for(Reports::PatientState, Reports::FacilityState, Reports::FacilityAppointmentScheduledDays)
4147
RefreshReportingViews.refresh_v2
4248
end
4349

spec/exporters/monthly_district_data/hypertension_data_exporter_spec.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
require "rails_helper"
22

33
describe MonthlyDistrictData::HypertensionDataExporter do
4+
def stub_get_refresh_months_for(*classes)
5+
classes.each do |klass|
6+
allow(klass).to receive(:get_refresh_months)
7+
.and_return(ReportingHelpers.get_refresh_months_between_dates(2.years.ago.to_date, Date.today))
8+
end
9+
end
10+
411
around do |example|
512
# This is in the style of ReportingHelpers::freeze_time_for_reporting_specs.
613
# Since FacilityAppointmentScheduledDays only keeps the last 6 months of data, the date cannot be a
@@ -34,8 +41,7 @@
3441
create(:appointment, facility: @facility1, scheduled_date: Date.today, device_created_at: 63.days.ago, patient: create(:patient, recorded_at: 1.year.ago))
3542

3643
@months = @period.downto(5).reverse.map(&:to_s)
37-
allow(Reports::PatientState).to receive(:get_refresh_months).and_return(ReportingHelpers.get_refresh_months_between_dates(2.years.ago.to_date, Date.today))
38-
allow(Reports::FacilityState).to receive(:get_refresh_months).and_return(ReportingHelpers.get_refresh_months_between_dates(2.years.ago.to_date, Date.today))
44+
stub_get_refresh_months_for(Reports::PatientState, Reports::FacilityState, Reports::FacilityAppointmentScheduledDays)
3945
RefreshReportingViews.refresh_v2
4046
end
4147

spec/exporters/monthly_state_data/diabetes_data_exporter_spec.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
require "rails_helper"
22

33
describe MonthlyStateData::DiabetesDataExporter do
4+
def stub_get_refresh_months_for(*classes)
5+
classes.each do |klass|
6+
allow(klass).to receive(:get_refresh_months)
7+
.and_return(ReportingHelpers.get_refresh_months_between_dates(3.months.ago.to_date, Date.today))
8+
end
9+
end
10+
411
around do |example|
512
# This is in the style of ReportingHelpers::freeze_time_for_reporting_specs.
613
# Since FacilityAppointmentScheduledDays only keeps the last 6 months of data, the date cannot be a
@@ -38,8 +45,7 @@
3845
create(:appointment, facility: @facility1, scheduled_date: Date.today, device_created_at: 63.days.ago, patient: create(:patient, :diabetes, recorded_at: 1.year.ago))
3946

4047
@months = @period.downto(5).reverse.map(&:to_s)
41-
allow(Reports::PatientState).to receive(:get_refresh_months).and_return(ReportingHelpers.get_refresh_months_between_dates(3.months.ago.to_date, Date.today))
42-
allow(Reports::FacilityState).to receive(:get_refresh_months).and_return(ReportingHelpers.get_refresh_months_between_dates(3.months.ago.to_date, Date.today))
48+
stub_get_refresh_months_for(Reports::PatientState, Reports::FacilityState, Reports::FacilityAppointmentScheduledDays)
4349
RefreshReportingViews.refresh_v2
4450
end
4551

0 commit comments

Comments
 (0)