malawi_hiv_program_reports 1.1.15 → 1.1.16

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: 7648d6a581512a4f62341d93358beb5dd3895ed261d97aa849e0f143bb16645d
4
- data.tar.gz: 87f0a1eb2a33f5839a97bc9af592b9a4f18bbf15236406b56a163ae0e2641de1
3
+ metadata.gz: 394a402d0b5e0c9b210bbf5feff0959854bb2b8d1322ecd97b07107201dcd694
4
+ data.tar.gz: 51615fcd686c06088e928b6b10e1bba26a754825e9c3d891e0e7c43c00647098
5
5
  SHA512:
6
- metadata.gz: 803bdb400dd06a631cc42c0712f507fc7750e7922c6a9709e13820037bc54f9dbae24360b6d93465f400ca59cb2a609c4dd015f05def601c15742cb6b4e22a8b
7
- data.tar.gz: 365656a38cf502c597cb4536f3aff8adb95241b176f2f3be20ebc6e42a4e8e7f861324a9ecdb35839d7c96157b433f85c31bc4868c6f7b93f59238e0528dd4ff
6
+ metadata.gz: aa30a38678f9f6dbeded4d8b3fe6ae920a46c6bea63937906f330cdf50b2cbc1ca6ae95edcf004caa2b4ca5501c6dbf627210f2f496dd49e1ba3964815e3a28f
7
+ data.tar.gz: cccee12cb7e4e5d9e5e685230b63309f93ad6db7199de3ae244d461a95e0f825f71a0d82f9b01a535636ad2c45fffc617dfcc68460d1f9508853c059ee71be9e
@@ -7,10 +7,11 @@ module MalawiHivProgramReports
7
7
  class CumulativeCohort
8
8
  include MalawiHivProgramReports::Utils::CommonSqlQueryUtils
9
9
 
10
- attr_reader :start_date, :end_date, :locations, :rebuild, :outcome, :definition
10
+ attr_reader :start_date, :end_date, :locations, :rebuild, :outcome, :definition, :iteration, :redis, :start_time
11
11
 
12
12
  LOCK_FILE = 'art_service/reports/cumulative_cohort.lock'
13
13
 
14
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
14
15
  def initialize(start_date:, end_date:, **kwargs)
15
16
  @start_date = ActiveRecord::Base.connection.quote(start_date)
16
17
  @end_date = ActiveRecord::Base.connection.quote(end_date)
@@ -18,13 +19,17 @@ module MalawiHivProgramReports
18
19
  locations = kwargs[:locations]
19
20
  @locations = locations.present? ? locations.split(',') : []
20
21
  @definition = kwargs[:definition] || 'pepfar'
21
-
22
+ @iteration = Concurrent::AtomicFixnum.new(0)
23
+ @start_time = Time.now
24
+ @redis = Redis.new
25
+ @redis.set('cumulative_progress', 0)
26
+ @redis.set('cumulative_status', 'running')
27
+ @redis.set('cumulative_time_taken', 0)
22
28
  definition = @definition.downcase
23
29
  raise ArgumentError, "Invalid outcomes definition: #{definition}" unless %w[moh pepfar].include?(definition)
24
30
  end
25
31
 
26
32
  def find_report
27
- start_time = Time.now
28
33
  handle_tables
29
34
  clear_cohort_status unless rebuild
30
35
  process_thread(locations:)
@@ -33,8 +38,11 @@ module MalawiHivProgramReports
33
38
  handle_failed
34
39
  time_taken_for_failed = ((Time.now - end_time) / 60).round(2)
35
40
  Rails.logger.info("Cumulative Cohort report took #{time_in_minutes} minutes to generate for these locations: #{locations}")
41
+ redis.set('cumulative_time_taken', time_in_minutes)
42
+ redis.set('cumulative_status', 'completed')
36
43
  { cohort_time: time_in_minutes, processing_failed: time_taken_for_failed }
37
44
  end
45
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
38
46
 
39
47
  private
40
48
 
@@ -46,13 +54,12 @@ module MalawiHivProgramReports
46
54
  # 5. We want to get information on clients birthdate etc after joining all the above
47
55
  # 6. Finally we want to get the outcomes of the clients in 5 above
48
56
 
49
- # rubocop:disable Metrics/MethodLength
57
+ # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
50
58
  def process_thread(locations: [])
