Class: CallRecord::SwitchvoxImporterSftp
- Inherits:
-
Object
- Object
- CallRecord::SwitchvoxImporterSftp
- Defined in:
- app/services/call_record/switchvox_importer_sftp.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
- #archive_file(file_name) ⇒ Object
- #attach_file_to_call_record(cr, local_file_path) ⇒ Object
-
#build_options ⇒ Object
Here we build an option hash for the SSH connection, any of these value can be added http://net-ssh.github.io/net-ssh/Net/SSH.html.
-
#call_attributes(xml_doc) ⇒ Object
Retrieve all interesting attributes to a hash.
- #caller_id_parser(cid) {|origin_name, origin_number| ... } ⇒ Object
- #connect_and_import(file_name) ⇒ Object
- #connection ⇒ Object
-
#download_file(file_name) ⇒ Object
Downloads a remote file to a local file path, returns the local file path.
- #error_folder ⇒ Object
-
#file_limit ⇒ Object
The maximum number of files we will process.
- #file_pattern ⇒ Object
- #get_file_list ⇒ Object
- #host ⇒ Object
- #import_file(file_name) ⇒ Object
-
#import_new_records(limit = nil) ⇒ Object
Main entry point method.
-
#initialize(options = {}) ⇒ SwitchvoxImporterSftp
constructor
A new instance of SwitchvoxImporterSftp.
- #login ⇒ Object
- #mv(source_file_path, destination_file_path) ⇒ Object
-
#parse_node(xml_doc, node_name) ⇒ Object
Simplify retrieval of attributes since we only have one in this file.
-
#parse_xml_file(file_path) ⇒ Object
Parse a remote file using nokogiri and returns a nokogiri doc.
- #password ⇒ Object
- #processed_folder ⇒ Object
- #remote_path ⇒ Object
- #rm(file_path) ⇒ Object
- #skip_download? ⇒ Boolean
- #unprocessable_file(file_name) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ SwitchvoxImporterSftp
Returns a new instance of SwitchvoxImporterSftp.
53 54 55 56 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 53 def initialize(={}) @options = @logger = [:logger] || Rails.logger end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
5 6 7 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 5 def logger @logger end |
Instance Method Details
#archive_file(file_name) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 105 def archive_file(file_name) # year,month,day = file_name.split('-') connection do |c| archive_path = processed_folder.dup # [year, month, day].each do |dp| # archive_path << "/#{dp}" # archive_path = archive_path.squeeze('/') # logger.info "Making directory #{archive_path}" # c.mkdir!(archive_path) rescue nil # end source_file_path = "#{remote_path}#{file_name}" target_file_path = "#{remote_path}#{archive_path}#{file_name}" logger.info "Moving file from #{source_file_path} to #{target_file_path}" begin c.rename!(source_file_path, target_file_path) logger.info "#{file_name} was moved to #{archive_path}." rescue StandardError => exc logger.error "#{file_name} could not be moved #{exc.to_s}, file missing perhaps #{archive_path}" end end end |
#attach_file_to_call_record(cr, local_file_path) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 135 def attach_file_to_call_record(cr, local_file_path) return false unless local_file_path && cr return false if cr.upload # Already has a file au = Upload.uploadify( local_file_path, "call_record", cr ) if au and !au.new_record? and au..present? logger.info " ** Audio record upload #{au.id} created" cr.update_attribute(:imported, true) return true else = "!! Problem creating audio record for #{local_file_path}: #{au.errors_to_s}" logger.error Mailer.admin_notification().deliver_now if (Rails.env.production? || Rails.env.staging?) return false end end |
#build_options ⇒ Object
Here we build an option hash for the SSH connection, any of these value
can be added
http://net-ssh.github.io/net-ssh/Net/SSH.html
39 40 41 42 43 44 45 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 39 def { password: password, auth_methods: ['password'], #Use password explicitly, don't do public/private key config: false #Don't read local options } end |
#call_attributes(xml_doc) ⇒ Object
Retrieve all interesting attributes to a hash
199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 199 def call_attributes(xml_doc) attributes = %w( recording_tag recorded_call_id recorder_cid recorded_cid recorder_account_id recorded_account_id from_account_id from_caller_id to_account_id to_caller_id duration date_created_ts date_created_secs ) hsh = {} attributes.each do |attr_name| val = parse_node(xml_doc, attr_name) logger.debug " *** #{attr_name} -> #{val}" hsh[attr_name.to_sym] = val end hsh end |
#caller_id_parser(cid) {|origin_name, origin_number| ... } ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 286 def caller_id_parser(cid) return unless cid.present? expr = cid.match(/(.+)\<(\d+)\>/) if expr and expr.length == 3 origin_name = expr[1].presence origin_number = expr[2] else # Just record the caller id as all letters in the name, all numbers in the number origin_name = cid.match(/[a-zA-Z\s]+/).to_s.presence origin_number = cid.match(/\d+/).to_s.presence end yield(origin_name, origin_number) end |
#connect_and_import(file_name) ⇒ Object
32 33 34 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 32 def connect_and_import(file_name) import_file file_name end |
#connection ⇒ Object
47 48 49 50 51 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 47 def connection Net::SFTP.start(host, login, ) do |sftp| yield(sftp) end end |
#download_file(file_name) ⇒ Object
Downloads a remote file to a local file path, returns the local file path
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 153 def download_file(file_name) begin local_file_path = Rails.application.config.x.temp_storage_path.join(file_name).to_s remote_file_path = "#{remote_path}#{file_name}" logger.info "Downloading #{remote_file_path}" connection do |c| c.download!(remote_file_path, local_file_path) end return local_file_path if File.exist?(local_file_path) rescue StandardError => exc msg = "Unable to download #{file_name} from ftp #{remote_file_path}, #{exc.to_s}" logger.error msg ErrorReporting.warning("Unable to download #{file_name} from ftp #{remote_file_path}, #{exc.to_s}") end return nil end |
#error_folder ⇒ Object
87 88 89 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 87 def error_folder @options[:error_folder] || "errors/" end |
#file_limit ⇒ Object
The maximum number of files we will process
79 80 81 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 79 def file_limit @options[:file_limit] || 99999 end |
#file_pattern ⇒ Object
74 75 76 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 74 def file_pattern @options[:file_pattern] || "*.wav" end |
#get_file_list ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 95 def get_file_list file_list = [] connection do |c| c.dir.glob(remote_path, "*.wav") do |entry| file_list << entry.name end end file_list end |
#host ⇒ Object
58 59 60 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 58 def host @options[:host] || Heatwave::Configuration.fetch(:switchvox, call_records_host) end |
#import_file(file_name) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 213 def import_file(file_name) # The file name is an xml document per our pattern. What is the base name? base_file_name = file_name.scan(/\A(.*)\.(\S{3})\z/).flatten[0] xml_file_name = "#{base_file_name}.xml" recording_file_name = "#{base_file_name}.wav" xml_local_file_path = download_file(xml_file_name) recording_local_file_path = download_file(recording_file_name) raise "Could not download #{file_name}" unless recording_local_file_path if xml_local_file_path call_attrs = parse_xml_file(xml_local_file_path) logger.debug("Call attributes processed") switchvox_recorded_call_id = call_attrs[:recorded_call_id] # see if we have one already to update cr = CallRecord.where(switchvox_recorded_call_id: switchvox_recorded_call_id).first_or_initialize cr.recording_source = 'switchvox' cr.created_at = Time.zone.parse( call_attrs[:date_created_ts] ) # Parse the caller id caller_id_parser( call_attrs[:from_caller_id] ) do |name, number| cr.origin_name = name cr.origin_number = number end # Parse the destination caller_id_parser( call_attrs[:to_caller_id] ) do |name, number| cr.destination_name = name cr.destination_number = number end cr.duration_secs = call_attrs[:duration] cr.reference1 = call_attrs[:recording_tag] cr.reference2 = base_file_name cr.switchvox_from_account_id = call_attrs[:from_account_id] cr.switchvox_to_account_id = call_attrs[:to_account_id] cr.switchvox_recorded_call_id = call_attrs[:recorded_call_id] else # Parse file name logger.info "No XML, using WAV filename information #{base_file_name}" cr = CallRecord.where(reference2: base_file_name).first_or_initialize cr.recording_source = 'switchvox' if cr.persisted? logger.info "CallRecord detected as previously imported: #{cr.id} for #{base_file_name}" else logger.info "Creating new call record for #{base_file_name}" end # Example string: 2018-01-08-09-46-37_+16305796221_829 # 2018-01-08-09-52-07_829_809 # 2018-01-08-10-02-04_852_17654526845 parts = base_file_name.scan(/\A(\d{4})-(\d{2})-(\d{2})-(\d{2})-(\d{2})-(\d{2})_\+?(\d{3,})_\+?(\d{3,})\Z/) year,month,day,hour,minutes,seconds,from_caller_id,to_caller_id = *parts.flatten cr.origin_number = from_caller_id cr.destination_number = to_caller_id end cr.match_parties if cr.valid? if cr.save logger.info "attaching local file: #{recording_local_file_path} to call record #{cr.id}" attach_file_to_call_record(cr, recording_local_file_path) archive_file "#{base_file_name}.xml" if xml_local_file_path # only try this if we had an xml file archive_file "#{base_file_name}.wav" else = " !! Call Record failed to save for file #{file_name}, #{cr.errors_to_s}" Mailer.admin_notification().deliver_now if (Rails.env.production? || Rails.env.staging?) logger.error unprocessable_file "#{base_file_name}.wav" unprocessable_file "#{base_file_name}.xml" end end |
#import_new_records(limit = nil) ⇒ Object
Main entry point method
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 8 def import_new_records(limit = nil) limit ||= file_limit #list all files #recording-20120911_153955-8475502400-58204-8475407775-10238-LES.wav logger.info "Retrieving list of files to process" file_paths = get_file_list total_files = file_paths.length logger.info "Detected #{total_files} files/record(s) to process. File limit set to #{file_limit}" file_paths.each_with_index do |file_name, i| break if i >= limit logger.info " - Processing record ##{i} / #{total_files} : #{file_name}" if Rails.env.development? #Import right away import_file file_name else #Enqueue logger.info " - Enqueuing for processing #{file_name}" CallRecordImporterWorker.perform_async(file_name) end end end |
#login ⇒ Object
62 63 64 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 62 def login @options[:login] || Heatwave::Configuration.fetch(:switchvox, :call_records_user) end |
#mv(source_file_path, destination_file_path) ⇒ Object
178 179 180 181 182 183 184 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 178 def mv(source_file_path,destination_file_path) connection do |c| logger.info "SFTP moving file #{source_file_path} to #{destination_file_path}" c.rename!(source_file_path, destination_file_path) end :deleted end |
#parse_node(xml_doc, node_name) ⇒ Object
Simplify retrieval of attributes since we only have one in this file
194 195 196 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 194 def parse_node(xml_doc, node_name) xml_doc.at_xpath("//#{node_name}").content.presence end |
#parse_xml_file(file_path) ⇒ Object
Parse a remote file using nokogiri and returns a nokogiri doc
188 189 190 191 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 188 def parse_xml_file(file_path) xml_doc = Nokogiri::XML( File.read(file_path) ) call_attributes(xml_doc) end |
#password ⇒ Object
66 67 68 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 66 def password @options[:password] || Heatwave::Configuration.fetch(:switchvox, :call_records_password) end |
#processed_folder ⇒ Object
83 84 85 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 83 def processed_folder @options[:processed_folder] || "processed/" end |
#remote_path ⇒ Object
70 71 72 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 70 def remote_path @options[:remote_path] || Heatwave::Configuration.fetch(:switchvox, :call_records_directory) end |
#rm(file_path) ⇒ Object
170 171 172 173 174 175 176 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 170 def rm(file_path) connection do |c| logger.info "SFTP deleting file #{file_path}" c.remove!(file_path) end :deleted end |
#skip_download? ⇒ Boolean
91 92 93 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 91 def skip_download? @options[:skip_download].nil? ? false : @options[:skip_download] end |
#unprocessable_file(file_name) ⇒ Object
127 128 129 130 131 132 133 |
# File 'app/services/call_record/switchvox_importer_sftp.rb', line 127 def unprocessable_file(file_name) connection do | ftp| ftp.chdir(remote_path) ftp.rename("#{remote_path}/#{file_name}", "#{error_folder}#{file_name}") logger.info "#{file_name} was moved to #{error_folder}" end end |