malawi_hiv_program_reports 1.0.25 → 1.0.26

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: 062d15881512b1cc583e750ad84ad6d48c5ae101bfe115faa29661f92a5a4c5f
4
- data.tar.gz: e8b0ee4083537c6880f2ac0f362e647a02c670cf73f50af6db92b2b31cf1855a
3
+ metadata.gz: 53d6565fca96ee2021dfe480bdede7d60e308abc3988c209c86658e56d66baa4
4
+ data.tar.gz: c8f56f53589f21725e00c4afe769ffbc330f6a2a687455e6fd43bc128657eb8f
5
5
  SHA512:
6
- metadata.gz: 9e262010a9b6b969e1cba70790c82ff8b4da89f45d3b902307ce5a9f9a9364f51e9f32077d9a7c962b3505f33e7f93834b424d233c9d1e4ea5c128a0db6b7304
7
- data.tar.gz: 7663c5ba36fd8346e41aa41b432951fc286da066c89a63f7966cd28be6a843247d66ed1869e31414793673dd2db3cba1c972d17666924833fa8de40916901c41
6
+ metadata.gz: b9909ffb6013589e67b6ccce8f7833308270cedb69c74d7ac77a203b2870475e69bbef0c635d88ad51d1095958670313b26c862dc16407358aa448a3eaf1378e
7
+ data.tar.gz: a08774fa4213673af1cfa0efa728059a52bbe6dcc0f0dd70174ebaddc68581ba25c50d53a56946c474ed99bd60234762b67c8e57b77f0d94eea8a2477790f27a
@@ -17,13 +17,17 @@ module MalawiHivProgramReports
17
17
  @rebuild = kwargs[:rebuild]&.casecmp?('true')
18
18
  locations = kwargs[:locations]
19
19
  @locations = locations.present? ? locations.split(',') : []
20
- @definition = kwargs[:definition]
20
+ @definition = kwargs[:definition] || 'pepfar'
21
+
22
+ definition = @definition.downcase
23
+ raise ArgumentError, "Invalid outcomes definition: #{definition}" unless %w[moh pepfar].include?(definition)
21
24
  end
22
25
 
23
26
  def find_report
24
27
  start_time = Time.now
25
28
  prepare_tables
26
29
  clear_tables if rebuild
30
+ clear_cohort_status unless rebuild
27
31
  process_thread
28
32
  end_time = Time.now
29
33
  time_in_minutes = ((end_time - start_time) / 60).round(2)
@@ -60,7 +64,8 @@ module MalawiHivProgramReports
60
64
  ActiveRecord::Base.connection_pool.with_connection do
61
65
  process_data loc
62
66
  rescue StandardError => e
63
- Rails.logger.error("Error processing location #{loc}: #{e.message}")
67
+ Rails.logger.info("Error processing location #{loc}: #{e.message}")
68
+ Rails.logger.info(e.backtrace.join("\n"))
64
69
  save_incomplete_site(location: loc, time_taken: 0)
65
70
  end
66
71
  end
@@ -80,8 +85,8 @@ module MalawiHivProgramReports
80
85
  potential_cohort_members location
81
86
  reason_for_starting_art location
82
87
  cohort_members location
83
- outcome = MalawiHivProgramReports::Moh::CumulativeOutcome.new(end_date:, location:, definition:, rebuild:)
84
- outcome.find_report
88
+ outcome = MalawiHivProgramReports::Moh::CumulativeOutcome.new(end_date:, location:, definition:, rebuild:, start_date:)
89
+ rebuild ? outcome.find_report : outcome.update_outcomes_by_definition
85
90
  end_time = Time.now
86
91
  time_taken = ((end_time - start_time) / 60).round(2)
87
92
  save_completed_site(location:, time_taken:)
