Skip to content

Instantly share code, notes, and snippets.

@joelbrewer
Created June 12, 2015 02:17
Show Gist options
  • Save joelbrewer/4d10c04c7fea4065fc4b to your computer and use it in GitHub Desktop.
Save joelbrewer/4d10c04c7fea4065fc4b to your computer and use it in GitHub Desktop.
require 'csv'
require 'json'
require 'set'
require 'pry'
class ProcessLog
def initialize
@mtaids = Set.new []
@data = Hash.new { |hash, key| hash[key] = {"sent": 0, "failed": 0, "successful": 0, "blocking_filters": (Hash.new 0), "blocking_domains": ( Hash.new 0 ) } }
@statuses = Hash.new 0
@status_messages = Hash.new 0
@file = ARGV[0]
@frequently_seen_strings = [ "mxlogic", "mimecast", "mcaf.ee", "symanteccloud", "IB504", "support.google.com", "Fortiguard" ]
unless @file
fail "Must specify a file"
end
end
def process_csv
CSV.foreach(@file) do |row|
domain = row[2]
mtaid = row[3]
status = row[4]
status_message = row[6]
@mtaids << mtaid
next if mtaid.empty?
@data[mtaid][:sent] += 1
if status == "failed"
@data[mtaid][:failed] += 1
else
@data[mtaid][:successful] += 1
end
if !status_message.empty?
@frequently_seen_strings.each do |string|
if status_message.include?(string)
@data[mtaid][:blocking_filters][string] += 1
@data[mtaid][:blocking_domains][domain] += 1
else
@data[mtaid][:blocking_filters]["other"] += 1
@data[mtaid][:blocking_domains][domain] += 1
break
end
end
end
end
end
def post_process
@mtaids.each do |mtaid|
@data[mtaid][:blocking_filters] = Hash[@data[mtaid][:blocking_filters].sort_by {|k,v| v}.reverse[0..9]]
@data[mtaid][:blocking_domains] = Hash[@data[mtaid][:blocking_domains].sort_by {|k,v| v}.reverse[0..9]]
end
end
def print_results(return_type)
case return_type
when :csv
puts @data.to_csv
when :json
puts @data.to_json
when :text
puts "mtaids:"
binding.pry
@data.each do |mtaid, value|
if !mtaid.empty?
puts
puts "mtaid: #{mtaid}"
puts "number sent: #{value[:sent]}"
puts "% failed: #{value[:failed].to_f/value[:sent].to_f * 100}"
puts "top blocking filters: #{value[:blocking_filters]}"
puts "top blocking domains: #{value[:blocking_domains]}"
end
end
end
end
end
process_log = ProcessLog.new
process_log.process_csv
process_log.post_process
process_log.print_results(:text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment