Module: EIRReportInterface::EIRReportInterfaceService

Defined in:
app/services/eir_report_interface/eir_report_interface.rb

Overview

EIRReportInterfaceService is a module within EIRReportInterface that provides various service methods for reporting and data disaggregation.

Class Method Summary collapse

Class Method Details

.add_initial_week(weeks, first_day) ⇒ Object

Adds the initial week to the weeks hash.

Parameters:

  • weeks (Hash)

    The hash to add the week to

  • first_day (Date)

    The first day of the initial week



112
113
114
115
116
117
118
119
# File 'app/services/eir_report_interface/eir_report_interface.rb', line 112

def add_initial_week(weeks, first_day)
  wk_of_first_day = first_day.cweek
  return unless wk_of_first_day > 1

  wk = "#{first_day.prev_year.year}W#{wk_of_first_day}"
  dates = "#{first_day - first_day.wday + 1} to #{first_day - first_day.wday + 1 + 6}"
  weeks[wk] = dates
end

.add_week(weeks, first_monday) ⇒ Object

Adds a week to the weeks hash.

Parameters:

  • weeks (Hash)

    The hash to add the week to

  • first_monday (Date)

    The first Monday of the week



124
125
126
127
128
# File 'app/services/eir_report_interface/eir_report_interface.rb', line 124

def add_week(weeks, first_monday)
  wk = "#{first_monday.year}W#{first_monday.cweek}"
  dates = "#{first_monday} to #{first_monday + 6}"
  weeks[wk] = dates
end

.calculate_age(dob) ⇒ Integer

Calculates the age based on the date of birth.

Parameters:

  • dob (Date)

    The date of birth

Returns:

  • (Integer)

    The calculated age



133
134
135
# File 'app/services/eir_report_interface/eir_report_interface.rb', line 133

def calculate_age(dob)
  ((Date.today - dob.to_date).to_i / 365).rescue(0)
end

.disaggregate(disaggregate_key, concept_ids, start_date, end_date, type) ⇒ Hash

Disaggregates data based on the provided criteria.

Parameters:

  • disaggregate_key (String)

    The key for disaggregation (e.g., ‘less’, ‘greater’)

  • concept_ids (Array<Integer>)

    The concept IDs to filter by

  • start_date (Date)

    The start date for the date range

  • end_date (Date)

    The end date for the date range

  • type (EncounterType)

    The encounter type

Returns:

  • (Hash)

    A hash containing the disaggregated data



34
35
36
37
# File 'app/services/eir_report_interface/eir_report_interface.rb', line 34

def disaggregate(disaggregate_key, concept_ids, start_date, end_date, type)
  data = fetch_data(concept_ids, start_date, end_date, type)
  { 'ids' => filter_data(data, disaggregate_key) }
end

.fetch_data(concept_ids, start_date, end_date, type) ⇒ ActiveRecord::Relation

Fetches data from the database based on the provided criteria.

Parameters:

  • concept_ids (Array<Integer>)

    The concept IDs to filter by

  • start_date (Date)

    The start date for the date range

  • end_date (Date)

    The end date for the date range

  • type (EncounterType)

    The encounter type

Returns:

  • (ActiveRecord::Relation)

    The fetched data



45
46
47
48
49
50
51
52
53
54
# File 'app/services/eir_report_interface/eir_report_interface.rb', line 45

def fetch_data(concept_ids, start_date, end_date, type)
  Encounter.where(
    'encounter_datetime BETWEEN ? AND ? AND encounter_type = ? AND value_coded IN (?) AND concept_id IN (6543, 6542)',
    start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
    end_date.to_date.strftime('%Y-%m-%d 23:59:59'), type.id, concept_ids
  ).joins(
    'INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
     INNER JOIN person p ON p.person_id = encounter.patient_id'
  ).select('encounter.encounter_type, obs.value_coded, p.*')
end

.filter_data(data, disaggregate_key) ⇒ Array<Integer>

Filters data based on the disaggregate key.

Parameters:

  • data (ActiveRecord::Relation)

    The data to filter

  • disaggregate_key (String)

    The key for disaggregation (e.g., ‘less’, ‘greater’)

Returns:

  • (Array<Integer>)

    The filtered IDs



60
61
62
63
64
65
66
67
68
69
70
# File 'app/services/eir_report_interface/eir_report_interface.rb', line 60

def filter_data(data, disaggregate_key)
  filtered_data = case disaggregate_key
                  when 'less'
                    data.select { |record| calculate_age(record['birthdate']) < 5 }
                  when 'greater'
                    data.select { |record| calculate_age(record['birthdate']) >= 5 }
                  else
                    []
                  end
  filtered_data.collect { |record| record['person_id'] }
end

.months_generatorArray<Array<String>>

Generates a hash of months with their respective date ranges.

Returns:

  • (Array<Array<String>>)

    An array of month-date range pairs



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/services/eir_report_interface/eir_report_interface.rb', line 74

def months_generator
  months = {}
  count = 1
  curr_date = Date.today

  while count < 13
    curr_date -= 1.month
    months[curr_date.strftime('%Y%m')] = [
      curr_date.strftime('%B-%Y'),
      "#{curr_date.beginning_of_month} to #{curr_date.end_of_month}"
    ]
    count += 1
  end

  months.to_a
end

.server_configHash

Reads and parses server configuration from a YAML file.

Returns:

  • (Hash)

    Parsed YAML configuration



23
24
25
# File 'app/services/eir_report_interface/eir_report_interface.rb', line 23

def server_config
  YAML.load_file("#{Rails.root}/config/application.yml")
end

.settingsHash

Reads and parses settings from a JSON file.

Returns:

  • (Hash)

    Parsed JSON configuration



16
17
18
19
# File 'app/services/eir_report_interface/eir_report_interface.rb', line 16

def settings
  file = File.read(Rails.root.join('db', 'idsr_metadata', 'idsr_ohsp_settings.json'))
  JSON.parse(file)
end

.weeks_generatorArray<Array<String>>

Generates a hash of weeks with their respective date ranges.

Returns:

  • (Array<Array<String>>)

    An array of week-date range pairs



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/services/eir_report_interface/eir_report_interface.rb', line 93

def weeks_generator
  weeks = {}
  first_day = (Date.today - 11.months).at_beginning_of_month
  add_initial_week(weeks, first_day)

  first_monday = first_day.next_week(:monday)

  while first_monday <= Date.today
    add_week(weeks, first_monday)
    first_monday += 7
  end

  this_wk = "#{Date.today.year}W#{Date.today.cweek}"
  weeks.reject { |key, _| key == this_wk }.to_a
end