malawi_hiv_program_reports 1.1.3 → 1.1.5

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: ed231eff9bb9c45a7d57027366c82fc112fc1de0cb6c4f457cd87bfeed1bd908
4
- data.tar.gz: 3b9a9292285146cac5a4f202bf2d2266fdf323f1acdb67a94706efee322f4afe
3
+ metadata.gz: df87fea0cc11ac0afae293f935d62c7f6c79a8e487e1ae0161d2e9e18b87e843
4
+ data.tar.gz: 9f1d344482d7f661d83f4140734f7339b81071e9984b20e25ed3af5e9c391bae
5
5
  SHA512:
6
- metadata.gz: 2dab0b971e5a76b0097586837b7adb5869d5c0d34563824cc79d02b8cce0679f22a1afce0955b3a733663e017a6886ccced14f924dd5636f34026c05fc6649b4
7
- data.tar.gz: 7015b44578a5bdfaea8a15c2b85b03823688b5a13fdf37e683612549c257376c0583117a28b03332d4af53c2561069c1b5d406862fd341ba15e0c724b0bbdfe7
6
+ metadata.gz: bead67daba89758fba7d271bfc729b0455e85a88edc1ae382ed17436bd2e67b6088df8b3d8ce181cbdb6457331c4c4fe3caef0b9fefd102bb66300ee9335c32d
7
+ data.tar.gz: d615b55bb62b59ed7eb3bd14d20ac8fb7ace2e04ec3dcbc8df2badcc3dce8ac370a96123471e6ac0403505810231914abb1ac3ee806183720be6e5c5fbceb29a
@@ -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', patient_state.start_date, #{location}, 1
232
- FROM cdr_temp_cohort_members PARTITION (p#{location}) AS patients
233
- INNER JOIN patient_program PARTITION (p#{location})
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 <= #{end_date}
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
@@ -124,14 +124,15 @@ module MalawiHivProgramReports
124
124
  ActiveRecord::Base.connection.select_all <<~SQL
125
125
  SELECT o.person_id, o.value_coded
126
126
  FROM obs #{current_partition} o
127
- LEFT JOIN obs #{current_partition} a ON a.obs_id = o.obs_id and a.obs_datetime > o.obs_datetime and a.concept_id in (#{pregnant_concepts.to_sql}) and a.voided = 0
127
+ LEFT JOIN obs #{current_partition} a ON a.person_id = o.person_id AND a.obs_datetime > o.obs_datetime AND a.concept_id IN (#{pregnant_concepts.to_sql}) AND a.voided = 0
128
128
  AND a.obs_datetime >= DATE(#{ActiveRecord::Base.connection.quote(start_date)}) AND a.obs_datetime < DATE(#{ActiveRecord::Base.connection.quote(end_date)}) + INTERVAL 1 DAY
129
129
  WHERE a.obs_id is null
130
- and o.obs_datetime < DATE(#{ActiveRecord::Base.connection.quote(end_date)}) + INTERVAL 1 DAY
130
+ AND o.obs_datetime >= DATE(#{ActiveRecord::Base.connection.quote(start_date)})
131
+ AND o.obs_datetime < DATE(#{ActiveRecord::Base.connection.quote(end_date)}) + INTERVAL 1 DAY
131
132
  AND o.voided = 0
132
133
  AND o.concept_id in (#{pregnant_concepts.to_sql})
133
- AND o.value_coded IN (#{yes_concepts.join(',')})
134
134
  AND o.person_id IN (#{patient_list.join(',')})
135
+ AND o.value_coded IN (#{yes_concepts.join(',')})
135
136
  GROUP BY o.person_id
136
137
  SQL
137
138
  end
@@ -140,14 +141,15 @@ module MalawiHivProgramReports
140
141
  ActiveRecord::Base.connection.select_all <<~SQL
141
142
  SELECT o.person_id, o.value_coded
142
143
  FROM obs #{current_partition} o
143
- LEFT JOIN obs #{current_partition} a ON a.obs_id = o.obs_id and a.obs_datetime > o.obs_datetime and a.concept_id in (#{breast_feeding_concepts.to_sql}) and a.voided = 0
144
+ LEFT JOIN obs #{current_partition} a ON a.person_id = o.person_id AND a.obs_datetime > o.obs_datetime AND a.concept_id IN (#{breast_feeding_concepts.to_sql}) AND a.voided = 0
144
145
  AND a.obs_datetime >= DATE(#{ActiveRecord::Base.connection.quote(start_date)}) AND a.obs_datetime < DATE(#{ActiveRecord::Base.connection.quote(end_date)}) + INTERVAL 1 DAY
145
146
  WHERE a.obs_id is null
146
- and o.obs_datetime < DATE(#{ActiveRecord::Base.connection.quote(end_date)}) + INTERVAL 1 DAY
147
+ AND o.obs_datetime >= DATE(#{ActiveRecord::Base.connection.quote(start_date)})
148
+ AND o.obs_datetime < DATE(#{ActiveRecord::Base.connection.quote(end_date)}) + INTERVAL 1 DAY
147
149
  AND o.voided = 0
148
150
  AND o.concept_id IN (#{breast_feeding_concepts.to_sql})
149
- AND o.value_coded IN (#{yes_concepts.join(',')})
150
151
  AND o.person_id IN (#{patient_list.join(',')})
152
+ AND o.value_coded IN (#{yes_concepts.join(',')})
151
153
  GROUP BY o.person_id
152
154
  SQL
153
155
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MalawiHivProgramReports
4
- VERSION = '1.1.3'
4
+ VERSION = '1.1.5'
5
5
  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.3
4
+ version: 1.1.5
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-20 00:00:00.000000000 Z
11
+ date: 2024-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails