You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
adding patitioned table for reporting_facility_appointment_scheduled_… (#5813)
…days
**Story card:**
[SIMPLEBACK-124](https://rtsl.atlassian.net/browse/SIMPLEBACK-124)
## Because
We need to move a our mat views to partitioned table
## This addresses
Creating a partitioned table
reporting_facility_appointment_scheduled_days.rb under simple_reporting
schema
Both mat view and partitioned table version will exist for a small time
for monitoring in prod
## Test instructions
Suite test
Co-authored-by: Ayushi Agrawal <ayushiagrawal@RTSL-P172G93770.local>
CREATE OR REPLACE FUNCTION simple_reporting.reporting_facility_appointment_scheduled_days_table_function(date) RETURNS SETOF simple_reporting.reporting_facility_appointment_scheduled_days
25
+
LANGUAGE plpgsql
26
+
AS $_$
27
+
BEGIN
28
+
RETURN QUERY
29
+
WITH latest_medical_histories AS (
30
+
SELECT DISTINCT ON (patient_id) mh.*
31
+
FROM medical_histories mh
32
+
WHERE mh.deleted_at IS NULL
33
+
ORDER BY patient_id, mh.device_created_at DESC
34
+
),
35
+
latest_appointments_per_patient_per_month AS (
36
+
SELECT DISTINCT ON (patient_id, month_date)
37
+
a.*,
38
+
lmh.hypertension,
39
+
lmh.diabetes,
40
+
to_char(a.device_created_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')), 'YYYY-MM-01')::date AS month_date
41
+
FROM appointments a
42
+
INNER JOIN patients p ON p.id = a.patient_id
43
+
INNER JOIN latest_medical_histories lmh ON lmh.patient_id = a.patient_id
44
+
WHERE a.scheduled_date >= date_trunc('day', a.device_created_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')))
45
+
AND date_trunc('month', a.device_created_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE'))) = $1
46
+
AND date_trunc('month', a.device_created_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE'))) > date_trunc('month', p.recorded_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')))
47
+
AND p.deleted_at IS NULL
48
+
AND p.diagnosed_confirmed_at IS NOT NULL
49
+
AND a.deleted_at IS NULL
50
+
AND (lmh.hypertension = 'yes' OR lmh.diabetes = 'yes')
51
+
ORDER BY a.patient_id, month_date, a.device_created_at DESC
52
+
),
53
+
scheduled_days_distribution AS (
54
+
SELECT
55
+
month_date,
56
+
width_bucket(
57
+
extract('days' FROM (scheduled_date - date_trunc('day', device_created_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')))))::integer,
58
+
array[0, 15, 32, 63]
59
+
) AS bucket,
60
+
COUNT(*) AS number_of_appointments,
61
+
hypertension,
62
+
diabetes,
63
+
creation_facility_id AS facility_id
64
+
FROM latest_appointments_per_patient_per_month
65
+
GROUP BY bucket, creation_facility_id, month_date, hypertension, diabetes
66
+
)
67
+
SELECT
68
+
facility_id,
69
+
month_date,
70
+
(SUM(number_of_appointments) FILTER (WHERE bucket = 1 AND hypertension = 'yes'))::integer AS htn_appts_scheduled_0_to_14_days,
71
+
(SUM(number_of_appointments) FILTER (WHERE bucket = 2 AND hypertension = 'yes'))::integer AS htn_appts_scheduled_15_to_31_days,
72
+
(SUM(number_of_appointments) FILTER (WHERE bucket = 3 AND hypertension = 'yes'))::integer AS htn_appts_scheduled_32_to_62_days,
73
+
(SUM(number_of_appointments) FILTER (WHERE bucket = 4 AND hypertension = 'yes'))::integer AS htn_appts_scheduled_more_than_62_days,
74
+
(SUM(number_of_appointments) FILTER (WHERE hypertension = 'yes'))::integer AS htn_total_appts_scheduled,
75
+
(SUM(number_of_appointments) FILTER (WHERE bucket = 1 AND diabetes = 'yes'))::integer AS diabetes_appts_scheduled_0_to_14_days,
76
+
(SUM(number_of_appointments) FILTER (WHERE bucket = 2 AND diabetes = 'yes'))::integer AS diabetes_appts_scheduled_15_to_31_days,
77
+
(SUM(number_of_appointments) FILTER (WHERE bucket = 3 AND diabetes = 'yes'))::integer AS diabetes_appts_scheduled_32_to_62_days,
78
+
(SUM(number_of_appointments) FILTER (WHERE bucket = 4 AND diabetes = 'yes'))::integer AS diabetes_appts_scheduled_more_than_62_days,
79
+
(SUM(number_of_appointments) FILTER (WHERE diabetes = 'yes'))::integer AS diabetes_total_appts_scheduled
80
+
FROM scheduled_days_distribution
81
+
GROUP BY facility_id, month_date;
82
+
END;
83
+
$_$;
84
+
SQL
85
+
end
86
+
87
+
defdown
88
+
execute<<~SQL
89
+
DROP FUNCTION IF EXISTS simple_reporting.reporting_facility_appointment_scheduled_days_table_function(date);
WHEREa.scheduled_date>= date_trunc('day', a.device_created_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')))
1775
+
AND date_trunc('month', a.device_created_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE'))) = $1
1776
+
AND date_trunc('month', a.device_created_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE'))) > date_trunc('month', p.recorded_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')))
1777
+
ANDp.deleted_at IS NULL
1778
+
ANDp.diagnosed_confirmed_atIS NOT NULL
1779
+
ANDa.deleted_at IS NULL
1780
+
AND (lmh.hypertension='yes'ORlmh.diabetes='yes')
1781
+
ORDER BYa.patient_id, month_date, a.device_created_atDESC
1782
+
),
1783
+
scheduled_days_distribution AS (
1784
+
SELECT
1785
+
month_date,
1786
+
width_bucket(
1787
+
extract('days'FROM (scheduled_date - date_trunc('day', device_created_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')))))::integer,
1788
+
array[0, 15, 32, 63]
1789
+
) AS bucket,
1790
+
COUNT(*) AS number_of_appointments,
1791
+
hypertension,
1792
+
diabetes,
1793
+
creation_facility_id AS facility_id
1794
+
FROM latest_appointments_per_patient_per_month
1795
+
GROUP BY bucket, creation_facility_id, month_date, hypertension, diabetes
CREATEINDEXindex_reporting_facility_appointment_scheduled_days_facility_idONsimple_reporting.reporting_facility_appointment_scheduled_days USING btree (facility_id);
0 commit comments