Class: MalawiHivProgramReports::Cohort::SideEffects

Inherits:
Object
  • Object
show all
Includes:
Adapters::Moh::Custom, Utils::ModelUtils
Defined in:
app/services/malawi_hiv_program_reports/cohort/side_effects.rb

Instance Method Summary collapse

Methods included from Adapters::Moh::Custom

#cast_manager, #current_partition, #exe_create_drill_down_table, #exe_temp_cohort_members_table, #exe_temp_order_details_table, #exe_temp_other_patient_types, #exe_temp_register_start_date_table, #exe_tmp_patient_table, #function_manager, #group_by_columns, #in_manager, #interval_manager, #min_filt, #site_manager, #timestampdiff_manager

Methods included from Utils::ModelUtils

#concept, #concept_id_to_name, #concept_name, #concept_name_to_id, #drug, #encounter_type, #global_property, #order_type, #patient_identifier_type, #program, #report_type, #user_property

Instance Method Details

#art_side_effectsObject



136
137
138
# File 'app/services/malawi_hiv_program_reports/cohort/side_effects.rb', line 136

def art_side_effects
  @art_side_effects ||= concept_name('Malawi ART Side Effects')
end

#initialize_tableObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/services/malawi_hiv_program_reports/cohort/side_effects.rb', line 19

def initialize_table
  ActiveRecord::Base.connection.execute <<~SQL
    DROP TABLE IF EXISTS temp_patient_side_effects
  SQL

  ActiveRecord::Base.connection.execute <<~SQL
    CREATE TABLE temp_patient_side_effects (
      patient_id INT PRIMARY KEY,
      has_se VARCHAR(120) NOT NULL,
      site_id INT DEFAULT NULL
    )
  SQL
  ActiveRecord::Base.connection.execute 'CREATE INDEX tpse_site_id ON temp_patient_side_effects(site_id)'
  ActiveRecord::Base.connection.execute 'CREATE INDEX idx_se ON temp_patient_side_effects(has_se)'
  ActiveRecord::Base.connection.execute <<~SQL
    CREATE INDEX idx_side_effects ON temp_patient_side_effects (patient_id, has_se, site_id)
  SQL
end

#load_patients_missing_side_effects(date) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'app/services/malawi_hiv_program_reports/cohort/side_effects.rb', line 117

