Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ServerLogParser
- PSEUDO_IP_MATCHER = /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
- PATH_MATCHER = /^(.*?) /
- attr_reader :hits
- def initialize file_name
- @log_file = File.open(file_name)
- @hits = {}
- create_data
- end
- def create_data
- @log_file.each do |log_line|
- path = log_line.match(PATH_MATCHER)
- ip = log_line.match(PSEUDO_IP_MATCHER)
- next unless path && ip
- path, ip = path.captures.first, ip.captures.first
- if @hits.keys.include?(path)
- @hits[path] << ip
- else
- @hits[path] = [ip]
- end
- end
- print_out
- end
- def print_out
- puts 'Total'
- print_lines(sorted_by_total)
- puts "\nUniques"
- print_lines(unique=true, sorted_by_uniques)
- end
- def sorted_by_uniques
- @hits.sort_by { |k, v| v.uniq.count }.reverse
- end
- def sorted_by_total
- @hits.sort_by { |k, v| v.count }.reverse
- end
- def get_value(data, unique)
- [data.first, unique ? data.last.uniq.count : data.last.count]
- end
- def print_lines(unique=false, data)
- data.each do |datapoint|
- puts get_value(datapoint, unique).join(' ')
- end
- end
- end
- if ARGV.any? && File.file?(ARGV.first)
- parser = ServerLogParser.new(ARGV.first)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement