malawi_hiv_program_reports 1.0.26 → 1.1.0

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: 53d6565fca96ee2021dfe480bdede7d60e308abc3988c209c86658e56d66baa4
4
- data.tar.gz: c8f56f53589f21725e00c4afe769ffbc330f6a2a687455e6fd43bc128657eb8f
3
+ metadata.gz: 9c6bffbef9d85fec441ab368a631e8e730446a87ed8722d2a60d348c68ecf969
4
+ data.tar.gz: dfd3ba0bfe3a2fc65ca948d3dd2bee21c9f8bb4f7c42ade37623f46c6fb8aefb
5
5
  SHA512:
6
- metadata.gz: b9909ffb6013589e67b6ccce8f7833308270cedb69c74d7ac77a203b2870475e69bbef0c635d88ad51d1095958670313b26c862dc16407358aa448a3eaf1378e
7
- data.tar.gz: a08774fa4213673af1cfa0efa728059a52bbe6dcc0f0dd70174ebaddc68581ba25c50d53a56946c474ed99bd60234762b67c8e57b77f0d94eea8a2477790f27a
6
+ metadata.gz: 9a0217e328962a1734f08e22d28df28624f0a6eff6ca02e1eb32f021e0397758ab58bca452ddada4ccf662cac36b862952c880b98e3c08e500e4160db51af968
7
+ data.tar.gz: c6f54d2927268c90cf1e5b87ea621eb68aa5cea3d6f17095f382f7c7f91bdd0f4360a9ab4e00f8be536414a498c2dd1b3d01d8a7f88e01425dbb0e0292309a1c
@@ -78,14 +78,17 @@ module MalawiHivProgramReports
78
78
 
79
79
  def process_data(location)
80
80
  start_time = Time.now
81
- cdr_other_patient_types location
82
- external_clients location
83
- transfer_ins location
84
- min_drug_orders location
85
- potential_cohort_members location
86
- reason_for_starting_art location
87
- cohort_members location
88
- outcome = MalawiHivProgramReports::Moh::CumulativeOutcome.new(end_date:, location:, definition:, rebuild:, start_date:)
81
+ if rebuild
82
+ cdr_other_patient_types location
83
+ external_clients location
84
+ transfer_ins location
85
+ min_drug_orders location
86
+ potential_cohort_members location
87
+ reason_for_starting_art location
88
+ cohort_members location
89
+ end
90
+ outcome = MalawiHivProgramReports::Moh::CumulativeOutcome.new(end_date:, location:, definition:, rebuild:,
91
+ start_date:)
89
92
  rebuild ? outcome.find_report : outcome.update_outcomes_by_definition
90
93
  end_time = Time.now
91
94
  time_taken = ((end_time - start_time) / 60).round(2)
@@ -428,7 +431,7 @@ module MalawiHivProgramReports
428
431
  end
429
432
  clear_cohort_status
430
433
  end
431
-
434
+
432
435
  def clear_cohort_status
433
436
  if locations.empty?
434
437
  ActiveRecord::Base.connection.execute('TRUNCATE TABLE cdr_temp_cohort_status')
@@ -18,7 +18,8 @@ module MalawiHivProgramReports
18
18
 
19
19
  def find_report
20
20
  if rebuild
21
- cohort = MalawiHivProgramReports::Moh::CumulativeCohort.new(start_date:, end_date:, definition: 'pepfar', locations: location.to_s, rebuild: true)
21
+ cohort = MalawiHivProgramReports::Moh::CumulativeCohort.new(start_date: start_date.to_date, end_date: end_date.to_date, definition: 'pepfar',
22
+ locations: location.to_s, rebuild: true.to_s)
22
23
  cohort.find_report
23
24
  end
24
25
  process_data
@@ -32,10 +33,10 @@ module MalawiHivProgramReports
32
33
  patient_id = pat['patient_id'].to_i
33
34
  outcome = pat['outcome']
34
35
  gender = begin
35
- pat['gender'].first.upcase
36
- rescue StandardError
37
- 'Unknown'
38
- end
36
+ pat['gender'].first.upcase
37
+ rescue StandardError
38
+ 'Unknown'
39
+ end
39
40
  age_group = pat['age_group']
40
41
 
41
42
  if data[age_group].blank?
@@ -70,49 +71,49 @@ module MalawiHivProgramReports
70
71
 