@@ -260,7 +265,7 @@ module MalawiHivProgramReports
260
265
  INSERT INTO cdr_temp_cohort_members PARTITION (p#{location})
261
266
  SELECT
262
267
  pcm.patient_id,
263
- pcm.site_id,
268
+ #{location},
264
269
  pcm.birthdate,
265
270
  pcm.birthdate_estimated,
266
271
  pcm.death_date,
@@ -271,11 +276,10 @@ module MalawiHivProgramReports
271
276
  IF(pcm.birthdate IS NOT NULL, TIMESTAMPDIFF(YEAR, pcm.birthdate, COALESCE(ti.value_datetime, mdo.start_date)), NULL) AS age_at_initiation,
272
277
  IF(pcm.birthdate IS NOT NULL, TIMESTAMPDIFF(DAY, pcm.birthdate, COALESCE(ti.value_datetime, mdo.start_date)), NULL) AS age_in_days,
273
278
  rfsa.reason_for_starting_art
274
- FROM cdr_temp_potential_cohort_members pcm
275
- INNER JOIN cdr_temp_min_drug_orders mdo ON mdo.patient_id = pcm.patient_id AND mdo.site_id = pcm.site_id
276
- INNER JOIN cdr_reason_for_starting_art rfsa ON rfsa.patient_id = pcm.patient_id AND rfsa.site_id = pcm.site_id
277
- LEFT JOIN cdr_temp_transfer_ins ti ON ti.patient_id = pcm.patient_id AND ti.site_id = pcm.site_id
278
- WHERE pcm.site_id = #{location}
279
+ FROM cdr_temp_potential_cohort_members PARTITION (p#{location}) pcm
280
+ INNER JOIN cdr_temp_min_drug_orders PARTITION (p#{location}) mdo ON mdo.patient_id = pcm.patient_id
281
+ INNER JOIN cdr_reason_for_starting_art PARTITION (p#{location}) AS rfsa ON rfsa.patient_id = pcm.patient_id
282
+ LEFT JOIN cdr_temp_transfer_ins PARTITION (p#{location}) ti ON ti.patient_id = pcm.patient_id
279
283
  HAVING reason_for_starting_art IS NOT NULL
280
284
  ON DUPLICATE KEY UPDATE birthdate = VALUES(birthdate), birthdate_estimated = VALUES(birthdate_estimated), death_date = VALUES(death_date),
281
285
  gender = VALUES(gender), date_enrolled = VALUES(date_enrolled), earliest_start_date = VALUES(earliest_start_date),
@@ -289,19 +293,18 @@ module MalawiHivProgramReports
289
293
  INSERT INTO cdr_temp_potential_cohort_members PARTITION (p#{location})
290
294
  SELECT
291
295
  pp.patient_id,
292
- pp.site_id,
296
+ #{location},
293
297
  p.birthdate,
294
298
  p.birthdate_estimated,
295
299
  p.death_date,
296
300
  LEFT(p.gender, 1) gender
297
- FROM patient_program pp
298
- INNER JOIN person p ON p.person_id = pp.patient_id AND p.site_id = pp.site_id AND p.voided = 0
299
- INNER JOIN patient_state ps ON ps.patient_program_id = pp.patient_program_id AND ps.site_id = pp.site_id
301
+ FROM patient_program PARTITION (p#{location}) AS pp
302
+ INNER JOIN person PARTITION (p#{location}) AS p ON p.person_id = pp.patient_id AND p.voided = 0
303
+ INNER JOIN patient_state PARTITION (p#{location}) AS ps ON ps.patient_program_id = pp.patient_program_id
300
304
  AND ps.voided = 0 AND ps.state = 7 AND ps.start_date IS NOT NULL -- 7 is On antiretrovirals
301
305
  AND ps.start_date < (DATE(#{end_date}) + INTERVAL 1 DAY)
302
- WHERE pp.program_id = 1 AND pp.voided = 0 AND (pp.patient_id, pp.site_id) NOT IN (SELECT patient_id, site_id FROM cdr_temp_external_clients)
303
- AND pp.site_id = #{location}
304
- GROUP BY pp.patient_id, pp.site_id
306
+ WHERE pp.program_id = 1 AND pp.voided = 0 AND (pp.patient_id) NOT IN (SELECT patient_id FROM cdr_temp_external_clients PARTITION (p#{location}))
307
+ GROUP BY pp.patient_id
305
308
  ON DUPLICATE KEY UPDATE birthdate = VALUES(birthdate), birthdate_estimated = VALUES(birthdate_estimated), death_date = VALUES(death_date),
306
309
  gender = VALUES(gender)
307
310
  SQL
@@ -310,24 +313,24 @@ module MalawiHivProgramReports
310
313
  def min_drug_orders(location)
311
314
  ActiveRecord::Base.connection.execute <<~SQL
312
315
  INSERT INTO cdr_temp_min_drug_orders PARTITION (p#{location})
313
- SELECT o.patient_id, o.site_id, DATE(MIN(o.start_date)) start_date
314
- FROM orders o
315
- INNER JOIN drug_order do ON do.order_id = o.order_id AND do.site_id = o.site_id AND do.quantity > 0
316
+ SELECT o.patient_id, #{location}, DATE(MIN(o.start_date)) start_date
317
+ FROM orders PARTITION (p#{location}) AS o
318
+ INNER JOIN drug_order PARTITION (p#{location}) AS do ON do.order_id = o.order_id AND do.quantity > 0
316
319
  LEFT JOIN (
317
- SELECT o.person_id patient_id, o.site_id, DATE(MIN(o.obs_datetime)) registered_date
318
- FROM obs o
319
- INNER JOIN cdr_other_patient_types other ON other.patient_id = o.person_id AND other.site_id = o.site_id
320
+ SELECT o.person_id patient_id, #{location}, DATE(MIN(o.obs_datetime)) registered_date
321
+ FROM obs PARTITION (p#{location}) AS o
322
+ INNER JOIN cdr_other_patient_types PARTITION (p#{location}) AS other ON other.patient_id = o.person_id
320
323
  WHERE o.concept_id = 3289 -- Type of patient
321
324
  AND o.value_coded = 7572 -- New patient
322
325
  AND o.voided = 0
323
326
  AND o.obs_datetime < (DATE(#{end_date}) + INTERVAL 1 DAY)
324
- GROUP BY o.person_id, o.site_id
325
- ) np ON np.patient_id = o.patient_id AND np.site_id = o.site_id
327
+ GROUP BY o.person_id
328
+ ) np ON np.patient_id = o.patient_id
326
329
  WHERE o.voided = 0 AND o.start_date < (DATE(#{end_date}) + INTERVAL 1 DAY)
327
330
  AND o.start_date > COALESCE(np.registered_date, DATE('1900-01-01'))
328
331
  AND o.concept_id IN (SELECT concept_id FROM concept_set WHERE concept_set = 1085) -- 1085 is ARV DRUGS
329
- AND o.order_type_id = 1 AND o.site_id = #{location}
330
- GROUP BY o.patient_id, o.site_id
332
+ AND o.order_type_id = 1
333
+ GROUP BY o.patient_id
331
334
  ON DUPLICATE KEY UPDATE start_date = VALUES(start_date)
332
335
  SQL
333
336
  end
@@ -335,15 +338,15 @@ module MalawiHivProgramReports
335
338
  def transfer_ins(location)
336
339
  ActiveRecord::Base.connection.execute <<~SQL
337
340
  INSERT INTO cdr_temp_transfer_ins PARTITION (p#{location})
338
- SELECT o.person_id, o.site_id, DATE(MIN(o.value_datetime)) value_datetime
339
- FROM obs o
340
- INNER JOIN encounter e ON e.patient_id = o.person_id AND e.site_id = o.site_id AND e.encounter_id = o.encounter_id
341
- AND e.program_id = 1 AND e.encounter_datetime < (DATE(#{end_date}) + INTERVAL 1 DAY)
341
+ SELECT o.person_id, #{location}, DATE(MIN(o.value_datetime)) value_datetime
342
+ FROM obs PARTITION (p#{location}) AS o
343
+ INNER JOIN encounter PARTITION (p#{location}) AS e ON e.patient_id = o.person_id AND e.encounter_id = o.encounter_id
344
+ AND e.program_id = 1 -- HIV Program
345
+ AND e.encounter_datetime < (DATE(#{end_date}) + INTERVAL 1 DAY)
342
346
  AND e.encounter_type = 9 -- HIV CLINIC REGISTRATION
343
347
  AND e.voided = 0
344
348
  WHERE o.concept_id = 2516 AND o.voided = 0 AND o.obs_datetime < (DATE(#{end_date}) + INTERVAL 1 DAY) -- 2516 is Date antiretrovirals started
345
- AND o.site_id = #{location}
346
- GROUP BY o.person_id, o.site_id
349
+ GROUP BY o.person_id
347
350
  ON DUPLICATE KEY UPDATE value_datetime = VALUES(value_datetime)
348
351
  SQL
349
352
  end
@@ -351,24 +354,21 @@ module MalawiHivProgramReports
351
354
  def external_clients(location)
352
355
  ActiveRecord::Base.connection.execute <<~SQL
353
356
  INSERT INTO cdr_temp_external_clients PARTITION (p#{location})
354
- SELECT e.patient_id, e.site_id, GROUP_CONCAT(DISTINCT(patient_type.value_coded)) AS patient_types, clinic_registration.encounter_id
355
- FROM patient_program as e
356
- INNER JOIN obs AS patient_type ON patient_type.person_id = e.patient_id
357
- AND patient_type.site_id = e.site_id
357
+ SELECT e.patient_id, #{location}, GROUP_CONCAT(DISTINCT(patient_type.value_coded)) AS patient_types, clinic_registration.encounter_id
358
+ FROM patient_program PARTITION (p#{location}) AS e
359
+ INNER JOIN obs PARTITION (p#{location}) AS patient_type ON patient_type.person_id = e.patient_id
358
360
  AND patient_type.voided = 0
359
361
  AND patient_type.concept_id = 3289 -- Type of patient
360
362
  AND patient_type.obs_datetime < DATE(#{end_date}) + INTERVAL 1 DAY
361
- LEFT JOIN encounter as clinic_registration ON clinic_registration.patient_id = e.patient_id
362
- AND clinic_registration.site_id = e.site_id
363
- AND clinic_registration.program_id = 1
363
+ LEFT JOIN encounter PARTITION (p#{location}) AS clinic_registration ON clinic_registration.patient_id = e.patient_id
364
+ AND clinic_registration.program_id = 1 -- HIV PROGRAM
364
365
  AND clinic_registration.encounter_type = 9 -- HIV CLINIC REGISTRATION
365
366
  AND clinic_registration.encounter_datetime < DATE(#{end_date}) + INTERVAL 1 DAY
366
367
  AND clinic_registration.voided = 0
367
368
  WHERE e.program_id = 1 -- HIV program
368
369
  AND e.voided = 0
369
- AND e.site_id = #{location}
370
370
  -- AND clinic_registration.encounter_id IS NOT NULL -- bone of contention
371
- GROUP BY e.patient_id, e.site_id
371
+ GROUP BY e.patient_id
372
372
  HAVING FIND_IN_SET('7572', patient_types) = 0 AND encounter_id IS NULL
373
373
  ON DUPLICATE KEY UPDATE patient_types = VALUES(patient_types)
374
374
  SQL
@@ -377,14 +377,13 @@ module MalawiHivProgramReports
377
377
  def cdr_other_patient_types(location)
378
378
  ActiveRecord::Base.connection.execute <<~SQL
379
379
  INSERT INTO cdr_other_patient_types PARTITION (p#{location})
380
- SELECT o.person_id, o.site_id
381
- FROM obs o
380
+ SELECT o.person_id, #{location}
381
+ FROM obs PARTITION (p#{location}) AS o
382
382
  WHERE o.concept_id = 3289 -- Type of patient
383
383
  AND o.value_coded != 7572 -- New patient
384
384
  AND o.voided = 0
385
385
  AND o.obs_datetime < (DATE(#{end_date}) + INTERVAL 1 DAY)
386
- AND o.site_id = #{location}
387
- GROUP BY o.person_id, o.site_id
386
+ GROUP BY o.person_id
388
387
  -- on duplicate just ignore
389
388
  ON DUPLICATE KEY UPDATE site_id = VALUES(site_id)
390
389
  SQL
@@ -393,14 +392,15 @@ module MalawiHivProgramReports
393
392
  def reason_for_starting_art(location)
394
393
  ActiveRecord::Base.connection.execute <<~SQL
395
394
  INSERT INTO cdr_reason_for_starting_art PARTITION (p#{location})
396
- SELECT a.person_id, a.site_id, a.value_coded
397
- FROM obs a
398
- INNER JOIN cdr_temp_potential_cohort_members ct ON ct.patient_id = a.person_id AND ct.site_id = a.site_id
399
- LEFT OUTER JOIN obs b ON a.person_id = b.person_id AND a.site_id = b.site_id AND b.concept_id = a.concept_id
400
- AND b.concept_id = 7563
401
- AND a.obs_datetime < b.obs_datetime AND b.voided = 0 AND b.obs_datetime < DATE(#{end_date}) + INTERVAL 1 DAY AND a.obs_datetime < DATE(#{end_date}) + INTERVAL 1 DAY
402
- WHERE b.obs_id IS NULL AND a.concept_id = 7563 AND a.voided = 0 AND a.site_id = #{location}
403
- GROUP BY a.person_id, a.site_id
395
+ SELECT a.person_id, #{location}, a.value_coded
396
+ FROM obs PARTITION (p#{location}) a
397
+ INNER JOIN cdr_temp_potential_cohort_members PARTITION (p#{location}) AS ct ON ct.patient_id = a.person_id
398
+ LEFT OUTER JOIN obs PARTITION (p#{location}) AS b ON a.person_id = b.person_id AND b.concept_id = a.concept_id
399
+ AND b.concept_id = 7563
400
+ AND a.obs_datetime < b.obs_datetime AND b.voided = 0
401
+ AND b.obs_datetime < DATE(#{end_date}) + INTERVAL 1 DAY AND a.obs_datetime < DATE(#{end_date}) + INTERVAL 1 DAY
402
+ WHERE b.obs_id IS NULL AND a.concept_id = 7563 AND a.voided = 0
403
+ GROUP BY a.person_id
404
404
  ON DUPLICATE KEY UPDATE reason_for_starting_art = VALUES(reason_for_starting_art)
405
405
  SQL
406
406
  end
@@ -417,7 +417,6 @@ module MalawiHivProgramReports
417
417
  ActiveRecord::Base.connection.execute('TRUNCATE TABLE cdr_temp_cohort_members')
418
418
  ActiveRecord::Base.connection.execute('TRUNCATE TABLE cdr_temp_external_clients')
419
419
  ActiveRecord::Base.connection.execute('TRUNCATE TABLE cdr_reason_for_starting_art')
420
- ActiveRecord::Base.connection.execute('TRUNCATE TABLE cdr_temp_cohort_status')
421
420
  else
422
421
  ActiveRecord::Base.connection.execute("DELETE FROM cdr_other_patient_types WHERE site_id IN (#{locations.join(',')})")
423
422
  ActiveRecord::Base.connection.execute("DELETE FROM cdr_temp_potential_cohort_members WHERE site_id IN (#{locations.join(',')})")
@@ -426,6 +425,14 @@ module MalawiHivProgramReports
426
425
  ActiveRecord::Base.connection.execute("DELETE FROM cdr_temp_cohort_members WHERE site_id IN (#{locations.join(',')})")
427
426
  ActiveRecord::Base.connection.execute("DELETE FROM cdr_temp_external_clients WHERE site_id IN (#{locations.join(',')})")
428
427
  ActiveRecord::Base.connection.execute("DELETE FROM cdr_reason_for_starting_art WHERE site_id IN (#{locations.join(',')})")
428
+ end
429
+ clear_cohort_status
430
+ end
431
+
432
+ def clear_cohort_status
433
+ if locations.empty?
434
+ ActiveRecord::Base.connection.execute('TRUNCATE TABLE cdr_temp_cohort_status')
435
+ else
429
436
  ActiveRecord::Base.connection.execute("DELETE FROM cdr_temp_cohort_status WHERE site_id IN (#{locations.join(',')})")
430
437
  end
431
438
  end