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 +4 -4
- data/app/services/malawi_hiv_program_reports/moh/cumulative_cohort.rb +12 -9
- data/app/services/malawi_hiv_program_reports/pepfar/tx_ml.rb +41 -40
- data/app/services/malawi_hiv_program_reports/pepfar/tx_new.rb +48 -47
- data/app/services/malawi_hiv_program_reports/pepfar/tx_rtt.rb +133 -133
- data/lib/malawi_hiv_program_reports/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c6bffbef9d85fec441ab368a631e8e730446a87ed8722d2a60d348c68ecf969
|
4
|
+
data.tar.gz: dfd3ba0bfe3a2fc65ca948d3dd2bee21c9f8bb4f7c42ade37623f46c6fb8aefb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
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
|
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
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
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
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
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
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
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
|
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[
|
124
|
-
cd4_greater_than_equal_to_200: age_group_report[
|
125
|
-
cd4_unknown_or_not_done: age_group_report[
|
126
|
-
transfer_in: age_group_report[
|
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
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
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
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|
-
|
41
|
+
private
|
40
42
|
|
41
|
-
|
43
|
+
GENDER = %w[M F].freeze
|
42
44
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
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
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
53
|
+
def process_report
|
54
|
+
report = init_report
|
55
|
+
process_data report
|
56
|
+
flatten_the_report report
|
57
|
+
end
|
56
58
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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
|
-
|
66
|
-
|
67
|
+
next unless GENDER.include?(gender)
|
68
|
+
next unless pepfar_age_groups.include?(age_group)
|
67
69
|
|
68
|
-
|
69
|
-
|
70
|
-
|
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
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
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
|
-
|
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
|
-
|
85
|
-
|
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
|
-
|
91
|
-
|
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
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
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
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
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
|
-
|
120
|
-
|
121
|
-
|
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
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
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
|