malawi_hiv_program_reports 1.0.17 → 1.0.19

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: 2528255432f2df888bfc40d425af064647391095c2a0ceebe9512e348fbe2f63
4
- data.tar.gz: 75916f779053f8acf6e1aa4ef258668961266eff12eadd92bdd4cd3cbf3e5a7f
3
+ metadata.gz: f86826f15ceb3f763a71ac85b14142481eb6177dea5e77d63b789c1f76955dfd
4
+ data.tar.gz: ae2542a2e7bc462c5fca169abb73503dfac6f4074eedd6c837a38ff7dbbf6a27
5
5
  SHA512:
6
- metadata.gz: 7f98b4442247cc8fce9df43887e4ee9cccfdae8017d15fd8f67a301c89428cf54a1c1e411c9e61fc4c56f99de41a45a2732303063ecf7b17a18b46efedba8aa3
7
- data.tar.gz: c3d0300aa8341c5bc387cf3ff89b854012d2b093cd058453278fbb378129b0e29ddd650a973d53d0e830953779e922e5a81a5c214006b7cbc53788473a86fa19
6
+ metadata.gz: 2726026897b6c08fdfea543e57b82396897e2400849b39615135dff0b1a1caab8113c910cc1cdb98042ff2211a548c541d8e3c56b68157ca165142659830e4bb
7
+ data.tar.gz: 164b30ad7ca003e832cf4a31ba222c92cee0d04fd0f3aa9875a45ef879d8f33b4dca863ac6afdb60de0f6cfe54a18e799e036d249df9f6ed7be30fb7d6d310c4
@@ -187,6 +187,10 @@ module MalawiHivProgramReports
187
187
  "DATE_PART('#{interval}', #{date2}::timestamp - #{date1}::timestamp)" if adapter == 'postgresql'
188
188
  end
189
189
 
190
+ def current_partition
191
+ "PARTITION (p#{@location})"
192
+ end
193
+
190
194
  # this is a min filter
191
195
  # @occupation: object
192
196
  def min_filt(occupation)
@@ -7,6 +7,7 @@ module MalawiHivProgramReports
7
7
  include MalawiHivProgramReports::Adapters::Moh::Custom
8
8
  include MalawiHivProgramReports::Utils::ModelUtils
9
9
  include Utils
10
+ include Pepfar::Utils
10
11
 
11
12
  def initialize(start_date:, end_date:, min_age:, max_age:, org:, initialize_tables:, **kwargs)
12
13
  @start_date = start_date.to_date.strftime('%Y-%m-%d 00:00:00')
@@ -17,6 +18,7 @@ module MalawiHivProgramReports
17
18
  @initialize_tables = (initialize_tables == 'true')
18
19
  @occupation = kwargs[:occupation]
19
20
  @location = kwargs[:location]
21
+ @format = kwargs[:format] || 'poc'
20
22
  end
21
23
 
22
24
  def find_report
@@ -40,23 +42,56 @@ module MalawiHivProgramReports
40
42
 
41
43
  if @initialize_tables
42
44
  report_type = (/pepfar/i.match?(@org) ? 'pepfar' : 'moh')
43
- MalawiHivProgramReports::Moh::CohortBuilder.new(outcomes_definition: report_type, location: @location).init_temporary_tables(@start_date,
44
- @end_date, @occupation)
45
+ MalawiHivProgramReports::Moh::CohortBuilder.new(outcomes_definition: report_type, location: @location). init_temporary_tables(@start_date, @end_date, @occupation)
45
46
  end
46
47
 
47
48
  patients = ActiveRecord::Base.connection.select_all <<~SQL
48
49
  SELECT
50
+ disaggregated_age_group(p.birthdate, DATE('#{@end_date}')) age_group,
49
51
  p.patient_id, p.date_enrolled, p.birthdate, p.gender,
50
52
  outcome.cum_outcome AS outcome
51
53
  FROM temp_earliest_start_date p
52
54
  LEFT JOIN temp_patient_outcomes outcome USING(patient_id)
53
55
  WHERE DATE(date_enrolled) <= DATE('#{@end_date}')
54
- AND TIMESTAMPDIFF(year, p.birthdate, DATE('#{@end_date}')) BETWEEN #{@min_age} AND #{@max_age}
55
- AND cum_outcome = 'On antiretrovirals';
56
+ AND cum_outcome = 'On antiretrovirals'
57
+ #{@format == 'poc' ? "AND TIMESTAMPDIFF(year, p.birthdate, DATE('#{@end_date}')) BETWEEN #{@min_age} AND #{@max_age}" : ''};
56
58
  SQL
57
59
 
58
60
  return {} if patients.blank?
59
61
 
62
+ def init_report
63
+ pepfar_age_groups.each_with_object({}) do |age_group, report|
64
+ report[age_group] = ['M', 'F'].each_with_object({}) do |gender, age_group_report|
65
+ age_group_report[gender] = {
66
+ less_than_3months: [],
67
+ three_to_five_months: [],
68
+ greater_than_six_months: []
69
+ }
70
+ end
71
+ end
72
+ end
73
+
74
+ if @format === 'pepfar'
75
+ return patients.uniq.each_with_object(init_report) do |patient, report|
76
+ age_group = patient['age_group']
77
+ gender = patient['gender']
78
+
79
+ dispensing_days = get_dispensing_info(patient['patient_id'], encounter_type, arv_concept_set, program_id)
80
+
81
+ return [] if dispensing_days.blank?
82
+
83
+ if dispensing_days < 90
84
+ report[age_group][gender][:greater_than_six_months] << patient['patient_id']
85
+ end
86
+ if dispensing_days >= 90 && dispensing_days <= 152
87
+ report[age_group][gender][:three_to_five_months] << patient['patient_id']
88
+ end
89
+ if dispensing_days >= 153
90
+ report[age_group][gender][:greater_than_six_months] << patient['patient_id']
91
+ end
92
+ end
93
+ end
94
+
60
95
  data = []
