malawi_hiv_program_reports 1.1.4 → 1.1.6
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e9cba775f0bc9cd2eaa2f0d078186ce06a39b90bb2012a55b7e1430e675dd6b
|
4
|
+
data.tar.gz: ffa4ed80f23149a3c41e1a0c90dd31de1a27325275655f2a292c850bae667fb3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0546e2d29b71951cf95ca6df50f82ca5e380e4a203cbbd23287aa48fe536503c1426eba5944693adac9b7291b6e685426659ad73616eebf91bba4694013ea2bb
|
7
|
+
data.tar.gz: 07dca51e1fe44552a13896ecb1106f2e68240b05e462a72985b83bf250716b757c8b862cb37fba6e47333a4e1e8bf35d400500f71555f526a975e9449fd5cbea
|
@@ -5,7 +5,7 @@ module MalawiHivProgramReports
|
|
5
5
|
# Disaggregated cohort report
|
6
6
|
# rubocop:disable Metrics/ClassLength
|
7
7
|
class Disaggregated
|
8
|
-
attr_accessor :type, :start_date, :end_date, :rebuild, :occupation, :report, :maternal, :location
|
8
|
+
attr_accessor :type, :start_date, :end_date, :rebuild, :occupation, :report, :maternal, :location, :aggregation
|
9
9
|
|
10
10
|
include MalawiHivProgramReports::Utils::ModelUtils
|
11
11
|
include MalawiHivProgramReports::Pepfar::Utils
|
@@ -16,6 +16,7 @@ module MalawiHivProgramReports
|
|
16
16
|
@start_date = start_date
|
17
17
|
@end_date = end_date
|
18
18
|
@rebuild = kwargs[:rebuild]&.casecmp?('true')
|
19
|
+
@aggregation = kwargs[:aggregation]&.casecmp?('false')
|
19
20
|
@occupation = kwargs[:occupation]
|
20
21
|
@location = kwargs[:location]
|
21
22
|
@maternal = {}
|
@@ -38,7 +39,7 @@ module MalawiHivProgramReports
|
|
38
39
|
|
39
40
|
def process_initialization
|
40
41
|
init_report
|
41
|
-
init_aggregate_rows
|
42
|
+
init_aggregate_rows if aggregation
|
42
43
|
end
|
43
44
|
|
44
45
|
def init_report
|
@@ -61,7 +62,7 @@ module MalawiHivProgramReports
|
|
61
62
|
|
62
63
|
def init_cohort_section(cursor)
|
63
64
|
cursor['tx_curr'] = []
|
64
|
-
|
65
|
+
COHORT_REGIMENS.each do |regimen|
|
65
66
|
cursor[regimen] = []
|
66
67
|
end
|
67
68
|
cursor['unknown'] = []
|
@@ -77,7 +78,7 @@ module MalawiHivProgramReports
|
|
77
78
|
def process_db_data
|
78
79
|
fetch_data.each do |data|
|
79
80
|
age_group = data['age_group']
|
80
|
-
regimen = data['regimen'] || '
|
81
|
+
regimen = data['regimen'] || 'unknown'
|
81
82
|
patient_id = data['patient_id']
|
82
83
|
# we need to handle regimes that only have one P to become PP. Otherwise if it is already PP or PA we leave
|
83
84
|
# it as is. Regimens are in this format NUMBERLETTERS
|
@@ -86,7 +87,7 @@ module MalawiHivProgramReports
|
|
86
87
|
report[age_group.to_s][gender.to_s][regimen.to_s] << patient_id
|
87
88
|
report[age_group.to_s][gender.to_s]['tx_curr'] << patient_id
|
88
89
|
report[age_group.to_s][gender.to_s]['total'] << patient_id
|
89
|
-
process_aggregate_rows(gender:, regimen:, patient_id:)
|
90
|
+
process_aggregate_rows(gender:, regimen:, patient_id:) if aggregation
|
90
91
|
end
|
91
92
|
end
|
92
93
|
|
@@ -66,6 +66,7 @@ module MalawiHivProgramReports
|
|
66
66
|
# HIC SUNT DRACONIS: The order of the operations below matters,
|
67
67
|
# do not change it unless you know what you are doing!!!
|
68
68
|
load_patients_who_died(start:)
|
69
|
+
load_other_patient_who_died(start:)
|
69
70
|
load_patients_who_stopped_treatment(start:)
|
70
71
|
load_patients_without_drug_orders(start:)
|
71
72
|
load_patients_on_treatment(start:)
|
@@ -94,7 +95,7 @@ module MalawiHivProgramReports
|
|
94
95
|
AND drug_order.site_id = o.site_id AND drug_order.quantity > 0
|
95
96
|
AND drug_order.drug_inventory_id IN (#{arv_drug})
|
96
97
|
WHERE o.order_type_id = 1 -- drug order
|
97
|
-
AND o.start_date < (DATE(#{start ? start_date : end_date}) + INTERVAL 1 DAY)
|
98
|
+
AND o.start_date < (DATE(#{start ? start_date : end_date}) #{start ? '' : '+ INTERVAL 1 DAY'})
|
98
99
|
AND o.voided = 0
|
99
100
|
GROUP BY o.patient_id
|
100
101
|
ON DUPLICATE KEY UPDATE start_date = VALUES(start_date), min_order_date = VALUES(min_order_date)
|
@@ -110,8 +111,8 @@ module MalawiHivProgramReports
|
|
110
111
|
INNER JOIN drug_order PARTITION (p#{location}) ON drug_order.order_id = o.order_id AND drug_order.quantity > 0
|
111
112
|
AND drug_order.drug_inventory_id IN (#{arv_drug})
|
112
113
|
WHERE o.order_type_id = 1 -- drug order
|
113
|
-
AND o.start_date < (DATE(#{start ? start_date : end_date}) + INTERVAL 1 DAY)
|
114
|
-
AND o.start_date >= (DATE(#{start ? prev_date : start_date}) + INTERVAL 1 DAY)
|
114
|
+
AND o.start_date < (DATE(#{start ? start_date : end_date}) #{start ? '' : '+ INTERVAL 1 DAY'})
|
115
|
+
AND o.start_date >= (DATE(#{start ? prev_date : start_date}) #{start ? '' : '+ INTERVAL 1 DAY'})
|
115
116
|
AND o.voided = 0
|
116
117
|
GROUP BY o.patient_id
|
117
118
|
ON DUPLICATE KEY UPDATE start_date = VALUES(start_date), min_order_date = VALUES(min_order_date)
|
@@ -185,7 +186,7 @@ module MalawiHivProgramReports
|
|
185
186
|
SELECT pp.patient_id, #{location}, MAX(ps.start_date) start_date
|
186
187
|
FROM patient_state PARTITION (p#{location}) AS ps
|
187
188
|
INNER JOIN patient_program PARTITION (p#{location}) AS pp ON pp.patient_program_id = ps.patient_program_id AND pp.program_id = 1 AND pp.voided = 0
|
188
|
-
WHERE ps.start_date < DATE(#{end_date}) + INTERVAL 1 DAY
|
189
|
+
WHERE ps.start_date < DATE(#{start ? start_date : end_date}) #{start ? '' : '+ INTERVAL 1 DAY'}
|
189
190
|
AND ps.voided = 0 AND pp.patient_id IN (SELECT patient_id FROM cdr_temp_cohort_members PARTITION (p#{location}))
|
190
191
|
GROUP BY pp.patient_id
|
191
192
|
HAVING start_date IS NOT NULL
|
@@ -228,28 +229,28 @@ module MalawiHivProgramReports
|
|
228
229
|
def load_patients_who_died(start: false)
|
229
230
|
ActiveRecord::Base.connection.execute <<~SQL
|
230
231
|
INSERT INTO cdr_temp_patient_outcomes#{start ? '_start' : ''} PARTITION (p#{location})
|
231
|
-
SELECT patients.patient_id, 'Patient died',
|
232
|
-
FROM
|
233
|
-
|
234
|
-
ON patient_program.patient_id = patients.patient_id
|
235
|
-
AND patient_program.program_id = 1
|
236
|
-
AND patient_program.voided = 0
|
237
|
-
INNER JOIN patient_state PARTITION (p#{location})
|
238
|
-
ON patient_state.patient_program_id = patient_program.patient_program_id
|
239
|
-
AND patient_state.state = (#{program_states('Patient died').limit(1).to_sql})
|
240
|
-
AND patient_state.start_date < DATE(#{end_date}) + INTERVAL 1 DAY
|
241
|
-
AND patient_state.voided = 0
|
242
|
-
WHERE patients.date_enrolled <= DATE(#{end_date})
|
243
|
-
AND patient_state.date_created = (
|
244
|
-
SELECT MAX(date_created)
|
245
|
-
FROM patient_state PARTITION (p#{location}) ps
|
246
|
-
WHERE ps.patient_program_id = patient_state.patient_program_id
|
247
|
-
AND ps.state = patient_state.state AND ps.voided = 0 AND ps.start_date <= #{end_date})
|
232
|
+
SELECT patients.patient_id, 'Patient died', patients.outcome_date, #{location}, 1
|
233
|
+
FROM cdr_temp_current_state#{start ? '_start' : ''} PARTITION (p#{location}) AS patients
|
234
|
+
WHERE patients.outcomes = 1 AND patients.cum_outcome = 'Patient died'
|
248
235
|
GROUP BY patients.patient_id
|
249
236
|
ON DUPLICATE KEY UPDATE cum_outcome = VALUES(cum_outcome), outcome_date = VALUES(outcome_date), step = VALUES(step)
|
250
237
|
SQL
|
251
238
|
end
|
252
239
|
|
240
|
+
def load_other_patient_who_died(start: false)
|
241
|
+
ActiveRecord::Base.connection.execute <<~SQL
|
242
|
+
INSERT INTO cdr_temp_patient_outcomes#{start ? '_start' : ''} PARTITION (p#{location})
|
243
|
+
SELECT tesd.patient_id, 'Patient died', MAX(ps.start_date), #{location}, 1
|
244
|
+
FROM cdr_temp_cohort_members PARTITION (p#{location}) tesd
|
245
|
+
INNER JOIN patient_program PARTITION (p#{location}) pp ON pp.patient_id = tesd.patient_id AND pp.program_id = 1 AND pp.voided = 0
|
246
|
+
INNER JOIN patient_state PARTITION (p#{location}) ps ON ps.patient_program_id = pp.patient_program_id AND ps.state = 3 AND ps.voided = 0 AND ps.start_date <= #{end_date}
|
247
|
+
WHERE tesd.patient_id NOT IN (SELECT patient_id FROM cdr_temp_patient_outcomes#{start ? '_start' : ''} PARTITION (p#{location}) WHERE step = 1)
|
248
|
+
AND tesd.date_enrolled < DATE(#{start ? start_date : end_date}) #{start ? '' : '+ INTERVAL 1 DAY'}
|
249
|
+
GROUP BY tesd.patient_id
|
250
|
+
ON DUPLICATE KEY UPDATE cum_outcome = VALUES(cum_outcome), outcome_date = VALUES(outcome_date), step = VALUES(step)
|
251
|
+
SQL
|
252
|
+
end
|
253
|
+
|
253
254
|
# Loads all patients with an outcome of transferred out or
|
254
255
|
# treatment stopped into temp_patient_outcomes table.
|
255
256
|
def load_patients_who_stopped_treatment(start: false)
|
@@ -278,7 +279,7 @@ module MalawiHivProgramReports
|
|
278
279
|
INSERT INTO cdr_temp_patient_outcomes#{start ? '_start' : ''} PARTITION (p#{location})
|
279
280
|
SELECT patients.patient_id, 'Unknown', NULL, #{location}, 3
|
280
281
|
FROM cdr_temp_cohort_members PARTITION (p#{location}) AS patients
|
281
|
-
WHERE date_enrolled
|
282
|
+
WHERE date_enrolled < DATE(#{start ? start_date : end_date}) #{start ? '' : '+ INTERVAL 1 DAY'}
|
282
283
|
AND (patient_id) NOT IN (SELECT patient_id FROM cdr_temp_patient_outcomes#{start ? '_start' : ''} PARTITION (p#{location}) WHERE step IN (1, 2, 3, 4))
|
283
284
|
AND (patient_id) NOT IN (SELECT patient_id FROM cdr_temp_max_drug_orders#{start ? '_start' : ''} PARTITION (p#{location}))
|
284
285
|
ON DUPLICATE KEY UPDATE cum_outcome = VALUES(cum_outcome), outcome_date = VALUES(outcome_date), step = VALUES(step)
|
@@ -292,7 +293,7 @@ module MalawiHivProgramReports
|
|
292
293
|
SELECT patients.patient_id, 'On antiretrovirals', COALESCE(cs.outcome_date, patients.start_date), #{location}, 4
|
293
294
|
FROM cdr_temp_min_auto_expire_date#{start ? '_start' : ''} PARTITION (p#{location}) AS patients
|
294
295
|
LEFT JOIN cdr_temp_current_state#{start ? '_start' : ''} PARTITION (p#{location}) AS cs ON cs.patient_id = patients.patient_id AND cs.site_id = patients.site_id
|
295
|
-
WHERE patients.#{definition == 'pepfar' ? 'pepfar_defaulter_date' : 'moh_defaulter_date'} > #{end_date}
|
296
|
+
WHERE patients.#{definition == 'pepfar' ? 'pepfar_defaulter_date' : 'moh_defaulter_date'} > DATE(#{start ? start_date : end_date}) #{start ? '- INTERVAL 1 DAY' : ''}
|
296
297
|
AND (patients.patient_id) NOT IN (SELECT patient_id FROM cdr_temp_patient_outcomes#{start ? '_start' : ''} PARTITION (p#{location}) WHERE step IN (1, 2, 3))
|
297
298
|
ON DUPLICATE KEY UPDATE cum_outcome = VALUES(cum_outcome), outcome_date = VALUES(outcome_date), step = VALUES(step)
|
298
299
|
SQL
|
@@ -304,7 +305,7 @@ module MalawiHivProgramReports
|
|
304
305
|
SELECT patients.patient_id, 'Defaulted', patients.#{definition == 'pepfar' ? 'pepfar_defaulter_date' : 'moh_defaulter_date'},#{location}, 5
|
305
306
|
FROM cdr_temp_current_medication#{start ? '_start' : ''} PARTITION (p#{location}) AS patients
|
306
307
|
LEFT JOIN cdr_temp_current_state#{start ? '_start' : ''} PARTITION (p#{location}) AS cs ON cs.patient_id = patients.patient_id
|
307
|
-
WHERE patients.#{definition == 'pepfar' ? 'pepfar_defaulter_date' : 'moh_defaulter_date'} <= #{end_date}
|
308
|
+
WHERE patients.#{definition == 'pepfar' ? 'pepfar_defaulter_date' : 'moh_defaulter_date'} <= DATE(#{start ? start_date : end_date}) #{start ? '- INTERVAL 1 DAY' : ''}
|
308
309
|
AND (patients.patient_id) NOT IN (SELECT patient_id FROM cdr_temp_patient_outcomes#{start ? '_start' : ''} PARTITION (p#{location}) WHERE step IN (1, 2, 3, 4))
|
309
310
|
ON DUPLICATE KEY UPDATE cum_outcome = VALUES(cum_outcome), outcome_date = VALUES(outcome_date), step = VALUES(step)
|
310
311
|
SQL
|
@@ -314,7 +315,7 @@ module MalawiHivProgramReports
|
|
314
315
|
def load_defaulters(start: false)
|
315
316
|
ActiveRecord::Base.connection.execute <<~SQL
|
316
317
|
INSERT INTO cdr_temp_patient_outcomes#{start ? '_start' : ''} PARTITION (p#{location})
|
317
|
-
SELECT patient_id, #{patient_outcome_function('patient_id', 'site_id')}, NULL, #{location}, 6
|
318
|
+
SELECT patient_id, #{patient_outcome_function('patient_id', 'site_id', start)}, NULL, #{location}, 6
|
318
319
|
FROM cdr_temp_cohort_members PARTITION (p#{location})
|
319
320
|
WHERE date_enrolled <= #{end_date}
|
320
321
|
AND (patient_id, site_id) NOT IN (SELECT patient_id, site_id FROM cdr_temp_patient_outcomes#{start ? '_start' : ''} PARTITION (p#{location}) WHERE step IN (1, 2, 3, 4, 5))
|
@@ -325,10 +326,10 @@ module MalawiHivProgramReports
|
|
325
326
|
# ===================================
|
326
327
|
# Function Management Region
|
327
328
|
# ===================================
|
328
|
-
def patient_outcome_function(sql_column, site_id)
|
329
|
+
def patient_outcome_function(sql_column, site_id, start)
|
329
330
|
case definition
|
330
|
-
when 'moh' then "patient_outcome(#{sql_column}, #{end_date}, #{site_id})"
|
331
|
-
when 'pepfar' then "pepfar_patient_outcome(#{sql_column}, #{end_date}, #{site_id})"
|
331
|
+
when 'moh' then "patient_outcome(#{sql_column}, #{start ? "'#{start_date.to_date - 1.day}'" : end_date}, #{site_id})"
|
332
|
+
when 'pepfar' then "pepfar_patient_outcome(#{sql_column}, #{start ? "'#{start_date.to_date - 1.day}'" : end_date}, #{site_id})"
|
332
333
|
else raise "Invalid outcomes definition: #{definition}"
|
333
334
|
end
|
334
335
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: malawi_hiv_program_reports
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roy Chanunkha
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-06-
|
11
|
+
date: 2024-06-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|