71
72
  def tx_ml_clients
72
73
  <<~SQL
73
- SELECT
74
- e.patient_id,
75
- e.birthdate,
76
- e.gender,
77
- e.date_enrolled,
78
- e.earliest_start_date,
79
- o.outcome_date,
80
- TIMESTAMPDIFF(MONTH, DATE(e.earliest_start_date), DATE(o.outcome_date)) months,
81
- disaggregated_age_group(e.birthdate, DATE('#{end_date}')) age_group,
82
- o.cum_outcome outcome
83
- FROM cdr_temp_cohort_members #{current_partition} e
84
- INNER JOIN cdr_temp_patient_outcomes #{current_partition} o ON e.patient_id = o.patient_id AND o.cum_outcome IN ('Defaulted', 'Patient died', 'Treatment stopped', 'Patient transferred out')
85
- WHERE e.patient_id IN (SELECT patient_id FROM cdr_temp_patient_outcomes_start #{current_partition} WHERE cum_outcome = 'On antiretrovirals')
86
- AND DATE(e.date_enrolled) < '#{start_date.to_date}'
87
- GROUP BY e.patient_id
88
- SQL
74
+ SELECT
75
+ e.patient_id,
76
+ e.birthdate,
77
+ e.gender,
78
+ e.date_enrolled,
79
+ e.earliest_start_date,
80
+ o.outcome_date,
81
+ TIMESTAMPDIFF(MONTH, DATE(e.earliest_start_date), DATE(o.outcome_date)) months,
82
+ disaggregated_age_group(e.birthdate, DATE('#{end_date}')) age_group,
83
+ o.cum_outcome outcome
84
+ FROM cdr_temp_cohort_members #{current_partition} e
85
+ INNER JOIN cdr_temp_patient_outcomes #{current_partition} o ON e.patient_id = o.patient_id AND o.cum_outcome IN ('Defaulted', 'Patient died', 'Treatment stopped', 'Patient transferred out')
86
+ WHERE e.patient_id IN (SELECT patient_id FROM cdr_temp_patient_outcomes_start #{current_partition} WHERE cum_outcome = 'On antiretrovirals')
87
+ AND DATE(e.date_enrolled) < '#{start_date.to_date}'
88
+ GROUP BY e.patient_id
89
+ SQL
89
90
  end
90
91
 
91
92
  def tx_ml_clients_new
92
93
  <<~SQL
93
- SELECT
94
- e.patient_id,
95
- e.birthdate,
96
- e.gender,
97
- e.date_enrolled,
98
- e.earliest_start_date,
99
- o.outcome_date,
100
- TIMESTAMPDIFF(MONTH, DATE(e.earliest_start_date), DATE(o.outcome_date)) months,
101
- disaggregated_age_group(e.birthdate, DATE('#{end_date}')) age_group,
102
- o.cum_outcome outcome
103
- FROM cdr_temp_cohort_members #{current_partition} e
104
- INNER JOIN cdr_temp_patient_outcomes #{current_partition} o ON e.patient_id = o.patient_id AND o.cum_outcome IN ('Defaulted', 'Patient died', 'Treatment stopped', 'Patient transferred out')
105
- WHERE e.date_enrolled BETWEEN DATE('#{start_date}') AND DATE('#{end_date}')
106
- GROUP BY e.patient_id
107
- SQL
94
+ SELECT
95
+ e.patient_id,
96
+ e.birthdate,
97
+ e.gender,
98
+ e.date_enrolled,
99
+ e.earliest_start_date,
100
+ o.outcome_date,
101
+ TIMESTAMPDIFF(MONTH, DATE(e.earliest_start_date), DATE(o.outcome_date)) months,
102
+ disaggregated_age_group(e.birthdate, DATE('#{end_date}')) age_group,
103
+ o.cum_outcome outcome
104
+ FROM cdr_temp_cohort_members #{current_partition} e
105
+ INNER JOIN cdr_temp_patient_outcomes #{current_partition} o ON e.patient_id = o.patient_id AND o.cum_outcome IN ('Defaulted', 'Patient died', 'Treatment stopped', 'Patient transferred out')
106
+ WHERE e.date_enrolled BETWEEN DATE('#{start_date}') AND DATE('#{end_date}')
107
+ GROUP BY e.patient_id
108
+ SQL
108
109
  end
109
110
 
110
111
  def process_tx_ml_clients
111
112
  ActiveRecord::Base.connection.select_all <<~SQL
112
- (#{tx_ml_clients})
113
- UNION
114
- (#{tx_ml_clients_new})
115
- SQL
113
+ (#{tx_ml_clients})
114
+ UNION
115
+ (#{tx_ml_clients_new})
116
+ SQL
116
117
  end
117
118
  end
118
119
  end
@@ -22,7 +22,8 @@ module MalawiHivProgramReports
22
22
  report = init_report
23
23
  addittional_groups report
24
24
  if rebuild
25
- cohort = MalawiHivProgramReports::Moh::CumulativeCohort.new(start_date:, end_date:, definition: 'pepfar', locations: location.to_s, rebuild: true)
25
+ cohort = MalawiHivProgramReports::Moh::CumulativeCohort.new(start_date: start_date.to_date, end_date: end_date.to_date, definition: 'pepfar',
26
+ locations: location.to_s, rebuild: true.to_s)
26
27
  cohort.find_report
27
28
  end
28
29
  process_data report
@@ -120,10 +121,10 @@ module MalawiHivProgramReports
120
121
  else
121
122
  (gender == 'M' ? 'Male' : gender)
122
123
  end,
123
- cd4_less_than_200: age_group_report['cd4_less_than_200'.to_sym],
124
- cd4_greater_than_equal_to_200: age_group_report['cd4_greater_than_equal_to_200'.to_sym],
125
- cd4_unknown_or_not_done: age_group_report['cd4_unknown_or_not_done'.to_sym],
126
- transfer_in: age_group_report['transfer_in'.to_sym]
124
+ cd4_less_than_200: age_group_report[:cd4_less_than_200],
125
+ cd4_greater_than_equal_to_200: age_group_report[:cd4_greater_than_equal_to_200],
126
+ cd4_unknown_or_not_done: age_group_report[:cd4_unknown_or_not_done],
127
+ transfer_in: age_group_report[:transfer_in]
127
128
  }
128
129
  end
129
130
  # rubocop:enable Metrics/AbcSize
@@ -151,48 +152,48 @@ module MalawiHivProgramReports
151
152
 
152
153
  def fetch_data
153
154
  ActiveRecord::Base.connection.select_all <<~SQL
154
- SELECT
155
- e.patient_id,
156
- e.gender,
157
- disaggregated_age_group(e.birthdate, DATE('#{end_date}')) age_group,
158
- CASE
159
- WHEN o.value_numeric < 200 THEN 'cd4_less_than_200'
160
- WHEN o.value_numeric = 200 AND o.value_modifier = '=' THEN 'cd4_greater_than_equal_to_200'
161
- WHEN o.value_numeric = 200 AND o.value_modifier = '<' THEN 'cd4_less_than_200'
162
- WHEN o.value_numeric = 200 AND o.value_modifier = '>' THEN 'cd4_greater_than_equal_to_200'
163
- WHEN o.value_numeric > 200 THEN 'cd4_greater_than_equal_to_200'
164
- ELSE 'cd4_unknown_or_not_done'
165
- END cd4_count_group,
166
- CASE
167
- WHEN e.recorded_start_date IS NULL THEN 1
168
- ELSE 0
169
- END new_patient,
170
- e.date_enrolled,
171
- e.earliest_start_date,
172
- preg_or_breast.name AS maternal_status,
173
- DATE(MIN(pregnant_or_breastfeeding.obs_datetime)) AS maternal_status_date
174
- FROM cdr_temp_cohort_members #{current_partition} e
175
- LEFT JOIN (
176
- SELECT max(o.obs_datetime) AS obs_datetime, o.person_id
177
- FROM obs #{current_partition} o
178
- INNER JOIN concept_name cn ON cn.concept_id = o.concept_id AND cn.name = 'CD4 count' AND cn.voided = 0
179
- INNER JOIN patient_program #{current_partition} pp ON pp.patient_id = o.person_id
180
- AND pp.program_id = #{program('HIV PROGRAM').id}
181
- AND pp.voided = 0
182
- INNER JOIN patient_state #{current_partition} ps ON ps.patient_program_id = pp.patient_program_id AND ps.voided = 0 AND ps.state = 7 AND ps.start_date <= DATE('#{end_date}')
183
- WHERE o.concept_id = #{concept_name('CD4 count').concept_id} AND o.voided = 0
184
- AND o.obs_datetime <= '#{end_date}' AND o.obs_datetime >= '#{start_date}'
185
- GROUP BY o.person_id
186
- ) current_cd4 ON current_cd4.person_id = e.patient_id
187
- LEFT JOIN obs #{current_partition} o ON o.person_id = e.patient_id AND o.concept_id = #{concept_name('CD4 count').concept_id} AND o.voided = 0 AND o.obs_datetime = current_cd4.obs_datetime
188
- LEFT JOIN obs #{current_partition} pregnant_or_breastfeeding ON pregnant_or_breastfeeding.person_id = e.patient_id
189
- AND pregnant_or_breastfeeding.concept_id IN (SELECT concept_id FROM concept_name WHERE name IN ('Breast feeding?', 'Breast feeding', 'Breastfeeding', 'Is patient pregnant?', 'patient pregnant') AND voided = 0)
190
- AND pregnant_or_breastfeeding.voided = 0
191
- AND pregnant_or_breastfeeding.value_coded = #{concept_name('Yes').concept_id}
192
- LEFT JOIN concept_name preg_or_breast ON preg_or_breast.concept_id = pregnant_or_breastfeeding.concept_id AND preg_or_breast.voided = 0
193
- WHERE e.date_enrolled <= '#{end_date}' AND e.date_enrolled >= '#{start_date}'
194
- GROUP BY e.patient_id
195
- SQL
155
+ SELECT
156
+ e.patient_id,
157
+ e.gender,
158
+ disaggregated_age_group(e.birthdate, DATE('#{end_date}')) age_group,
159
+ CASE
160
+ WHEN o.value_numeric < 200 THEN 'cd4_less_than_200'
161
+ WHEN o.value_numeric = 200 AND o.value_modifier = '=' THEN 'cd4_greater_than_equal_to_200'
162
+ WHEN o.value_numeric = 200 AND o.value_modifier = '<' THEN 'cd4_less_than_200'
163
+ WHEN o.value_numeric = 200 AND o.value_modifier = '>' THEN 'cd4_greater_than_equal_to_200'
164
+ WHEN o.value_numeric > 200 THEN 'cd4_greater_than_equal_to_200'
165
+ ELSE 'cd4_unknown_or_not_done'
166
+ END cd4_count_group,
167
+ CASE
168
+ WHEN e.recorded_start_date IS NULL THEN 1
169
+ ELSE 0
170
+ END new_patient,
171
+ e.date_enrolled,
172
+ e.earliest_start_date,
173
+ preg_or_breast.name AS maternal_status,
174
+ DATE(MIN(pregnant_or_breastfeeding.obs_datetime)) AS maternal_status_date
175
+ FROM cdr_temp_cohort_members #{current_partition} e
176
+ LEFT JOIN (
177
+ SELECT max(o.obs_datetime) AS obs_datetime, o.person_id
178
+ FROM obs #{current_partition} o
179
+ INNER JOIN concept_name cn ON cn.concept_id = o.concept_id AND cn.name = 'CD4 count' AND cn.voided = 0
180
+ INNER JOIN patient_program #{current_partition} pp ON pp.patient_id = o.person_id
181
+ AND pp.program_id = #{program('HIV PROGRAM').id}
182
+ AND pp.voided = 0
183
+ INNER JOIN patient_state #{current_partition} ps ON ps.patient_program_id = pp.patient_program_id AND ps.voided = 0 AND ps.state = 7 AND ps.start_date <= DATE('#{end_date}')
184
+ WHERE o.concept_id = #{concept_name('CD4 count').concept_id} AND o.voided = 0
185
+ AND o.obs_datetime <= '#{end_date}' AND o.obs_datetime >= '#{start_date}'
186
+ GROUP BY o.person_id
187
+ ) current_cd4 ON current_cd4.person_id = e.patient_id
188
+ LEFT JOIN obs #{current_partition} o ON o.person_id = e.patient_id AND o.concept_id = #{concept_name('CD4 count').concept_id} AND o.voided = 0 AND o.obs_datetime = current_cd4.obs_datetime
189
+ LEFT JOIN obs #{current_partition} pregnant_or_breastfeeding ON pregnant_or_breastfeeding.person_id = e.patient_id
190
+ AND pregnant_or_breastfeeding.concept_id IN (SELECT concept_id FROM concept_name WHERE name IN ('Breast feeding?', 'Breast feeding', 'Breastfeeding', 'Is patient pregnant?', 'patient pregnant') AND voided = 0)
191
+ AND pregnant_or_breastfeeding.voided = 0
192
+ AND pregnant_or_breastfeeding.value_coded = #{concept_name('Yes').concept_id}
193
+ LEFT JOIN concept_name preg_or_breast ON preg_or_breast.concept_id = pregnant_or_breastfeeding.concept_id AND preg_or_breast.voided = 0
194
+ WHERE e.date_enrolled <= '#{end_date}' AND e.date_enrolled >= '#{start_date}'
195
+ GROUP BY e.patient_id
196
+ SQL
196
197
  end
197
198
  end
198
199
  # rubocop:enable Metrics/ClassLength
@@ -1,163 +1,163 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module MalawiHivProgramReports
3
4
  module Pepfar
4
5
  class TxRtt
6
+ include Utils
7
+ include MalawiHivProgramReports::Utils::CommonSqlQueryUtils
8
+ include MalawiHivProgramReports::Adapters::Moh::Custom
9
+ include MalawiHivProgramReports::Utils::ModelUtils
10
+ attr_reader :start_date, :end_date, :location, :rebuild
11
+
12
+ def initialize(start_date:, end_date:, **kwargs)
13
+ @start_date = ActiveRecord::Base.connection.quote(start_date.to_date.beginning_of_day.strftime('%Y-%m-%d %H:%M:%S'))
14
+ @end_date = ActiveRecord::Base.connection.quote(end_date.to_date.end_of_day.strftime('%Y-%m-%d %H:%M:%S'))
15
+ @rebuild = kwargs[:rebuild]&.casecmp?('true')
16
+ @location = kwargs[:location]
17
+ end
5
18
 
6
- include Utils
7
- include MalawiHivProgramReports::Utils::CommonSqlQueryUtils
8
- include MalawiHivProgramReports::Adapters::Moh::Custom
9
- include MalawiHivProgramReports::Utils::ModelUtils
10
- attr_reader :start_date, :end_date, :location, :rebuild
11
-
12
- def initialize(start_date:, end_date:, **kwargs)
13
- @start_date = ActiveRecord::Base.connection.quote(start_date.to_date.beginning_of_day.strftime('%Y-%m-%d %H:%M:%S'))
14
- @end_date = ActiveRecord::Base.connection.quote(end_date.to_date.end_of_day.strftime('%Y-%m-%d %H:%M:%S'))
15
- @rebuild = kwargs[:rebuild]&.casecmp?(true)
16
- @location = kwargs[:location]
17
- end
18
-
19
- def find_report
20
- if rebuild
21
- cohort = MalawiHivProgramReports::Moh::CumulativeCohort.new(start_date:, end_date:, definition: 'pepfar', locations: location.to_s, rebuild: true)
22
- cohort.find_report
23
- end
24
- process_report
19
+ def find_report
20
+ if rebuild
21
+ cohort = MalawiHivProgramReports::Moh::CumulativeCohort.new(start_date: start_date.to_date, end_date: end_date.to_date, definition: 'pepfar',
22
+ locations: location.to_s, rebuild: true.to_s)
23
+ cohort.find_report
25
24
  end
25
+ process_report
26
+ end
26
27
 
27
- def data
28
- if rebuild
29
- cohort = MalawiHivProgramReports::Moh::CumulativeCohort.new(start_date:, end_date:, definition: 'pepfar', locations: location.to_s, rebuild: true)
30
- cohort.find_report
31
- end
32
- process_report
33
- rescue StandardError => e
34
- Rails.logger.error "Error running TX_RTT Report: #{e}"
35
- Rails.logger.error e.backtrace.join("\n")
36
- raise e
28
+ def data
29
+ if rebuild
30
+ cohort = MalawiHivProgramReports::Moh::CumulativeCohort.new(start_date: start_date.to_date, end_date: end_date.to_date, definition: 'pepfar',
31
+ locations: location.to_s, rebuild: true.to_s)
32
+ cohort.find_report
37
33
  end
34
+ process_report
35
+ rescue StandardError => e
36
+ Rails.logger.error "Error running TX_RTT Report: #{e}"
37
+ Rails.logger.error e.backtrace.join("\n")
38
+ raise e
39
+ end
38
40
 
39
- private
41
+ private
40
42
 
41
- GENDER = %w[M F].freeze
43
+ GENDER = %w[M F].freeze
42
44
 
43
- def init_report
44
- pepfar_age_groups.each_with_object({}) do |age_group, age_group_report|
45
- age_group_report[age_group] = GENDER.each_with_object({}) do |gender, gender_report|
46
- gender_report[gender] = indicators
47
- end
45
+ def init_report
46
+ pepfar_age_groups.each_with_object({}) do |age_group, age_group_report|
47
+ age_group_report[age_group] = GENDER.each_with_object({}) do |gender, gender_report|
48
+ gender_report[gender] = indicators
48
49
  end
49
50
  end
51
+ end
50
52
 
51
- def process_report
52
- report = init_report
53
- process_data report
54
- flatten_the_report report
55
- end
53
+ def process_report
54
+ report = init_report
55
+ process_data report
56
+ flatten_the_report report
57
+ end
56
58
 
57
- def process_data(report)
58
- fetch_data.each do |row|
59
- age_group = row['age_group']
60
- gender = row['gender']
61
- months = row['months']
62
- patient_id = row['patient_id']
63
- cd4_cat = row['cd4_count_group']
59
+ def process_data(report)
60
+ fetch_data.each do |row|
61
+ age_group = row['age_group']
62
+ gender = row['gender']
63
+ months = row['months']
64
+ patient_id = row['patient_id']
65
+ cd4_cat = row['cd4_count_group']
64
66
 
65
- next unless GENDER.include?(gender)
66
- next unless pepfar_age_groups.include?(age_group)
67
+ next unless GENDER.include?(gender)
68
+ next unless pepfar_age_groups.include?(age_group)
67
69
 
68
- cat = report[age_group][gender]
69
- process_months(cat, months, patient_id)
70
- process_cd4(cat, months, patient_id, cd4_cat)
71
- end
70
+ cat = report[age_group][gender]
71
+ process_months(cat, months, patient_id)
72
+ process_cd4(cat, months, patient_id, cd4_cat)
72
73
  end
74
+ end
73
75
 
74
- def process_months(report, months, patient_id)
75
- return report[:returned_less_than_3_months] << patient_id if months.blank?
76
- return report[:returned_less_than_3_months] << patient_id if months < 3
77
- if months >= 3 && months < 6
78
- return report[:returned_greater_than_3_months_and_less_than_6_months] << patient_id
79
- end
76
+ def process_months(report, months, patient_id)
77
+ return report[:returned_less_than_3_months] << patient_id if months.blank?
78
+ return report[:returned_less_than_3_months] << patient_id if months < 3
79
+ return report[:returned_greater_than_3_months_and_less_than_6_months] << patient_id if months >= 3 && months < 6
80
80
 
81
- report[:returned_greater_than_or_equal_to_6_months] << patient_id if months >= 6
81
+ report[:returned_greater_than_or_equal_to_6_months] << patient_id if months >= 6
82
+ end
83
+
84
+ def process_cd4(report, months, patient_id, cd4_cat)
85
+ if cd4_cat == 'unknown_cd4_count' && (months.blank? || months <= 2)
86
+ report[:not_eligible_for_cd4] << patient_id
87
+ return
82
88
  end
83
89
 
84
- def process_cd4(report, months, patient_id, cd4_cat)
85
- if cd4_cat == 'unknown_cd4_count' && (months.blank? || months <= 2)
86
- report[:not_eligible_for_cd4] << patient_id
87
- return
88
- end
90
+ report[cd4_cat.to_sym] << patient_id
91
+ end
89
92
 
90
- report[cd4_cat.to_sym] << patient_id
91
- end
93
+ def indicators
94
+ {
95
+ cd4_less_than_200: [],
96
+ cd4_greater_than_or_equal_to_200: [],
97
+ unknown_cd4_count: [],
98
+ not_eligible_for_cd4: [],
99
+ returned_less_than_3_months: [],
100
+ returned_greater_than_3_months_and_less_than_6_months: [],
101
+ returned_greater_than_or_equal_to_6_months: []
102
+ }
103
+ end
92
104
 
93
- def indicators
94
- {
95
- 'cd4_less_than_200': [],
96
- 'cd4_greater_than_or_equal_to_200': [],
97
- 'unknown_cd4_count': [],
98
- 'not_eligible_for_cd4': [],
99
- 'returned_less_than_3_months': [],
100
- 'returned_greater_than_3_months_and_less_than_6_months': [],
101
- 'returned_greater_than_or_equal_to_6_months': []
102
- }
103
- end
105
+ def process_age_group_report(age_group, gender, age_group_report)
106
+ {
107
+ age_group:,
108
+ gender:,
109
+ cd4_less_than_200: age_group_report[:cd4_less_than_200],
110
+ cd4_greater_than_or_equal_to_200: age_group_report[:cd4_greater_than_or_equal_to_200],
111
+ unknown_cd4_count: age_group_report[:unknown_cd4_count],
112
+ not_eligible_for_cd4: age_group_report[:not_eligible_for_cd4],
113
+ returned_less_than_3_months: age_group_report[:returned_less_than_3_months],
114
+ returned_greater_than_3_months_and_less_than_6_months: age_group_report[:returned_greater_than_3_months_and_less_than_6_months],
115
+ returned_greater_than_or_equal_to_6_months: age_group_report[:returned_greater_than_or_equal_to_6_months]
116
+ }
117
+ end
104
118
 
105
- def process_age_group_report(age_group, gender, age_group_report)
106
- {
107
- age_group:,
108
- gender:,
109
- cd4_less_than_200: age_group_report[:cd4_less_than_200],
110
- cd4_greater_than_or_equal_to_200: age_group_report[:cd4_greater_than_or_equal_to_200],
111
- unknown_cd4_count: age_group_report[:unknown_cd4_count],
112
- not_eligible_for_cd4: age_group_report[:not_eligible_for_cd4],
113
- returned_less_than_3_months: age_group_report[:returned_less_than_3_months],
114
- returned_greater_than_3_months_and_less_than_6_months: age_group_report[:returned_greater_than_3_months_and_less_than_6_months],
115
- returned_greater_than_or_equal_to_6_months: age_group_report[:returned_greater_than_or_equal_to_6_months]
116
- }
119
+ def flatten_the_report(report)
120
+ result = []
121
+ report.each do |age_group, age_group_report|
122
+ result << process_age_group_report(age_group, 'M', age_group_report['M'])
123
+ result << process_age_group_report(age_group, 'F', age_group_report['F'])
117
124
  end
118
-
119
- def flatten_the_report(report)
120
- result = []
121
- report.each do |age_group, age_group_report|
122
- result << process_age_group_report(age_group, 'M', age_group_report['M'])
123
- result << process_age_group_report(age_group, 'F', age_group_report['F'])
124
- end
125
- sorted_results = result.sort_by do |item|
126
- gender_score = item[:gender] == 'Female' ? 0 : 1
127
- age_group_score = pepfar_age_groups.index(item[:age_group])
128
- [gender_score, age_group_score]
129
- end
130
- # sort by gender, start all females and push all males to the end
131
- sorted_results.sort_by { |h| [h[:gender] == 'F' ? 0 : 1] }
125
+ sorted_results = result.sort_by do |item|
126
+ gender_score = item[:gender] == 'Female' ? 0 : 1
127
+ age_group_score = pepfar_age_groups.index(item[:age_group])
128
+ [gender_score, age_group_score]
132
129
  end
130
+ # sort by gender, start all females and push all males to the end
131
+ sorted_results.sort_by { |h| [h[:gender] == 'F' ? 0 : 1] }
132
+ end
133
133
 
134
- def fetch_data
135
- ActiveRecord::Base.connection.select_all <<~SQL
136
- SELECT
137
- e.patient_id,
138
- disaggregated_age_group(e.birthdate, #{end_date}) AS age_group,
139
- e.gender,
140
- s.cum_outcome initial_outcome,
141
- o.cum_outcome final_outcome,
142
- TIMESTAMPDIFF(MONTH, COALESCE(s.outcome_date, c.outcome_date), ord.min_order_date) months,
143
- CASE
144
- WHEN cd4_result.value_numeric < 200 THEN 'cd4_less_than_200'
145
- WHEN cd4_result.value_numeric = 200 AND cd4_result.value_modifier = '=' THEN 'cd4_greater_than_or_equal_to_200'
146
- WHEN cd4_result.value_numeric = 200 AND cd4_result.value_modifier = '<' THEN 'cd4_less_than_200'
147
- WHEN cd4_result.value_numeric = 200 AND cd4_result.value_modifier = '>' THEN 'cd4_greater_than_or_equal_to_200'
148
- WHEN cd4_result.value_numeric > 200 THEN 'cd4_greater_than_or_equal_to_200'
149
- ELSE 'unknown_cd4_count'
150
- END cd4_count_group
151
- FROM cdr_temp_cohort_members #{current_partition} e
152
- INNER JOIN cdr_temp_patient_outcomes #{current_partition} o ON o.patient_id = e.patient_id AND o.cum_outcome = 'On antiretrovirals'
153
- INNER JOIN cdr_temp_patient_outcomes_start #{current_partition} s ON s.patient_id = e.patient_id AND s.cum_outcome IN ('Defaulted', 'Treatment stopped')
154
- INNER JOIN cdr_temp_current_state_start #{current_partition} c ON c.patient_id = e.patient_id
155
- INNER JOIN cdr_temp_max_drug_orders #{current_partition} ord ON ord.patient_id = e.patient_id
156
- LEFT JOIN obs #{current_partition} cd4_result ON cd4_result.person_id = e.patient_id AND cd4_result.concept_id = #{concept_name('CD4 count').concept_id} AND cd4_result.voided = 0
157
- WHERE e.date_enrolled < #{start_date}
158
- GROUP BY e.patient_id
159
- SQL
160
- end
134
+ def fetch_data
135
+ ActiveRecord::Base.connection.select_all <<~SQL
136
+ SELECT
137
+ e.patient_id,
138
+ disaggregated_age_group(e.birthdate, #{end_date}) AS age_group,
139
+ e.gender,
140
+ s.cum_outcome initial_outcome,
141
+ o.cum_outcome final_outcome,
142
+ TIMESTAMPDIFF(MONTH, COALESCE(s.outcome_date, c.outcome_date), ord.min_order_date) months,
143
+ CASE
144
+ WHEN cd4_result.value_numeric < 200 THEN 'cd4_less_than_200'
145
+ WHEN cd4_result.value_numeric = 200 AND cd4_result.value_modifier = '=' THEN 'cd4_greater_than_or_equal_to_200'
146
+ WHEN cd4_result.value_numeric = 200 AND cd4_result.value_modifier = '<' THEN 'cd4_less_than_200'
147
+ WHEN cd4_result.value_numeric = 200 AND cd4_result.value_modifier = '>' THEN 'cd4_greater_than_or_equal_to_200'
148
+ WHEN cd4_result.value_numeric > 200 THEN 'cd4_greater_than_or_equal_to_200'
149
+ ELSE 'unknown_cd4_count'
150
+ END cd4_count_group
151
+ FROM cdr_temp_cohort_members #{current_partition} e
152
+ INNER JOIN cdr_temp_patient_outcomes #{current_partition} o ON o.patient_id = e.patient_id AND o.cum_outcome = 'On antiretrovirals'
153
+ INNER JOIN cdr_temp_patient_outcomes_start #{current_partition} s ON s.patient_id = e.patient_id AND s.cum_outcome IN ('Defaulted', 'Treatment stopped')
154
+ INNER JOIN cdr_temp_current_state_start #{current_partition} c ON c.patient_id = e.patient_id
155
+ INNER JOIN cdr_temp_max_drug_orders #{current_partition} ord ON ord.patient_id = e.patient_id
156
+ LEFT JOIN obs #{current_partition} cd4_result ON cd4_result.person_id = e.patient_id AND cd4_result.concept_id = #{concept_name('CD4 count').concept_id} AND cd4_result.voided = 0
157
+ WHERE e.date_enrolled < #{start_date}
158
+ GROUP BY e.patient_id
159
+ SQL
161
160
  end
162
161
  end
163
- end
162
+ end
163
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MalawiHivProgramReports
4
- VERSION = '1.0.26'
4
+ VERSION = '1.1.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: malawi_hiv_program_reports
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.26
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roy Chanunkha