51
- # use locations to thread but process 10 locations at a time
52
59
  queue = Queue.new
53
60
  locations.each { |loc| queue << loc }
54
61
 
55
- threads = Array.new(40) do
62
+ threads = Array.new(65) do
56
63
  Thread.new do
57
64
  until queue.empty?
58
65
  loc = begin
@@ -64,6 +71,8 @@ module MalawiHivProgramReports
64
71
 
65
72
  ActiveRecord::Base.connection_pool.with_connection do
66
73
  process_data loc
74
+ current_iteration = iteration.increment
75
+ update_progress(current_iteration, locations.size)
67
76
  rescue StandardError => e
68
77
  Rails.logger.info("Error processing location #{loc}: #{e.message}")
69
78
  Rails.logger.info(e.backtrace.join("\n"))
@@ -75,7 +84,18 @@ module MalawiHivProgramReports
75
84
 
76
85
  threads.each(&:join)
77
86
  end
78
- # rubocop:enable Metrics/MethodLength
87
+ # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
88
+
89
+ # update progress in redis and channel
90
+ def update_progress(current_iteration, total)
91
+ percentage = (current_iteration.to_f / total * 100).round(2)
92
+ redis.set('cumulative_progress', percentage)
93
+ ActionCable.server.broadcast('cumulative_updates_channel', {
94
+ progress: percentage,
95
+ status: 'running',
96
+ time_taken: ((Time.now - start_time) / 60).round(2)
97
+ })
98
+ end
79
99
 
80
100
  def reprocess_failed
81
101
  failed_locs = failed_sites&.map { |loc| loc[:site_id] }
@@ -84,6 +104,7 @@ module MalawiHivProgramReports
84
104
  process_thread(locations: failed_locs)
85
105
  end
86
106
 
107
+ # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
87
108
  def handle_failed
88
109
  failed_locs = failed_sites&.map { |loc| loc['site_id'].to_i }
89
110
  return if failed_locs.empty?
@@ -102,9 +123,13 @@ module MalawiHivProgramReports
102
123
  Rails.logger.info("Error processing location #{loc}: #{e.message}")
103
124
  Rails.logger.info(e.backtrace.join("\n"))
104
125
  save_incomplete_site(location: loc, time_taken: 0)
126
+ ensure
127
+ current_iteration = iteration.increment
128
+ update_progress(current_iteration, locations.size)
105
129
  end
106
130
  end
107
131
  end
132
+ # rubocop:enable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
108
133
 
109
134
  def failed_sites
110
135
  ActiveRecord::Base.connection.select_all <<~SQL
@@ -317,7 +317,7 @@ module MalawiHivProgramReports
317
317
  INSERT INTO cdr_temp_patient_outcomes#{start ? '_start' : ''} PARTITION (p#{location})
318
318
  SELECT patient_id, #{patient_outcome_function('patient_id', 'site_id', start)}, NULL, #{location}, 6
319
319
  FROM cdr_temp_cohort_members PARTITION (p#{location})
320
- WHERE date_enrolled <= #{end_date}
320
+ WHERE date_enrolled <= DATE(#{start ? start_date : end_date}) #{start ? '- INTERVAL 1 DAY' : ''}
321
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))
322
322
  ON DUPLICATE KEY UPDATE cum_outcome = VALUES(cum_outcome), outcome_date = VALUES(outcome_date), step = VALUES(step)
323
323
  SQL
@@ -586,7 +586,7 @@ module MalawiHivProgramReports
586
586
 
587
587
  def clear_tables(start: false)
588
588
  # we truncate if @locations is empty otherwise we only clean the specifieds
589
- if location.empty?
589
+ if location.blank?
590
590
  ActiveRecord::Base.connection.execute("TRUNCATE cdr_temp_patient_outcomes#{start ? '_start' : ''}")
591
591
  ActiveRecord::Base.connection.execute("TRUNCATE cdr_temp_max_drug_orders#{start ? '_start' : ''}")
592
592
  ActiveRecord::Base.connection.execute("TRUNCATE cdr_temp_min_auto_expire_date#{start ? '_start' : ''}")
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MalawiHivProgramReports
4
- VERSION = '1.1.15'
4
+ VERSION = '1.1.16'
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.15
4
+ version: 1.1.16
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-07-23 00:00:00.000000000 Z
11
+ date: 2024-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails