Skip to content

Instantly share code, notes, and snippets.

@jrichter
Created December 4, 2008 16:03
Show Gist options
  • Save jrichter/31974 to your computer and use it in GitHub Desktop.
Save jrichter/31974 to your computer and use it in GitHub Desktop.
def self.read_files_and_create_reports
@filenames = []
pids = []
dir = "public/txt_reports"
Dir.foreach(dir) do |entry|
if entry.include? ".txt"
@filenames << entry
end
end
do_work_concurrently(@filenames, dir, 2)
puts "Done Working"
end
def self.do_work_concurrently(files, dir, number_of_workers = 5)
config = ActiveRecord::Base.remove_connection
max = number_of_workers
pids = []
any_work = true
while any_work do
filename = files.first
files.delete filename
if files.length != 0
any_work = true
else
any_work = false
end
pids << fork_with_new_connection(config) {
File.open("#{dir}/#{filename}") do |file|
if filename[0..9] < "2007-02-01"
file.gets("CONFIDENTIAL")
else
file.gets("RADIOLOGY")
end
body = file.read
body = body.gsub(/[­]*/,"")#There is an invisible character between the brackets that causes mysql to truncate everything after it.
@report = Report.new(:name => filename, :body => body)
@report.save
puts @report.name + filename[0..9]
update_report(@report)
associate_report(@report)
end
}
while pids.length >= max
pids.each do |pid|
puts "waiting on #{pid} - inside work loop"
puts "#{pid.inspect}"
Process.waitpid(pid)
if $?.exited? == true
pids.delete pid
end
end
end
end
while pids.length != 0
pids.each do |pid|
puts "waiting on #{pid} - outside work loop"
Process.wait(pid)
if $?.exited? == true
pids.delete pid
end
end
end
ActiveRecord::Base.establish_connection(config)
end
def self.fork_with_new_connection(config, klass = ActiveRecord::Base)
fork do
begin
klass.establish_connection(config)
yield
ensure
klass.remove_connection
end
end
end
def self.update_report(report)
parse_text(report)
if @gen == "first"
report.update_attributes(:patient_name => @patient_name,
:dop => @dop,
:account_num => @account_num,
:ref_phy => @ref_phy,
:dic_phy => @dic_phy,
:generation => @gen)
logger.info("#{@gen.humanize} generation report was created for #{report.name}.")
elsif @gen == "second"
report.update_attributes(:patient_name => @patient_name,
:dop => @dop,
:account_num => @account_num,
:ref_phy => @ref_phy,
:dic_phy => @dic_phy,
:generation => @gen,
:procedure => @procedure)
logger.info("#{@gen.humanize} generation report was created for #{report.name}.")
elsif @gen == "third"
report.update_attributes(:patient_name => @patient_name,
:dop => @dop,
:account_num => @account_num,
:ref_phy => @ref_phy,
:dic_phy => @dic_phy,
:generation => @gen,
:procedure => @procedure,
:case_num => @case_num,
:dictated => @dictated,
:transcribed => @transcribed,
:elapsed_time => @elapsed_time,
:dic_trans_time => @dic_trans_time)
logger.info("#{@gen.humanize} generation report was created for #{report.name}.")
else
logger.info("#{report.name} did not update.")
end
end
def self.associate_report(report, study=nil)
puts "associating report."
person = Person.find_by_hosp_id(report.account_num)
if person
visit = person.visits.find(:first, :conditions => {:start_date => report.dop})
end
if study != nil
@@study = Study.find(study)
end
if person and visit
patient = person.patient
if report.generation == "first"
report.update_attributes(:patient_id => patient.id,
:visit_id => visit.id)
elsif report.generation == "second"
if study == nil
add_study_to_report(report,visit)
report.update_attributes(:patient_id => patient.id,
:visit_id => visit.id)
else
report.update_attributes(:patient_id => patient.id,
:visit_id => visit.id)
@@study.update_attributes(:report_id => report.id)
puts "report and study associated"
end
elsif report.generation == "third"
if study == nil
add_study_to_report(report,visit)
report.update_attributes(:patient_id => patient.id,
:visit_id => visit.id)
else
report.update_attributes(:patient_id => patient.id,
:visit_id => visit.id)
@@study.update_attributes(:report_id => report.id)
puts "report and study associated"
end
else
logger.info("#{report.name} did not associate.")
end
else
if report.generation != "first"
create_order_with_report(report)
end
end
end
def self.add_study_to_report(report,visit)
exam = which_exam?(report.procedure.downcase)
match = "no"
visit.studies.each do |study|
study_part = which_exam?(study.description.downcase)
resource = which_resource?(report)
if study_part == exam and study.modality.downcase == resource and study.start_date == report.dop
study.update_attributes(:report_id => report.id)
puts "study added to report."
match = "yes"
end
end
if match == "no"
create_order_with_report(report)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment