malawi_hiv_program_reports 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9c6bffbef9d85fec441ab368a631e8e730446a87ed8722d2a60d348c68ecf969
4
- data.tar.gz: dfd3ba0bfe3a2fc65ca948d3dd2bee21c9f8bb4f7c42ade37623f46c6fb8aefb
3
+ metadata.gz: af23c66f80f04922477b707fe4978ca1234e06814761e657a4e9a27c8bb433e3
4
+ data.tar.gz: 762826dfec0aafbf978fc024ab7dafca271d3d6588128c9d4a9180a0ffcf86ab
5
5
  SHA512:
6
- metadata.gz: 9a0217e328962a1734f08e22d28df28624f0a6eff6ca02e1eb32f021e0397758ab58bca452ddada4ccf662cac36b862952c880b98e3c08e500e4160db51af968
7
- data.tar.gz: c6f54d2927268c90cf1e5b87ea621eb68aa5cea3d6f17095f382f7c7f91bdd0f4360a9ab4e00f8be536414a498c2dd1b3d01d8a7f88e01425dbb0e0292309a1c
6
+ metadata.gz: dad18495c08f5e2ca3db34adad758449cdf147dbd8c9edabbb8b771f68e0dda3d4ae88d47082a24fa5ef62f52e74f603676f8bef15d896ae93947ba529421132
7
+ data.tar.gz: e3497d0e34c8397efec79d71d6e77e805b3d580e1b8096856a04a2e97d6fe7da73612bf593a58c680b13b93c73778f014d61ea3edfb5ba7fe690e31f62fbc778
@@ -25,14 +25,15 @@ module MalawiHivProgramReports
25
25
 
26
26
  def find_report
27
27
  start_time = Time.now
28
- prepare_tables
29
- clear_tables if rebuild
28
+ handle_tables
30
29
  clear_cohort_status unless rebuild
31
- process_thread
30
+ process_thread(locations:)
32
31
  end_time = Time.now
33
32
  time_in_minutes = ((end_time - start_time) / 60).round(2)
33
+ handle_failed
34
+ time_taken_for_failed = ((Time.now - end_time) / 60).round(2)
34
35
  Rails.logger.info("Cumulative Cohort report took #{time_in_minutes} minutes to generate for these locations: #{locations}")
35
- { cohort_time: time_in_minutes }
36
+ { cohort_time: time_in_minutes, processing_failed: time_taken_for_failed }
36
37
  end
37
38
 
38
39
  private
@@ -46,7 +47,7 @@ module MalawiHivProgramReports
46
47
  # 6. Finally we want to get the outcomes of the clients in 5 above
47
48
 
48
49
  # rubocop:disable Metrics/MethodLength
49
- def process_thread
50
+ def process_thread(locations: [])
50
51
  # use locations to thread but process 10 locations at a time
51
52
  queue = Queue.new
52
53
  locations.each { |loc| queue << loc }
@@ -76,6 +77,51 @@ module MalawiHivProgramReports
76
77
  end
77
78
  # rubocop:enable Metrics/MethodLength
78
79
 
