malawi_hiv_program_reports 1.1.4 → 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: f074dc44fc448956bea3c452a3b95a930b01d71357826666e5e0820dfc5340df
4
- data.tar.gz: 675f722dbc8c93e3bfdd0cde1e160801f0bd55c9159703eadd49d8963adbd730
3
+ metadata.gz: df87fea0cc11ac0afae293f935d62c7f6c79a8e487e1ae0161d2e9e18b87e843
4
+ data.tar.gz: 9f1d344482d7f661d83f4140734f7339b81071e9984b20e25ed3af5e9c391bae
5
5
  SHA512:
6
- metadata.gz: '095bfc87622d4516d50dea06daac994c9528fa8a182e9d2afee41aecbbd05bc63c9dafbdfc44006028c7e9469456e9d5c9d4a09e6fd1eaf494679d28c7ac9f6e'
7
- data.tar.gz: 867e696edf6d2fd7318c3227ad82e262e0b1b275323b5f151f587e138cf1e80eec59fe0c612032ecfcb009d07bca5df1040a72d82aa3c6af9be7f9302bb83998
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MalawiHivProgramReports
4
- VERSION = '1.1.4'
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.4
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-21 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