def load_patients_missing_side_effects(date)
  date = ActiveRecord::Base.connection.quote(date)

  ActiveRecord::Base.connection.execute <<~SQL
    INSERT INTO temp_patient_side_effects
    SELECT patient_id, 'Unknown', #{@location} FROM temp_earliest_start_date
    WHERE date_enrolled <= #{date} #{site_manager(operator: 'AND', column: 'site_id', location: @location)}
      AND patient_id NOT IN (SELECT patient_id FROM temp_patient_side_effects #{site_manager(operator: 'WHERE', column: 'site_id', location: @location)})
  SQL
end

#load_patients_with_side_effects(date) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/services/malawi_hiv_program_reports/cohort/side_effects.rb', line 38

def load_patients_with_side_effects(date)
  date = ActiveRecord::Base.connection.quote(date)

  ActiveRecord::Base.connection.execute <<~SQL
    INSERT INTO temp_patient_side_effects(patient_id, has_se, site_id)
    SELECT patients.patient_id, 'Yes', #{@location}
    FROM temp_earliest_start_date AS patients
    INNER JOIN temp_patient_outcomes
      ON temp_patient_outcomes.patient_id = patients.patient_id
      AND LOWER(temp_patient_outcomes.cum_outcome) = LOWER('On antiretrovirals') #{site_manager(operator: 'AND', column: 'temp_patient_outcomes.site_id', location: @location)}
    INNER JOIN obs AS side_effects_group
      ON side_effects_group.person_id = patients.patient_id
      AND side_effects_group.concept_id = #{art_side_effects.concept_id}
      AND side_effects_group.voided = 0 #{site_manager(operator: 'AND', column: 'side_effects_group.site_id', location: @location)}
    /* Limit check to last visit before #{date} */
    INNER JOIN (
      SELECT person_id, MAX(obs_datetime) AS obs_datetime FROM obs
      WHERE concept_id = #{art_side_effects.concept_id}
        /* Side effects on initial visit are treated as contra-indications */
        AND obs_datetime < #{interval_manager(date:, value: 1, interval: 'DAY', operator: '+')}
        AND voided = 0 #{site_manager(operator: 'AND', column: 'obs.site_id', location: @location)}
        AND person_id IN (SELECT patient_id FROM temp_patient_outcomes WHERE LOWER(cum_outcome) = LOWER('On antiretrovirals') #{site_manager(operator: 'AND', column: 'site_id', location: @location)})
      GROUP BY person_id
    ) AS last_visit
      ON last_visit.person_id = side_effects_group.person_id
      AND last_visit.obs_datetime = side_effects_group.obs_datetime
      AND last_visit.obs_datetime >= #{interval_manager(date: 'patients.date_enrolled', value: 1, interval: 'DAY', operator: '+')}
    INNER JOIN obs AS side_effects
      ON side_effects.person_id = patients.patient_id
      AND side_effects_group.obs_id = side_effects.obs_group_id
      AND side_effects.value_coded = #{yes.concept_id}
      AND side_effects.voided = 0 #{site_manager(operator: 'AND', column: 'side_effects.site_id', location: @location)}
    WHERE patients.date_enrolled <= #{date} #{site_manager(operator: 'AND', column: 'patients.site_id', location: @location)}
    GROUP BY patients.patient_id
  SQL
end

#load_patients_without_side_effects(date) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/services/malawi_hiv_program_reports/cohort/side_effects.rb', line 75

def load_patients_without_side_effects(date)
  date = ActiveRecord::Base.connection.quote(date)

  ActiveRecord::Base.connection.execute <<~SQL
    INSERT INTO temp_patient_side_effects
    SELECT patients.patient_id, 'No', #{@location}
    FROM temp_earliest_start_date AS patients
    INNER JOIN temp_patient_outcomes
      ON temp_patient_outcomes.patient_id = patients.patient_id
      AND LOWER(temp_patient_outcomes.cum_outcome) = LOWER('On antiretrovirals') #{site_manager(operator: 'AND', column: 'temp_patient_outcomes.site_id', location: @location)}
    INNER JOIN obs AS side_effects_group
      ON side_effects_group.person_id = patients.patient_id
      AND side_effects_group.concept_id = #{art_side_effects.concept_id}
      AND side_effects_group.voided = 0 #{site_manager(operator: 'AND', column: 'side_effects_group.site_id', location: @location)}
    /* Limit check to last visit before #{date} */
    INNER JOIN (
      SELECT person_id, MAX(obs_datetime) AS obs_datetime FROM obs
      WHERE concept_id = #{art_side_effects.concept_id}
        /* Side effects on initial visit are treated as contra-indications */
        AND obs_datetime < #{interval_manager(date:, value: 1, interval: 'DAY', operator: '+')}
        AND voided = 0 #{site_manager(operator: 'AND', column: 'site_id', location: @location)}
        AND person_id IN (
          SELECT patient_id FROM temp_patient_outcomes WHERE LOWER(cum_outcome) = LOWER('On antiretrovirals') #{site_manager(operator: 'AND', column: 'site_id', location: @location)}
        )
      GROUP BY person_id
    ) AS last_visit
      ON last_visit.person_id = side_effects_group.person_id
      AND last_visit.obs_datetime = side_effects_group.obs_datetime
      AND last_visit.obs_datetime >= #{interval_manager(date: 'patients.date_enrolled', value: 1, interval: 'DAY', operator: '+')}
    INNER JOIN obs AS side_effects
      ON side_effects.person_id = patients.patient_id
      AND side_effects_group.obs_id = side_effects.obs_group_id
      AND side_effects.value_coded = #{no.concept_id}
      AND side_effects.voided = 0 #{site_manager(operator: 'AND', column: 'side_effects.site_id', location: @location)}
    WHERE patients.date_enrolled <= #{date} #{site_manager(operator: 'AND', column: 'patients.site_id', location: @location)}
      AND patients.patient_id NOT IN (
        SELECT patient_id FROM temp_patient_side_effects WHERE has_se = 'Yes' #{site_manager(operator: 'AND', column: 'site_id', location: @location)}
      )
    GROUP BY patients.patient_id
  SQL
end

#noObject



132
133
134
# File 'app/services/malawi_hiv_program_reports/cohort/side_effects.rb', line 132

def no
  @no ||= concept_name('No')
end

#update_side_effects(date, location) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'app/services/malawi_hiv_program_reports/cohort/side_effects.rb', line 9

def update_side_effects(date, location)
  @location = location
  @adapter = ActiveRecord::Base.connection.adapter_name.downcase
  initialize_table

  load_patients_with_side_effects(date)
  load_patients_without_side_effects(date)
  load_patients_missing_side_effects(date)
end

#yesObject



128
129
130
# File 'app/services/malawi_hiv_program_reports/cohort/side_effects.rb', line 128

def yes
  @yes ||= concept_name('Yes')
end