80
+ def reprocess_failed
81
+ failed_locs = failed_locs&.map { |loc| loc[:site_id] }
82
+ return if failed_locs.empty?
83
+
84
+ process_thread(locations: failed_locs)
85
+ end
86
+
87
+ def handle_failed
88
+ failed_locs = failed_sites&.map { |loc| loc['site_id'].to_i }
89
+ return if failed_locs.empty?
90
+
91
+ 4.times do
92
+ reprocess_failed
93
+ end
94
+
95
+ failed_locs = failed_sites&.map { |loc| loc['site_id'].to_i }
96
+ return if failed_locs.empty?
97
+
98
+ failed_locs.each do |loc|
99
+ ActiveRecord::Base.connection_pool.with_connection do
100
+ process_data loc
101
+ rescue StandardError => e
102
+ Rails.logger.info("Error processing location #{loc}: #{e.message}")
103
+ Rails.logger.info(e.backtrace.join("\n"))
104
+ save_incomplete_site(location: loc, time_taken: 0)
105
+ end
106
+ end
107
+ end
108
+
109
+ def failed_sites
110
+ ActiveRecord::Base.connection.select_all <<~SQL
111
+ SELECT site_id
112
+ FROM cdr_temp_cohort_status
113
+ WHERE status = 'incomplete' AND site_id IN (#{locations.join(',')})
114
+ SQL
115
+ end
116
+
117
+ def handle_tables
118
+ prepare_tables
119
+ clear_tables if rebuild
120
+ outcome = MalawiHivProgramReports::Moh::CumulativeOutcome.new(end_date:, location: 0, definition:, rebuild:,
121
+ start_date:)
122
+ outcome.handle_tables
123
+ end
124
+
79
125
  def process_data(location)
80
126
  start_time = Time.now
81
127
  if rebuild
@@ -4,7 +4,6 @@ module MalawiHivProgramReports
4
4
  module Moh
5
5
  # This is the Cumulative Cohort Builder class
6
6
  # rubocop:disable Metrics/ClassLength
7
- # rubocop:disable Metrics/MethodLength
8
7
  class CumulativeOutcome
9
8
  include MalawiHivProgramReports::Utils::CommonSqlQueryUtils
10
9
  attr_reader :end_date, :definition, :rebuild, :start_date, :prev_date, :location
@@ -17,11 +16,9 @@ module MalawiHivProgramReports
17
16
  @definition = definition
18
17
  @rebuild = kwargs[:rebuild]
19
18
  @location = kwargs[:location]
20
- Rails.logger.info "Definition: #{definition}. Rebuild: #{@rebuild}. Location: #{@location}. Start date: #{@start_date}. End date: #{@end_date}. Previous date: #{@prev_date}."
21
19
  end
22
20
 
23
21
  def find_report
24
- prepare_tables
25
22
  [false, true].each do |start|
26
23
  clear_tables(start:) if rebuild
27
24
  update_steps(start:, portion: false) unless rebuild
@@ -30,15 +27,18 @@ module MalawiHivProgramReports
30
27
  end
31
28
 
32
29
  def update_outcomes_by_definition
33
- prepare_tables
34
30
  [false, true].each do |start|
35
- update_steps(start: start, portion: true)
31
+ update_steps(start:, portion: true)
36
32
  load_patients_on_treatment(start:)
37
33
  load_without_clinical_contact(start:)
38
34
  load_defaulters(start:)
39
35
  end
40
36
  end
41
37
 
38
+ def handle_tables
39
+ prepare_tables
40
+ end
41
+
42
42
  private
43
43
 
44
44
  # The main idea here is to come up with cumulative outcomes for patients in temp_earliest_start_date
@@ -61,7 +61,6 @@ module MalawiHivProgramReports
61
61
  # ===================================
62
62
  # Data Management Region
63
63
  # ===================================
64
- # rubocop:disable Metrics/MethodLength
65
64
  def process_data(start: false)
66
65
  denormalize(start:)
67
66
  # HIC SUNT DRACONIS: The order of the operations below matters,
@@ -95,7 +94,7 @@ module MalawiHivProgramReports
95
94
  AND drug_order.site_id = o.site_id AND drug_order.quantity > 0
96
95
  AND drug_order.drug_inventory_id IN (#{arv_drug})
97
96
  WHERE o.order_type_id = 1 -- drug order
98
- AND o.start_date < (DATE(#{start ? start_date : end_date }) + INTERVAL 1 DAY)
97
+ AND o.start_date < (DATE(#{start ? start_date : end_date}) + INTERVAL 1 DAY)
99
98
  AND o.voided = 0
100
99
  GROUP BY o.patient_id
101
100
  ON DUPLICATE KEY UPDATE start_date = VALUES(start_date), min_order_date = VALUES(min_order_date)
@@ -111,8 +110,8 @@ module MalawiHivProgramReports
111
110
  INNER JOIN drug_order PARTITION (p#{location}) ON drug_order.order_id = o.order_id AND drug_order.quantity > 0
112
111
  AND drug_order.drug_inventory_id IN (#{arv_drug})
113
112
  WHERE o.order_type_id = 1 -- drug order
114
- AND o.start_date < (DATE(#{start ? start_date : end_date }) + INTERVAL 1 DAY)
115
- AND o.start_date >= (DATE(#{start ? prev_date : start_date }) + INTERVAL 1 DAY)
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)
116
115
  AND o.voided = 0
117
116
  GROUP BY o.patient_id
118
117
  ON DUPLICATE KEY UPDATE start_date = VALUES(start_date), min_order_date = VALUES(min_order_date)
@@ -323,8 +322,6 @@ module MalawiHivProgramReports
323
322
  SQL
324
323
  end
325
324
 
326
- # rubocop:enable Metrics/MethodLength
327
-
328
325
  # ===================================
329
326
  # Function Management Region
330
327
  # ===================================
@@ -342,15 +339,33 @@ module MalawiHivProgramReports
342
339
  def prepare_tables
343
340
  [false, true].each do |start|
344
341
  create_outcome_table(start:) unless check_if_table_exists("cdr_temp_patient_outcomes#{start ? '_start' : ''}")
345
- drop_temp_patient_outcome_table(start:) unless count_table_columns("cdr_temp_patient_outcomes#{start ? '_start' : ''}") == 5
346
- create_tmp_max_drug_orders_table(start:) unless check_if_table_exists("cdr_temp_max_drug_orders#{start ? '_start' : ''}")
347
- drop_temp_max_drug_orders_table(start:) unless count_table_columns("cdr_temp_max_drug_orders#{start ? '_start' : ''}") == 4
348
- create_tmp_min_auto_expire_date(start:) unless check_if_table_exists("cdr_temp_min_auto_expire_date#{start ? '_start' : ''}")
349
- drop_tmp_min_auto_expirte_date(start:) unless count_table_columns("cdr_temp_min_auto_expire_date#{start ? '_start' : ''}") == 6
350
- create_cdr_temp_max_patient_state(start:) unless check_if_table_exists("cdr_temp_max_patient_state#{start ? '_start' : ''}")
351
- create_temp_current_state(start:) unless check_if_table_exists("cdr_temp_current_state#{start ? '_start' : ''}")
352
- drop_temp_current_state(start:) unless count_table_columns("cdr_temp_current_state#{start ? '_start' : ''}") == 7
353
- create_temp_current_medication(start:) unless check_if_table_exists("cdr_temp_current_medication#{start ? '_start' : ''}")
342
+ unless count_table_columns("cdr_temp_patient_outcomes#{start ? '_start' : ''}") == 5
343
+ drop_temp_patient_outcome_table(start:)
344
+ end
345
+ unless check_if_table_exists("cdr_temp_max_drug_orders#{start ? '_start' : ''}")
346
+ create_tmp_max_drug_orders_table(start:)
347
+ end
348
+ unless count_table_columns("cdr_temp_max_drug_orders#{start ? '_start' : ''}") == 4
349
+ drop_temp_max_drug_orders_table(start:)
350
+ end
351
+ unless check_if_table_exists("cdr_temp_min_auto_expire_date#{start ? '_start' : ''}")
352
+ create_tmp_min_auto_expire_date(start:)
353
+ end
354
+ unless count_table_columns("cdr_temp_min_auto_expire_date#{start ? '_start' : ''}") == 6
355
+ drop_tmp_min_auto_expirte_date(start:)
356
+ end
357
+ unless check_if_table_exists("cdr_temp_max_patient_state#{start ? '_start' : ''}")
358
+ create_cdr_temp_max_patient_state(start:)
359
+ end
360
+ unless check_if_table_exists("cdr_temp_current_state#{start ? '_start' : ''}")
361
+ create_temp_current_state(start:)
362
+ end
363
+ unless count_table_columns("cdr_temp_current_state#{start ? '_start' : ''}") == 7
364
+ drop_temp_current_state(start:)
365
+ end
366
+ unless check_if_table_exists("cdr_temp_current_medication#{start ? '_start' : ''}")
367
+ create_temp_current_medication(start:)
368
+ end
354
369
  end
355
370
  end
356
371
 
@@ -390,7 +405,7 @@ module MalawiHivProgramReports
390
405
  end
391
406
 
392
407
  def drop_temp_patient_outcome_table(start: false)
393
- ActiveRecord::Base.connection.execute <<~SQL
408
+ ActiveRecord::Base.connection.execute <<~SQL
394
409
  DROP TABLE IF EXISTS cdr_temp_patient_outcomes#{start ? '_start' : ''}
395
410
  SQL
396
411
  create_outcome_table(start:)
@@ -451,13 +466,13 @@ module MalawiHivProgramReports
451
466
 
452
467
  def create_min_auto_expire_date_indexes(start: false)
453
468
  ActiveRecord::Base.connection.execute <<~SQL
454
- CREATE INDEX idx_min_auto_expire_date#{start ? '_start' : ''} ON temp_min_auto_expire_date#{start ? '_start' : ''} (auto_expire_date)
469
+ CREATE INDEX idx_min_auto_expire_date#{start ? '_start' : ''} ON cdr_temp_min_auto_expire_date#{start ? '_start' : ''} (auto_expire_date)
455
470
  SQL
456
471
  ActiveRecord::Base.connection.execute <<~SQL
457
- CREATE INDEX idx_min_pepfar#{start ? '_start' : ''} ON temp_min_auto_expire_date#{start ? '_start' : ''} (pepfar_defaulter_date)
472
+ CREATE INDEX idx_min_pepfar#{start ? '_start' : ''} ON cdr_temp_min_auto_expire_date#{start ? '_start' : ''} (pepfar_defaulter_date)
458
473
  SQL
459
474
  ActiveRecord::Base.connection.execute <<~SQL
460
- CREATE INDEX idx_min_moh#{start ? '_start' : ''} ON temp_min_auto_expire_date#{start ? '_start' : ''} (moh_defaulter_date)
475
+ CREATE INDEX idx_min_moh#{start ? '_start' : ''} ON cdr_temp_min_auto_expire_date#{start ? '_start' : ''} (moh_defaulter_date)
461
476
  SQL
462
477
  end
463
478
 
@@ -560,7 +575,7 @@ module MalawiHivProgramReports
560
575
 
561
576
  def update_steps(start: false, portion: false)
562
577
  ActiveRecord::Base.connection.execute <<~SQL
563
- UPDATE cdr_temp_patient_outcomes#{start ? '_start' : ''} SET step = 0 WHERE step >= #{portion ? 4 : 1 } AND site_id = #{location}
578
+ UPDATE cdr_temp_patient_outcomes#{start ? '_start' : ''} SET step = 0 WHERE step >= #{portion ? 4 : 1} AND site_id = #{location}
564
579
  SQL
565
580
  end
566
581
 
@@ -588,6 +603,5 @@ module MalawiHivProgramReports
588
603
  end
589
604
  end
590
605
  # rubocop:enable Metrics/ClassLength
591
- # rubocop:enable Metrics/MethodLength
592
606
  end
593
607
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MalawiHivProgramReports
4
- VERSION = '1.1.0'
4
+ VERSION = '1.1.1'
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.0
4
+ version: 1.1.1
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-12 00:00:00.000000000 Z
11
+ date: 2024-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails