11require "faker"
22require "yaml"
33
4+ logfile = Rails . root . join ( "log" , "#{ Rails . env } .log" )
5+ logger = ActiveSupport ::Logger . new ( logfile )
6+ logger . extend ( ActiveSupport ::Logger . broadcast ( ActiveSupport ::Logger . new ( $stdout) ) )
7+
48namespace :opensrp do
59 desc "Export simple patient-related data as opensrp fhir resources"
610 task :export , [ :config_file , :output_file ] => :environment do |_task , args |
@@ -14,36 +18,42 @@ namespace :opensrp do
1418 raise "Output file should be JSON" unless output_file . split ( "." ) . last == "json"
1519 config = YAML . load_file ( config_file )
1620
21+ logger . info "Exporting data using config at #{ config_file } "
22+
1723 report_start = DateTime . parse ( "2020-01-01" )
1824 report_end = DateTime . now
1925 facilities_to_export = config [ "facilities" ]
2026 time_boundaries = config [ "time_boundaries" ]
2127 using_time_boundaries = using_time_boundaries? config
2228 report_start = DateTime . parse ( time_boundaries [ "report_start" ] ) if has_report_start? ( config )
2329 report_end = DateTime . parse ( time_boundaries [ "report_end" ] ) if has_report_end? ( config )
24- time_window = report_start ..report_end
30+
31+ logger . info "Time Boundaries: [#{ report_start } ..#{ report_end } ]"
2532
2633 resources = [ ]
2734 encounters = [ ]
2835 patients = Patient . where ( assigned_facility_id : facilities_to_export . keys )
36+
37+ logger . info "Preparing data for #{ patients . size } patients"
2938 patients . each do |patient |
39+ logger . debug "Preparing data for Patient[##{ patient . id } ]"
3040 patient_exporter = OneOff ::Opensrp ::PatientExporter . new ( patient , facilities_to_export )
3141 resources << patient_exporter . export
3242 resources << patient_exporter . export_registration_questionnaire_response
3343 encounters << patient_exporter . export_registration_encounter
3444
3545 blood_pressures = patient . blood_pressures
3646 blood_pressures = blood_pressures . where ( recorded_at : time_window ) . or ( updated_at : time_window ) if using_time_boundaries
47+ logger . debug "Patient[##{ patient . id } ] has #{ blood_pressures . size } blood pressure readings."
3748 blood_pressures . each do |bp |
38- bp_exporter = OneOff ::Opensrp ::BloodPressureExporter . new ( bp , facilities_to_export )
3949 resources << bp_exporter . export
4050 encounters << bp_exporter . export_encounter
4151 end
4252
4353 blood_sugars = patient . blood_sugars
4454 blood_sugars = blood_sugars . where ( recorded_at : time_window ) . or ( updated_at : time_window ) if using_time_boundaries
55+ logger . debug "Patient[##{ patient . id } ] has #{ blood_sugars . size } blood sugar readings."
4556 blood_sugars . each do |bp |
46- bs_exporter = OneOff ::Opensrp ::BloodSugarExporter . new ( bs , facilities_to_export )
4757 if patient . medical_history . diabetes_no?
4858 resources << bs_exporter . export_no_diabetes_observation
4959 end
@@ -53,8 +63,8 @@ namespace :opensrp do
5363
5464 prescription_drugs = patient . prescription_drugs
5565 prescription_drugs = prescription_drugs . where ( created_at : time_window ) . or ( updated_at : time_window ) if using_time_boundaries
66+ logger . debug "Patient[##{ patient . id } ] has #{ prescription_drugs . size } drugs prescribed."
5667 prescription_drugs . each do |bp |
57- drug_exporter = OneOff ::Opensrp ::PrescriptionDrugExporter . new ( drug , facilities_to_export )
5868 resources << drug_exporter . export_dosage_flag
5969 encounters << drug_exporter . export_encounter
6070 end
@@ -66,8 +76,8 @@ namespace :opensrp do
6676
6777 appointments = patient . appointments
6878 appointments = appointments . where ( created_at : time_window ) . or ( updated_at : time_window ) if using_time_boundaries
79+ logger . debug "Patient[##{ patient . id } ] has #{ appointments . size } appointments."
6980 appointments . each do |bp |
70- next unless appointment . status_scheduled?
7181 appointment_exporter = OneOff ::Opensrp ::AppointmentExporter . new ( appointment , facilities_to_export )
7282 resources << appointment_exporter . export
7383 if appointment . call_results . present?
0 commit comments