require 'log'

total_size = 0
by_resource = Hash.new(0)
by_hour = Hash.new(0)
by_day = Hash.new(0)
by_host = Hash.new(0)

Log::each [:host, :size, :resource, :hour, :day, :status] do |r|
  next if r[:status] != 200
  bytes = r[:size]
  total_size += bytes
  by_hour[r[:hour]] += bytes
  by_day[r[:day]] += bytes
  by_resource[r[:resource]] += bytes
  by_host[r[:host]] += bytes
end

def meg(i)
  format("%.1f", i / (1024.0 * 1024.0))
end

def report(hash, name)
  keys = hash.keys.sort_by{ |key| - hash[key] }
  puts "By #{name}:"
  keys[0 .. 9].each do |key|
    puts "#{meg(hash[key])}: #{key}"
  end
  puts
end

puts "Total bytes: #{total_size} (#{meg(total_size)}M)"
report(by_host, "Host")
report(by_resource, "Resource")
report(by_hour, "Hour")
report(by_day, "Day")