lib/echi-converter.rb in echi-converter-0.3.1 vs lib/echi-converter.rb in echi-converter-0.3.2

- old
+ new

@@ -2,20 +2,20 @@ require 'active_record' require 'faster_csv' require 'net/ftp' require 'net/smtp' require 'fileutils' +require 'uuidtools' class Logger #Change the logging format to include a timestamp def format_message(severity, timestamp, progname, msg) "#{timestamp} (#{$$}) #{msg}\n" end end module EchiConverter - def connect_database databaseconfig = $workingdir + '/../config/database.yml' dblogfile = $workingdir + '/../log/database.log' ActiveRecord::Base.logger = Logger.new(dblogfile, $config["log_number"], $config["log_length"]) case $config["log_level"] @@ -257,13 +257,20 @@ begin if $config["echi_ftp_directory"] != nil ftp_session.chdir($config["echi_ftp_directory"]) end files = ftp_session.list('chr*') + + #Also fetch the agname.dat file if it is configured to be processed + if $config["echi_update_agent_data"] == "Y" + files = files + ftp_session.list("agname.dat") + end + file_cnt = 0 files.each do | file | file_data = file.split(' ') + local_filename = $workingdir + '/../files/to_process/' + file_data[8] ftp_session.getbinaryfile(file_data[8], local_filename) if $config["echi_ftp_delete"] == 'Y' begin ftp_session.delete(remote_filename) @@ -278,65 +285,132 @@ @log.fatal "Could not fetch from ftp server - " + err end end return end -end -def process_ascii filename - echi_file = $workingdir + "/../files/to_process/" + filename + def process_ascii filename + echi_file = $workingdir + "/../files/to_process/" + filename - if $config["echi_process_log"] == "Y" - #Log the file - echi_log = EchiLog.new - echi_log.filename = filename - #echi_log.filenumber = filenumber - #echi_log.version = fileversion - end + if $config["echi_process_log"] == "Y" + #Log the file + echi_log = EchiLog.new + echi_log.filename = filename + #echi_log.filenumber = filenumber + #echi_log.version = fileversion + end - #Perform a transaction for each file, including the log table - #in order to commit as one atomic action upon success - EchiRecord.transaction do - @record_cnt = 0 - FasterCSV.foreach(echi_file) do |row| - if row != nil - @log.debug '<====================START RECORD ' + @record_cnt.to_s + ' ====================>' - echi_record = EchiRecord.new - cnt = 0 - @echi_schema["fields"].each do | field | - if field["type"] == "bool" || field["type"] == "bool_int" - case row[cnt] - when "0" - echi_record[field["name"]] = "N" - when "1" - echi_record[field["name"]] = "Y" - end - @log.debug field["name"] + ' == ' + row[cnt] - else - echi_record[field["name"]] = row[cnt] - if row[cnt] != nil + #Perform a transaction for each file, including the log table + #in order to commit as one atomic action upon success + EchiRecord.transaction do + @record_cnt = 0 + FasterCSV.foreach(echi_file) do |row| + if row != nil + @log.debug '<====================START RECORD ' + @record_cnt.to_s + ' ====================>' + echi_record = EchiRecord.new + cnt = 0 + @echi_schema["fields"].each do | field | + if field["type"] == "bool" || field["type"] == "bool_int" + case row[cnt] + when "0" + echi_record[field["name"]] = "N" + when "1" + echi_record[field["name"]] = "Y" + end @log.debug field["name"] + ' == ' + row[cnt] + else + echi_record[field["name"]] = row[cnt] + if row[cnt] != nil + @log.debug field["name"] + ' == ' + row[cnt] + end end + cnt += 1 end - cnt += 1 + echi_record.save + @log.debug '<====================STOP RECORD ' + @record_cnt.to_s + ' ====================>' + @record_cnt += 1 end - echi_record.save - @log.debug '<====================STOP RECORD ' + @record_cnt.to_s + ' ====================>' - @record_cnt += 1 end end - end - #Move the file to the processed directory - FileUtils.mv(echi_file, @processeddirectory) + #Move the file to the processed directory + FileUtils.mv(echi_file, @processeddirectory) - if $config["echi_process_log"] == "Y" - #Finish logging the details on the file - echi_log.records = @record_cnt - echi_log.processed_at = Time.now - echi_log.save + if $config["echi_process_log"] == "Y" + #Finish logging the details on the file + echi_log.records = @record_cnt + echi_log.processed_at = Time.now + echi_log.save + end + + return @record_cnt end + + def insert_agent_data field + + begin + echi_agent = EchiAgent.new + echi_agent.group_id = field[0] + echi_agent.login_id = field[1] + echi_agent.name = field[2] + echi_agent.save + rescue => err + @log.info "Unable to insert agent record - " + err + end + + end - return @record_cnt -end + #Method to insert data into 'echi_agents' based on agname.dat + def process_agent_data + agent_file = $workingdir + "/../files/to_process/agname.dat" + + if File.exists?(agent_file) + EchiAgent.transaction do + @record_cnt = 0 + File.open(agent_file).each do |row| + if row != nil + field = row.rstrip.split('|') + @log.debug '<====================START AGENT RECORD ' + @record_cnt.to_s + ' ====================>' + agent = EchiAgent.find(:first, :conditions => [ "login_id = ? AND group_id = ?", field[1], field[0]]) + if agent != nil + if agent.name != field[2] + agent.name = field[2] + agent.update + @record_cnt += 1 + @log.debug "Updated record - " + field.inspect + else + @log.debug "No update required for - " + field.inspect + end + else + insert_agent_data field + @record_cnt += 1 + @log.debug "Inserted new record - " + field.inspect + end + end + @log.debug '<====================STOP AGENT RECORD ' + @record_cnt.to_s + ' ====================>' + end + end + @agent_file_processed = Time.now + #Move the file to the processed directory + begin + agname_new_filename = "agname_" + UUID.timestamp_create.to_s + ".dat" + target_file = @processeddirectory + "/" + agname_new_filename + FileUtils.mv(agent_file, target_file) + rescue => err + @log.info "Issue with agname_*.dat filename - " + err + end + if $config["echi_process_log"] == "Y" + #Log the file + echi_log = EchiLog.new + echi_log.filename = agname_new_filename + #echi_log.filenumber = filenumber + #echi_log.version = fileversion + #Finish logging the details on the file + echi_log.records = @record_cnt + echi_log.processed_at = Time.now + echi_log.save + end + end + end -require $workingdir + '/echi-converter/version.rb' + require $workingdir + '/echi-converter/version.rb' +end \ No newline at end of file