61
96
  patients.each do |p|
62
97
  data << [p['patient_id'].to_i, p['gender'], p['birthdate']]
@@ -223,7 +223,7 @@ module MalawiHivProgramReports
223
223
  # rubocop:disable Metrics/MethodLength
224
224
  def cohort_members(location)
225
225
  ActiveRecord::Base.connection.execute <<~SQL
226
- INSERT INTO cdr_temp_cohort_members
226
+ INSERT INTO cdr_temp_cohort_members PARTITION (p#{location})
227
227
  SELECT
228
228
  pcm.patient_id,
229
229
  pcm.site_id,
@@ -252,7 +252,7 @@ module MalawiHivProgramReports
252
252
 
253
253
  def potential_cohort_members(location)
254
254
  ActiveRecord::Base.connection.execute <<~SQL
255
- INSERT INTO cdr_temp_potential_cohort_members
255
+ INSERT INTO cdr_temp_potential_cohort_members PARTITION (p#{location})
256
256
  SELECT
257
257
  pp.patient_id,
258
258
  pp.site_id,
@@ -275,7 +275,7 @@ module MalawiHivProgramReports
275
275
 
276
276
  def min_drug_orders(location)
277
277
  ActiveRecord::Base.connection.execute <<~SQL
278
- INSERT INTO cdr_temp_min_drug_orders
278
+ INSERT INTO cdr_temp_min_drug_orders PARTITION (p#{location})
279
279
  SELECT o.patient_id, o.site_id, DATE(MIN(o.start_date)) start_date
280
280
  FROM orders o
281
281
  INNER JOIN drug_order do ON do.order_id = o.order_id AND do.site_id = o.site_id AND do.quantity > 0
@@ -300,7 +300,7 @@ module MalawiHivProgramReports
300
300
 
301
301
  def transfer_ins(location)
302
302
  ActiveRecord::Base.connection.execute <<~SQL
303
- INSERT INTO cdr_temp_transfer_ins
303
+ INSERT INTO cdr_temp_transfer_ins PARTITION (p#{location})
304
304
  SELECT o.person_id, o.site_id, DATE(MIN(o.value_datetime)) value_datetime
305
305
  FROM obs o
306
306
  INNER JOIN encounter e ON e.patient_id = o.person_id AND e.site_id = o.site_id AND e.encounter_id = o.encounter_id
@@ -316,7 +316,7 @@ module MalawiHivProgramReports
316
316
 
317
317
  def external_clients(location)
318
318
  ActiveRecord::Base.connection.execute <<~SQL
319
- INSERT INTO cdr_temp_external_clients
319
+ INSERT INTO cdr_temp_external_clients PARTITION (p#{location})
320
320
  SELECT e.patient_id, e.site_id, GROUP_CONCAT(DISTINCT(patient_type.value_coded)) AS patient_types, clinic_registration.encounter_id
321
321
  FROM patient_program as e
322
322
  INNER JOIN obs AS patient_type ON patient_type.person_id = e.patient_id
@@ -342,7 +342,7 @@ module MalawiHivProgramReports
342
342
 
343
343
  def cdr_other_patient_types(location)
344
344
  ActiveRecord::Base.connection.execute <<~SQL
345
- INSERT INTO cdr_other_patient_types
345
+ INSERT INTO cdr_other_patient_types PARTITION (p#{location})
346
346
  SELECT o.person_id, o.site_id
347
347
  FROM obs o
348
348
  WHERE o.concept_id = 3289 -- Type of patient
@@ -358,7 +358,7 @@ module MalawiHivProgramReports
358
358
 
359
359
  def reason_for_starting_art(location)
360
360
  ActiveRecord::Base.connection.execute <<~SQL
361
- INSERT INTO cdr_reason_for_starting_art
361
+ INSERT INTO cdr_reason_for_starting_art PARTITION (p#{location})
362
362
  SELECT a.person_id, a.site_id, a.value_coded
363
363
  FROM obs a
364
364
  INNER JOIN cdr_temp_potential_cohort_members ct ON ct.patient_id = a.person_id AND ct.site_id = a.site_id
@@ -23,7 +23,7 @@ module MalawiHivProgramReports
23
23
  process_data
24
24
  end_time = Time.now
25
25
  total_time_in_minutes = ((end_time - start_time) / 60).round(2)
26
- Rails.logger.info("Cumulative Outcome report completed in #{total_time_in_minutes} minutes")
26
+ Rails.logger.info("Cumulative Outcome report completed in #{total_time_in_minutes} minutes for location: #{location}")
27
27
  { cumulative_outcome_time: total_time_in_minutes, definition: }
28
28
  end
29
29
 
@@ -70,7 +70,7 @@ module MalawiHivProgramReports
70
70
 
71
71
  def load_max_drug_orders
72
72
  ActiveRecord::Base.connection.execute <<~SQL
73
- INSERT INTO cdr_temp_max_drug_orders
73
+ INSERT INTO cdr_temp_max_drug_orders PARTITION (p#{location})
74
74
  SELECT o.patient_id, o.site_id, MAX(o.start_date) AS start_date
75
75
  FROM orders o
76
76
  INNER JOIN cdr_temp_cohort_members tesd ON tesd.patient_id = o.patient_id AND tesd.site_id = o.site_id
@@ -87,7 +87,7 @@ module MalawiHivProgramReports
87
87
 
88
88
  def load_min_auto_expire_date
89
89
  ActiveRecord::Base.connection.execute <<~SQL
90
- INSERT INTO cdr_temp_min_auto_expire_date
90
+ INSERT INTO cdr_temp_min_auto_expire_date PARTITION (p#{location})
91
91
  SELECT patient_id, o.site_id, MIN(auto_expire_date) AS auto_expire_date
92
92
  FROM orders o
93
93
  INNER JOIN cdr_temp_max_drug_orders USING (patient_id, site_id, start_date)
@@ -104,7 +104,7 @@ module MalawiHivProgramReports
104
104
 
105
105
  def load_max_appointment_date
106
106
  ActiveRecord::Base.connection.execute <<~SQL
107
- INSERT INTO cdr_temp_max_patient_appointment
107
+ INSERT INTO cdr_temp_max_patient_appointment PARTITION (p#{location})
108
108
  SELECT o.person_id, o.site_id, DATE(MAX(o.value_datetime)) appointment_date
109
109
  FROM obs o
110
110
  INNER JOIN encounter e ON e.encounter_id = o.encounter_id AND e.site_id = o.site_id AND e.voided = 0
@@ -118,7 +118,7 @@ module MalawiHivProgramReports
118
118
 
119
119
  def load_max_patient_state
120
120
  ActiveRecord::Base.connection.execute <<~SQL
121
- INSERT INTO cdr_temp_max_patient_state
121
+ INSERT INTO cdr_temp_max_patient_state PARTITION (p#{location})
122
122
  SELECT pp.patient_id, pp.site_id, MAX(ps.start_date) start_date
123
123
  FROM patient_state ps
124
124
  INNER JOIN patient_program pp ON pp.patient_program_id = ps.patient_program_id AND pp.site_id = ps.site_id AND pp.program_id = 1 AND pp.voided = 0
@@ -134,7 +134,7 @@ module MalawiHivProgramReports
134
134
  # into the temp_patient_outcomes table.
135
135
  def load_patients_who_died
136
136
  ActiveRecord::Base.connection.execute <<~SQL
137
- INSERT INTO cdr_temp_patient_outcomes
137
+ INSERT INTO cdr_temp_patient_outcomes PARTITION (p#{location})
138
138
  SELECT patients.patient_id, 'Patient died', patient_state.start_date, patients.site_id, 1
139
139
  FROM cdr_temp_cohort_members AS patients
140
140
  INNER JOIN patient_program
@@ -163,7 +163,7 @@ module MalawiHivProgramReports
163
163
  # treatment stopped into temp_patient_outcomes table.
164
164
  def load_patients_who_stopped_treatment
165
165
  ActiveRecord::Base.connection.execute <<~SQL
166
- INSERT INTO cdr_temp_patient_outcomes
166
+ INSERT INTO cdr_temp_patient_outcomes PARTITION (p#{location})
167
167
  SELECT patients.patient_id,
168
168
  (
169
169
  SELECT name FROM concept_name
@@ -192,7 +192,7 @@ module MalawiHivProgramReports
192
192
  AND max_patient_state.site_id = patient_state.site_id
193
193
  AND max_patient_state.start_date = patient_state.start_date
194
194
  WHERE patients.date_enrolled <= #{end_date} AND patients.site_id = #{location}
195
- AND (patients.patient_id, patients.site_id) NOT IN (SELECT patient_id, site_id FROM cdr_temp_patient_outcomes WHERE step = 1)
195
+ AND (patients.patient_id, patients.site_id) NOT IN (SELECT patient_id, site_id FROM cdr_temp_patient_outcomes PARTITION (p#{location}) WHERE step = 1)
196
196
  GROUP BY patients.patient_id, patients.site_id
197
197
  ON DUPLICATE KEY UPDATE cum_outcome = VALUES(cum_outcome), outcome_date = VALUES(outcome_date), step = VALUES(step)
198
198
  SQL
@@ -201,7 +201,7 @@ module MalawiHivProgramReports
201
201
  # Load all patients on Pre-ART.
202
202
  def load_patients_on_pre_art
203
203
  ActiveRecord::Base.connection.execute <<~SQL
204
- INSERT INTO cdr_temp_patient_outcomes
204
+ INSERT INTO cdr_temp_patient_outcomes PARTITION (p#{location})
205
205
  SELECT patients.patient_id,
206
206
  CASE
207
207
  WHEN #{current_defaulter_function('patients.patient_id', 'patients.site_id')} = 1 THEN 'Defaulted'
@@ -226,7 +226,7 @@ module MalawiHivProgramReports
226
226
  AND max_patient_state.site_id = patient_state.site_id
227
227
  AND max_patient_state.start_date = patient_state.start_date
228
228
  WHERE patients.date_enrolled <= #{end_date} AND patients.site_id = #{location}
229
- AND (patients.patient_id, patients.site_id) NOT IN (SELECT patient_id, site_id FROM cdr_temp_patient_outcomes WHERE step IN (1, 2))
229
+ AND (patients.patient_id, patients.site_id) NOT IN (SELECT patient_id, site_id FROM cdr_temp_patient_outcomes PARTITION (p#{location}) WHERE step IN (1, 2))
230
230
  GROUP BY patients.patient_id, patients.site_id
231
231
  ON DUPLICATE KEY UPDATE cum_outcome = VALUES(cum_outcome), outcome_date = VALUES(outcome_date), step = VALUES(step)
232
232
  SQL
@@ -235,7 +235,7 @@ module MalawiHivProgramReports
235
235
  # Load all patients without a state
236
236
  def load_patients_without_state
237
237
  ActiveRecord::Base.connection.execute <<~SQL
238
- INSERT INTO cdr_temp_patient_outcomes
238
+ INSERT INTO cdr_temp_patient_outcomes PARTITION (p#{location})
239
239
  SELECT patients.patient_id,
240
240
  CASE
241
241
  WHEN #{current_defaulter_function('patients.patient_id', 'patients.site_id')} = 1 THEN 'Defaulted'
@@ -254,7 +254,7 @@ module MalawiHivProgramReports
254
254
  FROM patient_state
255
255
  WHERE start_date < DATE(#{end_date}) + INTERVAL 1 DAY AND voided = 0
256
256
  )
257
- AND (patients.patient_id, patients.site_id) NOT IN (SELECT patient_id, site_id FROM cdr_temp_patient_outcomes WHERE step IN (1, 2, 3))
257
+ AND (patients.patient_id, patients.site_id) NOT IN (SELECT patient_id, site_id FROM cdr_temp_patient_outcomes PARTITION (p#{location}) WHERE step IN (1, 2, 3))
258
258
  GROUP BY patients.patient_id, patients.site_id
259
259
  HAVING cum_outcome = 'Defaulted'
260
260
  ON DUPLICATE KEY UPDATE cum_outcome = VALUES(cum_outcome), outcome_date = VALUES(outcome_date), step = VALUES(step)
@@ -265,14 +265,14 @@ module MalawiHivProgramReports
265
265
  # without a quantity.
266
266
  def load_patients_without_drug_orders
267
267
  ActiveRecord::Base.connection.execute <<~SQL
268
- INSERT INTO cdr_temp_patient_outcomes
268
+ INSERT INTO cdr_temp_patient_outcomes PARTITION (p#{location})
269
269
  SELECT patients.patient_id,
270
270
  'Unknown',
271
271
  NULL, patients.site_id, 5
272
272
  FROM cdr_temp_cohort_members AS patients
273
273
  WHERE date_enrolled <= #{end_date} AND site_id = #{location}
274
- AND (patient_id, site_id) NOT IN (SELECT patient_id, site_id FROM cdr_temp_patient_outcomes WHERE step IN (1, 2, 3, 4))
275
- AND (patient_id, site_id) NOT IN (SELECT patient_id, site_id FROM cdr_temp_max_drug_orders)
274
+ AND (patient_id, site_id) NOT IN (SELECT patient_id, site_id FROM cdr_temp_patient_outcomes PARTITION (p#{location}) WHERE step IN (1, 2, 3, 4))
275
+ AND (patient_id, site_id) NOT IN (SELECT patient_id, site_id FROM cdr_temp_max_drug_orders PARTITION (p#{location}))
276
276
  ON DUPLICATE KEY UPDATE cum_outcome = VALUES(cum_outcome), outcome_date = VALUES(outcome_date), step = VALUES(step)
277
277
  SQL
278
278
  end
@@ -280,7 +280,7 @@ module MalawiHivProgramReports
280
280
  # Loads all patients who are on treatment
281
281
  def load_patients_on_treatment
282
282
  ActiveRecord::Base.connection.execute <<~SQL
283
- INSERT INTO cdr_temp_patient_outcomes
283
+ INSERT INTO cdr_temp_patient_outcomes PARTITION (p#{location})
284
284
  SELECT patients.patient_id, 'On antiretrovirals', patient_state.start_date, patients.site_id, 6
285
285
  FROM cdr_temp_cohort_members AS patients
286
286
  INNER JOIN patient_program
@@ -310,7 +310,7 @@ module MalawiHivProgramReports
310
310
  AND first_order_to_expire.site_id = patient_program.site_id
311
311
  AND (first_order_to_expire.auto_expire_date >= #{end_date} OR TIMESTAMPDIFF(DAY,first_order_to_expire.auto_expire_date, #{end_date}) <= #{@definition == 'pepfar' ? 28 : 56})
312
312
  WHERE patients.date_enrolled <= #{end_date} AND patients.site_id = #{location}
313
- AND (patients.patient_id, patients.site_id) NOT IN (SELECT patient_id, site_id FROM cdr_temp_patient_outcomes WHERE step IN (1, 2, 3, 4, 5))
313
+ AND (patients.patient_id, patients.site_id) NOT IN (SELECT patient_id, site_id FROM cdr_temp_patient_outcomes PARTITION (p#{location}) WHERE step IN (1, 2, 3, 4, 5))
314
314
  GROUP BY patients.patient_id, patients.site_id
315
315
  ON DUPLICATE KEY UPDATE cum_outcome = VALUES(cum_outcome), outcome_date = VALUES(outcome_date), step = VALUES(step)
316
316
  SQL
@@ -318,7 +318,7 @@ module MalawiHivProgramReports
318
318
 
319
319
  def load_without_clinical_contact
320
320
  ActiveRecord::Base.connection.execute <<~SQL
321
- INSERT INTO cdr_temp_patient_outcomes
321
+ INSERT INTO cdr_temp_patient_outcomes PARTITION (p#{location})
322
322
  SELECT patients.patient_id, 'Defaulted', null, patients.site_id, 7
323
323
  FROM cdr_temp_cohort_members AS patients
324
324
  INNER JOIN patient_program
@@ -349,7 +349,7 @@ module MalawiHivProgramReports
349
349
  AND first_order_to_expire.auto_expire_date < #{end_date}
350
350
  AND TIMESTAMPDIFF(DAY,first_order_to_expire.auto_expire_date, #{end_date}) >= 365
351
351
  WHERE patients.date_enrolled <= #{end_date} AND patients.site_id = #{location}
352
- AND (patients.patient_id, patients.site_id) NOT IN (SELECT patient_id, site_id FROM cdr_temp_patient_outcomes WHERE step IN (1, 2, 3, 4, 5, 6))
352
+ AND (patients.patient_id, patients.site_id) NOT IN (SELECT patient_id, site_id FROM cdr_temp_patient_outcomes PARTITION (p#{location}) WHERE step IN (1, 2, 3, 4, 5, 6))
353
353
  GROUP BY patients.patient_id, patients.site_id
354
354
  ON DUPLICATE KEY UPDATE cum_outcome = VALUES(cum_outcome), outcome_date = VALUES(outcome_date), step = VALUES(step)
355
355
  SQL
@@ -358,11 +358,11 @@ module MalawiHivProgramReports
358
358
  # Load defaulters
359
359
  def load_defaulters
360
360
  ActiveRecord::Base.connection.execute <<~SQL
361
- INSERT INTO cdr_temp_patient_outcomes
361
+ INSERT INTO cdr_temp_patient_outcomes PARTITION (p#{location})
362
362
  SELECT patient_id, #{patient_outcome_function('patient_id', 'site_id')}, NULL, site_id, 8
363
363
  FROM cdr_temp_cohort_members
364
364
  WHERE date_enrolled <= #{end_date} AND site_id = #{location}
365
- AND (patient_id, site_id) NOT IN (SELECT patient_id, site_id FROM cdr_temp_patient_outcomes WHERE step IN (1, 2, 3, 4, 5, 6, 7))
365
+ AND (patient_id, site_id) NOT IN (SELECT patient_id, site_id FROM cdr_temp_patient_outcomes PARTITION (p#{location}) WHERE step IN (1, 2, 3, 4, 5, 6, 7))
366
366
  ON DUPLICATE KEY UPDATE cum_outcome = VALUES(cum_outcome), outcome_date = VALUES(outcome_date), step = VALUES(step)
367
367
  SQL
368
368
  end
@@ -32,8 +32,8 @@ module MalawiHivProgramReports
32
32
  p.birthdate AS birthdate,
33
33
  p.gender,
34
34
  MIN(pi.identifier)
35
- FROM person p
36
- LEFT JOIN patient_identifier pi ON pi.patient_id = p.person_id AND pi.voided = 0 AND pi.identifier_type = 4
35
+ FROM person #{current_partition} p
36
+ LEFT JOIN patient_identifier #{current_partition} pi ON pi.patient_id = p.person_id AND pi.voided = 0 AND pi.identifier_type = 4
37
37
  WHERE p.voided = 0
38
38
  AND #{in_manager(column: 'p.person_id', values: ids)}
39
39
  #{site_manager(operator: 'AND', column: 'p.site_id', location: location)}
@@ -50,25 +50,25 @@ module MalawiHivProgramReports
50
50
  o.patient_id, drug.drug_id, o.order_id, i.identifier,
51
51
  drug.name, d.quantity, o.start_date, obs.value_numeric,
52
52
  person.birthdate, person.gender
53
- FROM orders o
54
- INNER JOIN drug_order d ON d.order_id = o.order_id AND d.quantity > 0
55
- INNER JOIN drug ON drug.drug_id = d.drug_inventory_id
56
- INNER JOIN arv_drug On arv_drug.drug_id = drug.drug_id
57
- INNER JOIN temp_patient_outcomes t ON o.patient_id = t.patient_id AND t.cum_outcome = 'On antiretrovirals'
58
- INNER JOIN person ON person.person_id = o.patient_id AND person.voided = 0
53
+ FROM orders #{current_partition} o
54
+ INNER JOIN drug_order #{current_partition} d ON d.order_id = o.order_id AND d.quantity > 0
55
+ INNER JOIN drug #{current_partition} ON drug.drug_id = d.drug_inventory_id
56
+ INNER JOIN arv_drug #{current_partition} On arv_drug.drug_id = drug.drug_id
57
+ INNER JOIN temp_patient_outcomes #{current_partition} t ON o.patient_id = t.patient_id AND t.cum_outcome = 'On antiretrovirals'
58
+ INNER JOIN person #{current_partition} ON person.person_id = o.patient_id AND person.voided = 0
59
59
  #{site_manager(operator: 'AND', column: 'person.site_id', location: @location)}
60
60
  INNER JOIN (
61
61
  SELECT MAX(o.start_date) start_date, o.patient_id
62
- FROM orders o
63
- INNER JOIN drug_order dor ON dor.order_id = o.order_id AND dor.quantity > 0
64
- AND dor.drug_inventory_id IN (SELECT drug_id FROM arv_drug)
62
+ FROM orders #{current_partition} o
63
+ INNER JOIN drug_order #{current_partition} dor ON dor.order_id = o.order_id AND dor.quantity > 0
64
+ AND dor.drug_inventory_id IN (SELECT drug_id FROM arv_drug #{current_partition})
65
65
  WHERE o.voided = 0
66
66
  AND o.start_date <= '#{@end_date.to_date.strftime("%Y-%m-%d 23:59:59")}'
67
67
  AND o.start_date >= '#{@start_date.to_date.strftime("%Y-%m-%d 00:00:00")}'
68
68
  GROUP BY o.patient_id
69
69
  ) lor ON lor.start_date = o.start_date AND lor.patient_id = o.patient_id
70
- LEFT JOIN obs on obs.order_id = o.order_id AND obs.concept_id=#{pills_dispensed} AND obs.voided = 0
71
- LEFT JOIN patient_identifier i ON i.patient_id = o.patient_id
70
+ LEFT JOIN obs #{current_partition} on obs.order_id = o.order_id AND obs.concept_id=#{pills_dispensed} AND obs.voided = 0
71
+ LEFT JOIN patient_identifier #{current_partition} i ON i.patient_id = o.patient_id
72
72
  AND i.identifier_type = #{patient_identifier_type} AND i.voided = 0
73
73
  WHERE o.voided = 0
74
74
  AND o.start_date <= '#{@end_date.to_date.strftime("%Y-%m-%d 23:59:59")}'
@@ -121,7 +121,7 @@ module MalawiHivProgramReports
121
121
  encounter_type_id = ::EncounterType.find_by_name('DISPENSING').id
122
122
  arv_concept_id = ::ConceptName.find_by_name('Antiretroviral drugs').concept_id
123
123
 
124
- drug_ids = ::Drug.joins('INNER JOIN concept_set s ON s.concept_id = drug.concept_id')\
124
+ drug_ids = ::Drug.joins('INNER JOIN concept_set #{current_partition} s ON s.concept_id = drug.concept_id')\
125
125
  .where('s.concept_set = ?', arv_concept_id).map(&:drug_id)
126
126
 
127
127
  ActiveRecord::Base.connection.execute('drop table if exists tmp_latest_arv_dispensation ;')
@@ -129,7 +129,7 @@ module MalawiHivProgramReports
129
129
  ActiveRecord::Base.connection.execute("
130
130
  create table tmp_latest_arv_dispensation AS
131
131
  SELECT patient_id,DATE(MAX(start_date)) as start_date
132
- FROM orders INNER JOIN drug_order t USING (order_id)
132
+ FROM orders #{current_partition} INNER JOIN drug_order #{current_partition} t USING (order_id)
133
133
  WHERE
134
134
  (
135
135
  start_date BETWEEN '#{@start_date.to_date.strftime('%Y-%m-%d 00:00:00')}' AND '#{@end_date.to_date.strftime('%Y-%m-%d 23:59:59')}'
@@ -146,10 +146,10 @@ module MalawiHivProgramReports
146
146
  o.order_id,
147
147
  MIN(d.quantity),
148
148
  MIN(drug.name)
149
- FROM orders o
150
- INNER JOIN drug_order d ON o.order_id = d.order_id
151
- INNER JOIN drug ON d.drug_inventory_id = drug.drug_id
152
- INNER JOIN tmp_latest_arv_dispensation k on (o.patient_id = k.patient_id and DATE(o.start_date) = k.start_date)
149
+ FROM orders #{current_partition} o
150
+ INNER JOIN drug_order #{current_partition} d ON o.order_id = d.order_id
151
+ INNER JOIN drug #{current_partition} ON d.drug_inventory_id = drug.drug_id
152
+ INNER JOIN tmp_latest_arv_dispensation #{current_partition} k on (o.patient_id = k.patient_id and DATE(o.start_date) = k.start_date)
153
153
  WHERE #{in_manager(column: 'd.drug_inventory_id', values: drug_ids)}
154
154
  AND d.quantity > 0 AND o.voided = 0 AND o.start_date BETWEEN '#{@start_date.to_date.strftime('%Y-%m-%d 00:00:00')}'
155
155
  AND '#{@end_date.to_date.strftime('%Y-%m-%d 23:59:59')}'
@@ -169,9 +169,9 @@ module MalawiHivProgramReports
169
169
  date_antiretrovirals_started(CAST(p.patient_id AS INT), CAST(MIN(s.start_date) AS DATE), CAST(#{@location} AS INT)) AS earliest_start_date
170
170
  FROM
171
171
  ((patient_program p
172
- LEFT JOIN person pe ON (pe.person_id = p.patient_id))
173
- LEFT JOIN patient_state s ON (p.patient_program_id = s.patient_program_id))
174
- LEFT JOIN person ON (person.person_id = p.patient_id)
172
+ LEFT JOIN person #{current_partition} pe ON (pe.person_id = p.patient_id))
173
+ LEFT JOIN patient_state #{current_partition} s ON (p.patient_program_id = s.patient_program_id))
174
+ LEFT JOIN person #{current_partition} ON (person.person_id = p.patient_id)
175
175
  WHERE
176
176
  (p.voided = 0
177
177
  AND s.voided = 0
@@ -184,7 +184,7 @@ module MalawiHivProgramReports
184
184
  end
185
185
 
186
186
  def max_patient_order_date(patient_id)
187
- drug_ids = Drug.joins('INNER JOIN concept_set s ON s.concept_id = drug.concept_id')\
187
+ drug_ids = Drug.joins('INNER JOIN concept_set #{current_partition} s ON s.concept_id = drug.concept_id')\
188
188
  .where('s.concept_set = ?', ConceptName.find_by_name('Antiretroviral drugs').concept_id).map(&:drug_id)
189
189
 
190
190
  Order.joins(drug_order: :drug)
@@ -202,7 +202,7 @@ module MalawiHivProgramReports
202
202
  encounter_type_id = EncounterType.find_by_name('DISPENSING').id
203
203
  arv_concept_id = ConceptName.find_by_name('Antiretroviral drugs').concept_id
204
204
 
205
- drug_ids = Drug.joins('INNER JOIN concept_set s ON s.concept_id = drug.concept_id')\
205
+ drug_ids = Drug.joins('INNER JOIN concept_set #{current_partition} s ON s.concept_id = drug.concept_id')\
206
206
  .where('s.concept_set = ?', arv_concept_id).map(&:drug_id)
207
207
  max_order_date = max_patient_order_date(patient_id)
208
208
  orders = Order.joins(drug_order: :drug)
@@ -258,9 +258,9 @@ module MalawiHivProgramReports
258
258
  SELECT
259
259
  p.birthdate, p.gender, i.identifier arv_number,
260
260
  n.given_name, n.family_name
261
- FROM person p
262
- LEFT JOIN person_name n ON n.person_id = p.person_id AND n.voided = 0
263
- LEFT JOIN patient_identifier i ON i.patient_id = p.person_id
261
+ FROM person #{current_partition} p
262
+ LEFT JOIN person_name #{current_partition} n ON n.person_id = p.person_id AND n.voided = 0
263
+ LEFT JOIN patient_identifier #{current_partition} i ON i.patient_id = p.person_id
264
264
  AND i.identifier_type = 4 AND i.voided = 0
265
265
  WHERE p.person_id = #{patient_id} GROUP BY p.person_id
266
266
  ORDER BY n.date_created DESC, i.date_created DESC;
@@ -305,7 +305,7 @@ module MalawiHivProgramReports
305
305
 
306
306
 
307
307
  outcome_status = ActiveRecord::Base.connection.select_one <<~SQL
308
- SELECT cum_outcome FROM temp_patient_outcomes WHERE patient_id = #{patient_id};
308
+ SELECT cum_outcome FROM temp_patient_outcomes #{current_partition} WHERE patient_id = #{patient_id};
309
309
  SQL
310
310
 
311
311
  next if outcome_status.blank?
@@ -333,9 +333,9 @@ module MalawiHivProgramReports
333
333
  SELECT
334
334
  p.birthdate, p.gender, i.identifier arv_number,
335
335
  n.given_name, n.family_name, p.person_id
336
- FROM person p
337
- LEFT JOIN person_name n ON n.person_id = p.person_id AND n.voided = 0
338
- LEFT JOIN patient_identifier i ON i.patient_id = p.person_id
336
+ FROM person #{current_partition} p
337
+ LEFT JOIN person_name #{current_partition} n ON n.person_id = p.person_id AND n.voided = 0
338
+ LEFT JOIN patient_identifier #{current_partition} i ON i.patient_id = p.person_id
339
339
  AND i.identifier_type = 4 AND i.voided = 0
340
340
  WHERE p.person_id = #{patient_id}
341
341
  GROUP BY p.person_id, n.given_name, n.family_name, i.identifier, p.birthdate, p.gender
@@ -401,17 +401,17 @@ module MalawiHivProgramReports
401
401
  # ActiveRecord::Base.connection.select_one <<~SQL
402
402
  # SELECT lab_result_obs.obs_datetime AS result_date,
403
403
  # CONCAT (COALESCE(measure.value_modifier, '='),' ',COALESCE(measure.value_numeric, measure.value_text, '')) as result
404
- # FROM obs AS lab_result_obs
405
- # INNER JOIN orders
404
+ # FROM obs #{current_partition} AS lab_result_obs
405
+ # INNER JOIN orders #{current_partition}
406
406
  # ON orders.order_id = lab_result_obs.order_id
407
407
  # AND orders.voided = 0
408
- # INNER JOIN obs AS measure
408
+ # INNER JOIN obs #{current_partition} AS measure
409
409
  # ON measure.obs_group_id = lab_result_obs.obs_id
410
410
  # AND measure.voided = 0
411
411
  # INNER JOIN (
412
412
  # SELECT concept_id, name
413
- # FROM concept_name
414
- # INNER JOIN concept USING (concept_id)
413
+ # FROM concept_name #{current_partition}
414
+ # INNER JOIN USING (concept_id)
415
415
  # WHERE concept.retired = 0
416
416
  # AND name NOT LIKE 'Lab test result'
417
417
  # GROUP BY concept_id
@@ -429,8 +429,8 @@ module MalawiHivProgramReports
429
429
  def latest_vl_orders(patient_list)
430
430
  ActiveRecord::Base.connection.select_all <<~SQL
431
431
  SELECT odr.patient_id, MAX(start_date) AS order_date
432
- FROM obs o
433
- INNER JOIN orders odr ON odr.order_id = o.order_id AND odr.voided = 0 AND DATE(odr.start_date) <= '#{@end_date}'
432
+ FROM obs #{current_partition} o
433
+ INNER JOIN orders #{current_partition} odr ON odr.order_id = o.order_id AND odr.voided = 0 AND DATE(odr.start_date) <= '#{@end_date}'
434
434
  WHERE o.concept_id = #{::ConceptName.find_by_name('Test Type').concept_id}
435
435
  AND o.value_coded = #{::ConceptName.find_by_name('HIV viral load').concept_id}
436
436
  AND o.voided = 0
@@ -444,11 +444,11 @@ module MalawiHivProgramReports
444
444
  SELECT o.person_id AS patient_id,
445
445
  o.obs_datetime AS result_date,
446
446
  CONCAT (COALESCE(o.value_modifier, '='),' ',COALESCE(o.value_numeric, o.value_text, '')) AS result
447
- FROM obs o
447
+ FROM obs #{current_partition} o
448
448
  INNER JOIN (
449
449
  SELECT MAX(obs_datetime) AS obs_datetime, person_id
450
- FROM obs co
451
- INNER JOIN orders odr ON odr.order_id = co.order_id AND odr.voided = 0
450
+ FROM obs #{current_partition} co
451
+ INNER JOIN orders #{current_partition} odr ON odr.order_id = co.order_id AND odr.voided = 0
452
452
  WHERE co.concept_id = #{::ConceptName.find_by_name('HIV viral load').concept_id}
453
453
  AND co.voided = 0
454
454
  AND co.obs_datetime <= '#{@end_date}'
@@ -456,7 +456,7 @@ module MalawiHivProgramReports
456
456
  AND #{in_manager(column: 'co.person_id', values: patient_list)}
457
457
  GROUP BY co.person_id
458
458
  ) AS latest_vl ON latest_vl.obs_datetime = o.obs_datetime AND latest_vl.person_id = o.person_id
459
- INNER JOIN orders odr ON odr.order_id = o.order_id AND odr.voided = 0
459
+ INNER JOIN orders #{current_partition} odr ON odr.order_id = o.order_id AND odr.voided = 0
460
460
  WHERE o.concept_id = #{::ConceptName.find_by_name('HIV viral load').concept_id}
461
461
  AND o.voided = 0 AND o.obs_datetime <= '#{@end_date}'
462
462
  AND (o.value_numeric IS NOT NULL || o.value_text IS NOT NULL)
@@ -138,17 +138,17 @@ module MalawiHivProgramReports
138
138
  orders.order_id, orders.start_date, drug_order.quantity, drug.name,
139
139
  orders.patient_id, obs.value_numeric, orders.start_date,
140
140
  patient_identifier.identifier,drug.drug_id
141
- FROM orders
142
- INNER JOIN drug_order ON drug_order.order_id = orders.order_id AND drug_order.quantity > 0
143
- INNER JOIN arv_drug ON arv_drug.drug_id = drug_order.drug_inventory_id
144
- INNER JOIN drug ON drug.drug_id = arv_drug.drug_id
145
- INNER JOIN encounter ON encounter.encounter_id = orders.encounter_id
141
+ FROM orders #{current_partition}
142
+ INNER JOIN drug_order #{current_partition} ON drug_order.order_id = orders.order_id AND drug_order.quantity > 0
143
+ INNER JOIN arv_drug #{current_partition} ON arv_drug.drug_id = drug_order.drug_inventory_id
144
+ INNER JOIN drug #{current_partition} ON drug.drug_id = arv_drug.drug_id
145
+ INNER JOIN encounter #{current_partition} ON encounter.encounter_id = orders.encounter_id
146
146
  AND encounter.program_id = #{::Program.find_by(name: 'HIV PROGRAM').id}
147
147
  #{site_manager(operator: 'AND', column: 'encounter.site_id', location: @location)}
148
- INNER JOIN obs ON obs.order_id = orders.order_id AND obs.voided = 0
148
+ INNER JOIN obs #{current_partition} ON obs.order_id = orders.order_id AND obs.voided = 0
149
149
  AND obs.concept_id = #{amount_dispensed} AND obs.value_numeric > 0
150
150
  #{site_manager(operator: 'AND', column: 'obs.site_id', location: @location)}
151
- LEFT JOIN patient_identifier ON patient_identifier.patient_id = orders.patient_id
151
+ LEFT JOIN patient_identifier #{current_partition} ON patient_identifier.patient_id = orders.patient_id
152
152
  AND patient_identifier.identifier_type = #{identifier_type}
153
153
  AND patient_identifier.voided = 0
154
154
  #{site_manager(operator: 'AND', column: 'patient_identifier.site_id', location: @location)}
@@ -85,11 +85,11 @@ module MalawiHivProgramReports
85
85
  start_date = ActiveRecord::Base.connection.select_one <<-SQL
86
86
  SELECT
87
87
  DATE(MIN(o.start_date)) date
88
- FROM person p
89
- INNER JOIN orders o ON o.patient_id = p.person_id
90
- INNER JOIN drug_order t ON o.order_id = t.order_id
91
- INNER JOIN drug d ON d.drug_id = t.drug_inventory_id
92
- INNER JOIN encounter e ON e.patient_id = o.patient_id AND e.program_id = 1
88
+ FROM person #{current_partition} p
89
+ INNER JOIN orders #{current_partition} o ON o.patient_id = p.person_id
90
+ INNER JOIN drug_order #{current_partition} t ON o.order_id = t.order_id
91
+ INNER JOIN drug #{current_partition} d ON d.drug_id = t.drug_inventory_id
92
+ INNER JOIN encounter #{current_partition} e ON e.patient_id = o.patient_id AND e.program_id = 1
93
93
  WHERE o.voided = 0 AND o.patient_id = #{patient_id}
94
94
  AND d.concept_id = 656 AND t.quantity > 0;
95
95
  SQL
@@ -146,11 +146,11 @@ module MalawiHivProgramReports
146
146
  SELECT
147
147
  o.patient_id, t.drug_inventory_id, t.quantity,
148
148
  o.start_date, o.auto_expire_date
149
- FROM person p
150
- INNER JOIN orders o ON o.patient_id = p.person_id
151
- INNER JOIN drug_order t ON o.order_id = t.order_id
152
- INNER JOIN drug d ON d.drug_id = t.drug_inventory_id
153
- INNER JOIN encounter e ON e.patient_id = o.patient_id AND e.program_id = 1
149
+ FROM person #{current_partition} p
150
+ INNER JOIN orders #{current_partition} o ON o.patient_id = p.person_id
151
+ INNER JOIN drug_order #{current_partition} t ON o.order_id = t.order_id
152
+ INNER JOIN drug #{current_partition} d ON d.drug_id = t.drug_inventory_id
153
+ INNER JOIN encounter #{current_partition} e ON e.patient_id = o.patient_id AND e.program_id = 1
154
154
  WHERE o.voided = 0 AND (o.start_date
155
155
  BETWEEN '#{initiation_start_date}' AND '#{@completion_end_date}')
156
156
  AND d.concept_id = 656 AND t.quantity > 0