lib/echi-converter.rb in echi-converter-0.3.6 vs lib/echi-converter.rb in echi-converter-0.3.7

- old
+ new

@@ -35,11 +35,13 @@ begin ActiveRecord::Base.establish_connection(YAML::load(File.open(databaseconfig))) @log.info "Initialized the database" rescue => err @log.fatal "Could not connect to the database - " + err - send_email_alert "DATABASE" + if $config["send_email"] == true + send_email_alert "DATABASE" + end end end #Method to open our application log def initiate_logger @@ -59,10 +61,11 @@ end end #Method to send alert emails def send_email_alert reason + @log.debug "send_email_alert method" begin Net::SMTP.start($config["smtp_server"], $config["smtp_port"]) do |smtp| smtp.open_message_stream('donotreply@echi-converter.rubyforge.org', [$config["alert_email_address"]]) do |f| f.puts "From: donotreply@echi-converter.rubyforge.org" f.puts "To: " + $config['alert_email_address'] @@ -83,10 +86,11 @@ end #Set the working directory to copy processed files to, if it does not exist creat it #Directory names based on year/month so as not to exceed 5K files in a single directory def set_directory working_directory + @log.debug "set_directory method" time = Time.now directory_year = working_directory + "/../files/processed/" + time.year.to_s directory_month = directory_year + "/" + time.month.to_s if File.exists?(directory_month) == false @@ -99,10 +103,11 @@ return directory_month end #Method to get FTP files def get_ftp_files + @log.debug "get_ftp_files method" filelist_fetcher = FtpFetcher.new filequeue = filelist_fetcher.fetch_list @log if filequeue == nil return -1 @@ -138,16 +143,19 @@ @log.info "Using a single ftp session to fetch the files" fetcher = FtpFetcher.new result = fetcher.fetch_ftp_files filequeue, @log end if result == false - send_email_alert "FTP" + if $config["send_email"] == true + send_email_alert "FTP" + end end end #Method to write to the log table def log_processed_file type, filedata + @log.debug "log_processed file method" begin echi_log = EchiLog.new echi_log.filename = filedata["name"] if type == 'BINARY' echi_log.filenumber = filedata["number"] @@ -163,10 +171,11 @@ return 0 end #Method for parsing the various datatypes from the ECH file def dump_binary type, length + @log.debug "dump_binary method" case type when 'int' #Process integers, assigning appropriate profile based on length #such as long int, short int and tiny int. case length @@ -203,10 +212,11 @@ return value end #Mehtod that performs the conversions def convert_binary_file filename + @log.debug "convert_binary_file" #Open the file to process echi_file = $workingdir + "/../files/to_process/" + filename @binary_file = open(echi_file,"rb") @log.debug "File size: " + @binary_file.stat.size.to_s @@ -278,10 +288,11 @@ return @record_cnt end def process_ascii filename + @log.debug "process_ascii method" echi_file = $workingdir + "/../files/to_process/" + filename begin #Perform a transaction for each file, including the log table #in order to commit as one atomic action upon success @@ -332,21 +343,29 @@ return @record_cnt end def insert_dat_data tablename, row - + @log.debug "insert_dat_data method" begin case tablename + when "echi_acds" + echi_dat_record = EchiAcd.new when "echi_agents" echi_dat_record = EchiAgent.new when "echi_aux_reasons" echi_dat_record = EchiAuxReason.new when "echi_cwcs" echi_dat_record = EchiCwc.new + when "echi_splits" + echi_dat_record = EchiSplit.new + when "echi_trunk_groups" + echi_dat_record = EchiTrunkGroup.new when "echi_vdns" echi_dat_record = EchiVdn.new + when "echi_vectors" + echi_dat_record = EchiVector.new end cnt = 0 @echi_schema[tablename].each do | field | echi_dat_record[field["name"]] = row[cnt] cnt += 1 @@ -356,80 +375,118 @@ @log.info "Unable to insert " + tablename + " file record - " + err end end - #Process the appropriate table name - def process_proper_table file - @record_cnt = 0 - process_file = File.open(file["filename"]) - process_file.each do |row| - if row != nil - field = row.rstrip.split('|') - @log.debug '<====================START ' + file["name"] + ' RECORD ' + @record_cnt.to_s + ' ====================>' - case file["name"] - when "echi_agents" - record = EchiAgent.find(:first, :conditions => [ "login_id = ? AND group_id = ?", field[1], field[0]]) - when "echi_aux_reasons" - record = EchiAuxReason.find(:first, :conditions => [ "aux_reason = ? AND group_id = ?", field[1], field[0]]) - when "echi_cwcs" - record = EchiCwc.find(:first, :conditions => [ "cwc = ? AND group_id = ?", field[1], field[0]]) - when "echi_vdns" - record = EchiVdn.find(:first, :conditions => [ "vdn = ? AND group_id = ?", field[1], field[0]]) - end - if record != nil - if record.name != field[2] - record.name = field[2] - record.update - @record_cnt += 1 - @log.debug "Updated record - " + field.inspect - else - @log.debug "No update required for - " + field.inspect - end - else - insert_dat_data file["name"], field - @record_cnt += 1 - @log.debug "Inserted new record - " + field.inspect - end - end - @log.debug '<====================STOP ' + file["name"] + ' RECORD ' + @record_cnt.to_s + ' ====================>' - end - process_file.close - + #Move the file to the archive location + def archive_file file, record_cnt + @log.debug "archive_file method" case file["name"] + when "echi_acds" + filename_elements = $config["echi_acd_dat"].split(".") when "echi_agents" filename_elements = $config["echi_agent_dat"].split(".") when "echi_aux_reasons" filename_elements = $config["echi_aux_rsn_dat"].split(".") when "echi_cwcs" filename_elements = $config["echi_cwc_dat"].split(".") + when "echi_splits" + filename_elements = $config["echi_split_dat"].split(".") when "echi_vdns" filename_elements = $config["echi_vdn_dat"].split(".") + when "echi_trunk_groups" + filename_elements = $config["echi_trunk_group_dat"].split(".") + when "echi_vectors" + filename_elements = $config["echi_vector_dat"].split(".") end new_filename = filename_elements[0] + "_" + UUID.timestamp_create.to_s + "." + filename_elements[1] target_file = @processeddirectory + "/" + new_filename begin FileUtils.mv(file["filename"], target_file) if $config["echi_process_log"] == "Y" - log_processed_file nil, { "name" => new_filename, "cnt" => @record_cnt } + log_processed_file nil, { "name" => new_filename, "cnt" => record_cnt } end rescue => err @log.info "Unable to move processed file - " + err end + end + #Process the appropriate table name + def process_proper_table file + @log.debug "process_proper_table method" + @record_cnt = 0 + process_file = File.open(file["filename"]) + process_file.each do |row| + if row != nil + begin + field = row.rstrip.split('|') + @log.debug '<====================START ' + file["name"] + ' RECORD ' + @record_cnt.to_s + ' ====================>' + case file["name"] + when "echi_acds" + record = EchiAcd.find(:first, :conditions => [ "number = ? AND acd_id = ?", field[1], field[0]]) + when "echi_agents" + record = EchiAgent.find(:first, :conditions => [ "login_id = ? AND group_id = ?", field[1], field[0]]) + when "echi_aux_reasons" + record = EchiAuxReason.find(:first, :conditions => [ "aux_reason = ? AND group_id = ?", field[1], field[0]]) + when "echi_cwcs" + record = EchiCwc.find(:first, :conditions => [ "cwc = ? AND group_id = ?", field[1], field[0]]) + when "echi_splits" + record = EchiSplit.find(:first, :conditions => [ "number = ? AND acd_number = ?", field[1], field[0]]) + when "echi_trunk_groups" + record = EchiTrunkGroup.find(:first, :conditions => [ "trunk_group = ? AND acd_number = ?", field[1], field[0]]) + when "echi_vdns" + record = EchiVdn.find(:first, :conditions => [ "vdn = ? AND group_id = ?", field[1], field[0]]) + when "echi_vectors" + record = EchiVector.find(:first, :conditions => [ "number = ? AND acd_number = ?", field[1], field[0]]) + end + if record != nil + if record.name != field[2] + record.name = field[2] + record.update + @record_cnt += 1 + @log.debug "Updated record - " + field.inspect + else + @log.debug "No update required for - " + field.inspect + end + else + insert_dat_data file["name"], field + @record_cnt += 1 + @log.debug "Inserted new record - " + field.inspect + end + rescue => err + @log.info "Error processing ECHI record in process_proper_table_method - " + err + end + end + @log.debug '<====================STOP ' + file["name"] + ' RECORD ' + @record_cnt.to_s + ' ====================>' + end + + process_file.close + + archive_file file, @record_cnt + end + #Method to insert data into 'echi_agents' based on agname.dat def process_dat_files + @log.debug "process_dat_files method" dat_files = Array.new - dat_files[0] = { "name" => "echi_agents", "filename" => $workingdir + "/../files/to_process/" + $config["echi_agent_dat"] } - dat_files[1] = { "name" => "echi_aux_reasons", "filename" => $workingdir + "/../files/to_process/" + $config["echi_aux_rsn_dat"] } - dat_files[2] = { "name" =>"echi_cwcs", "filename" => $workingdir + "/../files/to_process/" + $config["echi_cwc_dat"] } - dat_files[3] = { "name" =>"echi_vdns", "filename" => $workingdir + "/../files/to_process/" + $config["echi_vdn_dat"] } - + dat_files[0] = { "name" => "echi_acds", "filename" => $workingdir + "/../files/to_process/" + $config["echi_acd_dat"] } + dat_files[1] = { "name" => "echi_agents", "filename" => $workingdir + "/../files/to_process/" + $config["echi_agent_dat"] } + dat_files[2] = { "name" => "echi_aux_reasons", "filename" => $workingdir + "/../files/to_process/" + $config["echi_aux_rsn_dat"] } + dat_files[3] = { "name" => "echi_cwcs", "filename" => $workingdir + "/../files/to_process/" + $config["echi_cwc_dat"] } + dat_files[4] = { "name" => "echi_splits", "filename" => $workingdir + "/../files/to_process/" + $config["echi_split_dat"] } + dat_files[5] = { "name" => "echi_trunk_groups", "filename" => $workingdir + "/../files/to_process/" + $config["echi_trunk_group_dat"] } + dat_files[6] = { "name" => "echi_vdns", "filename" => $workingdir + "/../files/to_process/" + $config["echi_vdn_dat"] } + dat_files[7] = { "name" => "echi_vectors", "filename" => $workingdir + "/../files/to_process/" + $config["echi_vector_dat"] } + dat_files.each do |file| - if File.exists?(file["filename"]) + if File.exists?(file["filename"]) && File.size(file["filename"]) > 0 case file["name"] + when "echi_acds" + EchiAcd.transaction do + process_proper_table file + end when "echi_agents" EchiAgent.transaction do process_proper_table file end when "echi_aux_reasons" @@ -438,14 +495,28 @@ end when "echi_cwcs" EchiCwc.transaction do process_proper_table file end + when "echi_splits" + EchiSplit.transaction do + process_proper_table file + end + when "echi_trunk_groups" + EchiTrunkGroup.transaction do + process_proper_table file + end when "echi_vdns" EchiVdn.transaction do process_proper_table file end + when "echi_vectors" + EchiVector.transaction do + process_proper_table file + end end + else + archive_file file, 0 end end end require $workingdir + '/echi-converter/version.rb' \ No newline at end of file