`

ror项目里通过FTP定时获取文件存储到程序目录

    博客分类:
  • RUBY
FTP 
阅读更多
module Net
  class FTP
    def mput(pattern, &block)
      Dir[pattern].each{ |file| put(file, &block) }
    end

    def mget(pattern, remote_path = ".", local_path = ".", &block)
#      remote_path = is_cw ? remote_path + "/" + pattern : remote_path  
      files = remote_path.blank? ? list(pattern) : list(remote_path + "/" + pattern)
#      files = is_cw ? list(remote_path) : nlst(remote_path)  
      localfiles = []
      files.each{ |file|
        filename = filename(file)
        file_extname = filename.split(".").last
        localfile = local_path + "/" + File.basename(filename)
        ext_name = ["txt","xml", "dat"] # 标准的文本文件结尾
        ext_name << "end" 备份文件采集.end 结尾,实际上是TXT文件
        if(ext_name.include?(file_extname.downcase)||file_extname.downcase.match(/\d{4}-\d{1,2}-\d{1,2}$/))
          gettextfile(remote_path + "/" + filename, localfile, &block)
#          gettextfile( filename, localfile, &block)  
        else
          getbinaryfile(remote_path + filename, localfile, &block)
        end
        sleep(1)
        localfiles << localfile
      }
      return localfiles
    end

    def filename(o_filename)
      if(o_filename.start_with?("l"))
        s = o_filename.split(" ")
        s[s.length - 3]
      else
        o_filename.split(" ").last
      end
    end
  end
end




rake定时文件

desc 'Import Hubei radius CDR'
namespace :hb do
  task :import_radius  => :environment do
    date = ENV['date']
    date ||= (DateTime.now - 1.day).strftime("%Y-%m-%d")
    FileUtils.mkpath("#{RAILS_ROOT}/tmp/radius")
    FileUtils.mkpath("#{RAILS_ROOT}/tmp/sqlldr")
    radius_config = YAML.load_file("#{RAILS_ROOT}/config/value_analysis.yml").symbolize_keys!
    time_start = Time.now
    sqlldr_file = File.new("#{RAILS_ROOT}/tmp/sqlldr/#{date}-radius.ctl", "w")
    begin
      write_header(sqlldr_file)
      date_pattern = date.to_date.strftime("%Y%m%d")
      sqlldr_lines = []
      Net::FTP.open(radius_config[:radius_host], radius_config[:radius_username], radius_config[:radius_password]) { |ftp|
        ftp.passive = true
        ftp.debug_mode = true
        ftp.resume = true
        ftp.mget("*" + date_pattern + "*.txt", radius_config[:radius_path] + "/#{date_pattern}", "#{RAILS_ROOT}/tmp/radius") do |line|
          sqlldr_line = []
          unless line.nil?
            cell_def.each do |d|
              key = d.keys.first
              value = d.values.first
              fields_value = line.split("|")
              if fields_value.length > 5
                if ['net_type','auth_type'].include?(key.to_s)
                  sqlldr_line << value
                elsif value.is_a? Fixnum
                  sqlldr_line << fields_value[value]
                elsif value[:format].nil?
                  sqlldr_line << fields_value[value[:index]]
                else
                  sqlldr_line << value[:format].call(fields_value[value[:index]])
                end
              end              
            end
            sqlldr_lines << sqlldr_line.join(",")
          end
        end
      }
      sqlldr_file << sqlldr_lines.join("\n")
      puts "total_time for writing sqlldr_file:#{Time.now - time_start}"
      sleep 10
      time_start = Time.now
      sqlldr_com = "sqlldr userid=#{$CFG['username']}/#{$CFG['password']}@#{$CFG['service']},control=#{RAILS_ROOT}/tmp/sqlldr/#{date}-radius.ctl,log=#{RAILS_ROOT}/log/#{date}.log,direct=true"
      puts sqlldr_com
      system sqlldr_com
      puts "total_time for importing data:#{Time.now - time_start}"
      time_start = Time.now
      puts "calling deal wlan auth perf task procedure"
      sleep 0.02
      Report.deal_auth_deal_perf_task(date)
      puts "total_time for execute procedure:#{Time.now - time_start}"
    ensure
      sqlldr_file.close
    end
  end

  def cell_def
    [
      {:login => 2},
      {:start_time => {
          :index => 30,
          :head => "START_TIME Date 'yyyymmddHH24miss'"
        }},
      {:sta_mac => {
          :index => 20,
          :format => Proc.new {|mac| format_mac(mac) }
        }},
      {:end_time => {
          :index => 31,
          :head => "END_TIME Date 'yyyymmddHH24miss'"
        }},
      {:period_time => {
          :index => 32,
          :format => Proc.new {|time_len| time_len.to_i / 60 }
        }},
      {:bytes_in => {
          :index => 12,
          :format => Proc.new {|bytes| bytes.to_i * 1000 }
        }
      },
      {:bytes_out => {
          :index => 11,
          :format => Proc.new {|bytes| bytes.to_i * 1000 }
        }
      },
      {:svlan_id => 48},
      {:net_type => 1},
      {:auth_type => 1}
    ]
  end

  def format_mac(mac)
    "#{mac[0..1]}:#{mac[2..3]}:#{mac[4..5]}:#{mac[6..7]}:#{mac[8..9]}:#{mac[10..11]}".upcase
  end

  def write_header(sqlldr_file)
    sqlldr_file << "OPTIONS(ERRORS=100000)\nLoad DATA\nINFILE *\nTRUNCATE INTO TABLE WLAN_AUTH_IMPORT_TEMP\nFields terminated by \",\"\nTRAILING NULLCOLS\n"
    head = []
    cell_def.each do |d|
      key = d.keys.first
      value = d.values.first
      if value.is_a? Fixnum or value[:head].nil?
        head << key.to_s.upcase
      else
        head << value[:head]
      end
    end
    sqlldr_file << "(#{head.join(",")})\nBEGINDATA\n"
  end
end
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics