Skip to content

Commit cad5b45

Browse files
authored
fix(OpenSRP Exporter): Patient Export, Syntax, et al (#5536)
**Story card:** [sc-14814](https://app.shortcut.com/simpledotorg/story/14814/opensrp-exports-export-a-specific-time-window) ## Because Running the exporter failed for a few reasons ## This addresses All the reason the exporter fails per commit - YAML parses dates directly into datetime - declare `time_window` in local functions - change hash to active_record#relation + add exporters - use `time_window` definition in helper function - ensure config data fits the `meta` function call - return original keys; code is dependent on them - only add the patient encounter if recorded within `time_window` ## Test instructions - build a new image - run an export - ensure all `lastUpdatedBy` for resource types which are not `Condition` happen within the time window specified
1 parent 5593326 commit cad5b45

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

app/services/one_off/opensrp/encounter_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def generate
1919
service_provider = first_child_encounter.serviceProvider
2020
child_encounters.pluck(:child_encounter).append(
2121
FHIR::Encounter.new(
22-
meta: meta(opensrp_ids),
22+
meta: meta(opensrp_ids.symbolize_keys),
2323
status: "finished",
2424
id: parent_id,
2525
identifier: [

config/opensrp-export.sample.yml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@ time_boundaries:
44
facilities:
55
d1dbd3c6-26bb-48e7-aa89-bc8a0b2bf75b:
66
name: Health Facility 1
7-
opensrp_practitioner_id: 0c375fe8-b38f-484e-aa64-c02750ee183b
8-
opensrp_organization_id: d3363aea-66ad-4370-809a-8e4436a4218f
9-
opensrp_care_team_id: 1c8100b5-222b-4815-ba4d-3ebde537c6ce
10-
opensrp_location_id: ABC01230123
11-
b100eb1a-18b3-424f-a2bf-c2e6b1655b1b:
12-
name: Health Facility 2
13-
opensrp_practitioner_id: 34a9da76-e18c-4fd6-a696-dee7a8454fb4
14-
opensrp_organization_id: 5466dfd9-b10b-4bd8-8692-5732340de7f1
15-
opensrp_care_team_id: f91c89c9-6790-4a94-b877-5feb578e0af8
16-
opensrp_location_id: DEF01230123
7+
practitioner_id: 0c375fe8-b38f-484e-aa64-c02750ee183b
8+
organization_id: d3363aea-66ad-4370-809a-8e4436a4218f
9+
care_team_id: 1c8100b5-222b-4815-ba4d-3ebde537c6ce
10+
location_id: ABC01230123

lib/tasks/opensrp.rake

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ namespace :opensrp do
2525
facilities_to_export = config["facilities"]
2626
time_boundaries = config["time_boundaries"]
2727
using_time_boundaries = using_time_boundaries? config
28-
report_start = DateTime.parse(time_boundaries["report_start"]) if has_report_start?(config)
29-
report_end = DateTime.parse(time_boundaries["report_end"]) if has_report_end?(config)
28+
report_start = time_boundaries["report_start"] if has_report_start?(config)
29+
report_end = time_boundaries["report_end"] if has_report_end?(config)
30+
time_window = report_start..report_end
3031

3132
logger.info "Time Boundaries: [#{report_start}..#{report_end}]"
3233

@@ -37,23 +38,27 @@ namespace :opensrp do
3738
logger.info "Preparing data for #{patients.size} patients"
3839
patients.each do |patient|
3940
logger.debug "Preparing data for Patient[##{patient.id}]"
40-
patient_exporter = OneOff::Opensrp::PatientExporter.new(patient, facilities_to_export)
41-
resources << patient_exporter.export
42-
resources << patient_exporter.export_registration_questionnaire_response
43-
encounters << patient_exporter.export_registration_encounter
41+
if time_window.cover?(patient.recorded_at)
42+
patient_exporter = OneOff::Opensrp::PatientExporter.new(patient, facilities_to_export)
43+
resources << patient_exporter.export
44+
resources << patient_exporter.export_registration_questionnaire_response
45+
encounters << patient_exporter.export_registration_encounter
46+
end
4447

4548
blood_pressures = patient.blood_pressures
46-
blood_pressures = blood_pressures.where(recorded_at: time_window).or(updated_at: time_window) if using_time_boundaries
49+
blood_pressures = blood_pressures.where(recorded_at: time_window).or(blood_pressures.where(updated_at: time_window)) if using_time_boundaries
4750
logger.debug "Patient[##{patient.id}] has #{blood_pressures.size} blood pressure readings."
4851
blood_pressures.each do |bp|
52+
bp_exporter = OneOff::Opensrp::BloodPressureExporter.new(bp, facilities_to_export)
4953
resources << bp_exporter.export
5054
encounters << bp_exporter.export_encounter
5155
end
5256

5357
blood_sugars = patient.blood_sugars
54-
blood_sugars = blood_sugars.where(recorded_at: time_window).or(updated_at: time_window) if using_time_boundaries
58+
blood_sugars = blood_sugars.where(recorded_at: time_window).or(blood_sugars.where(updated_at: time_window)) if using_time_boundaries
5559
logger.debug "Patient[##{patient.id}] has #{blood_sugars.size} blood sugar readings."
56-
blood_sugars.each do |bp|
60+
blood_sugars.each do |bs|
61+
bs_exporter = OneOff::Opensrp::BloodSugarExporter.new(bs, facilities_to_export)
5762
if patient.medical_history.diabetes_no?
5863
resources << bs_exporter.export_no_diabetes_observation
5964
end
@@ -62,9 +67,10 @@ namespace :opensrp do
6267
end
6368

6469
prescription_drugs = patient.prescription_drugs
65-
prescription_drugs = prescription_drugs.where(created_at: time_window).or(updated_at: time_window) if using_time_boundaries
70+
prescription_drugs = prescription_drugs.where(created_at: time_window).or(prescription_drugs.where(updated_at: time_window)) if using_time_boundaries
6671
logger.debug "Patient[##{patient.id}] has #{prescription_drugs.size} drugs prescribed."
67-
prescription_drugs.each do |bp|
72+
prescription_drugs.each do |drug|
73+
drug_exporter = OneOff::Opensrp::PrescriptionDrugExporter.new(drug, facilities_to_export)
6874
resources << drug_exporter.export_dosage_flag
6975
encounters << drug_exporter.export_encounter
7076
end
@@ -75,9 +81,9 @@ namespace :opensrp do
7581
end
7682

7783
appointments = patient.appointments
78-
appointments = appointments.where(created_at: time_window).or(updated_at: time_window) if using_time_boundaries
84+
appointments = appointments.where(created_at: time_window).or(appointments.where(updated_at: time_window)) if using_time_boundaries
7985
logger.debug "Patient[##{patient.id}] has #{appointments.size} appointments."
80-
appointments.each do |bp|
86+
appointments.each do |appointment|
8187
appointment_exporter = OneOff::Opensrp::AppointmentExporter.new(appointment, facilities_to_export)
8288
resources << appointment_exporter.export
8389
if appointment.call_results.present?
@@ -109,11 +115,11 @@ namespace :opensrp do
109115
end
110116

111117
def has_report_start?(config)
112-
using_time_boundaries?(config) && time_boundaries.has_key?("report_start")
118+
using_time_boundaries?(config) && config["time_boundaries"].has_key?("report_start")
113119
end
114120

115121
def has_report_end?(config)
116-
using_time_boundaries?(config) && time_boundaries.has_key?("report_end")
122+
using_time_boundaries?(config) && config["time_boundaries"].has_key?("report_end")
117123
end
118124

119125
def create_audit_record(facilities, patient)

0 commit comments

Comments
 (0)