SHARE
TWEET

Untitled

a guest Dec 8th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class ServerLogParser
  2.   PSEUDO_IP_MATCHER = /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
  3.   PATH_MATCHER = /^(.*?) /
  4.  
  5.   attr_reader :hits
  6.  
  7.   def initialize file_name
  8.     @log_file = File.open(file_name)
  9.     @hits = {}
  10.  
  11.     create_data
  12.   end
  13.  
  14.   def create_data
  15.     @log_file.each do |log_line|
  16.       path = log_line.match(PATH_MATCHER)
  17.       ip = log_line.match(PSEUDO_IP_MATCHER)
  18.       next unless path && ip
  19.       path, ip = path.captures.first, ip.captures.first
  20.       if @hits.keys.include?(path)
  21.         @hits[path] << ip
  22.       else
  23.         @hits[path] = [ip]
  24.       end
  25.     end
  26.  
  27.     print_out
  28.   end
  29.  
  30.   def print_out
  31.     puts 'Total'
  32.     print_lines(sorted_by_total)
  33.     puts "\nUniques"
  34.     print_lines(unique=true, sorted_by_uniques)
  35.   end
  36.  
  37.   def sorted_by_uniques
  38.     @hits.sort_by { |k, v| v.uniq.count }.reverse
  39.   end
  40.  
  41.   def sorted_by_total
  42.     @hits.sort_by { |k, v| v.count }.reverse
  43.   end
  44.  
  45.   def get_value(data, unique)
  46.     [data.first, unique ? data.last.uniq.count : data.last.count]
  47.   end
  48.  
  49.   def print_lines(unique=false, data)
  50.     data.each do |datapoint|
  51.       puts get_value(datapoint, unique).join(' ')
  52.     end
  53.   end
  54. end
  55.  
  56. if ARGV.any? && File.file?(ARGV.first)
  57.   parser = ServerLogParser.new(ARGV.first)
  58. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top