malawi_hiv_program_reports 1.0.26 → 1.0.27
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b8fed6ebe30ff7bc1f0573f0b560d33796ee13af2ff4de885c694dd023fbfc7
|
4
|
+
data.tar.gz: 59b5f6b719c14f0eb297ee1ff9e8aabf54e69ffa7ab97cda0f803aa4414f1a01
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 47261e1116d8d96465267866238b02402bddc68c6ff4385590fdf1e3347abebdbbe003250b313a993ae443cff4d9803d408f95a7199e14e4569c3aa335ce3ff8
|
7
|
+
data.tar.gz: a32981a2ab869c4bd28de08625eca90cd3b93bdb267604bc3ebbf9197cb5489dd59728406c035529c513f55832bba41c296e0df10adabeeba6c1039256652582
|
@@ -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:, end_date:, definition: 'pepfar',
|
22
|
+
locations: location.to_s, rebuild: true)
|
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:, end_date:, definition: 'pepfar',
|
31
|
+
locations: location.to_s, rebuild: true)
|
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
|