Skip to content

Instantly share code, notes, and snippets.

@ashmckenzie
Created September 1, 2009 01:02
Show Gist options
  • Save ashmckenzie/178817 to your computer and use it in GitHub Desktop.
Save ashmckenzie/178817 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'pp'
DEBUG = false
stats = {}
stats['Firefox-3.5'] = { :hits => 0, :percent => 0 }
stats['Firefox-3.0'] = { :hits => 0, :percent => 0 }
stats['Firefox-2.0'] = { :hits => 0, :percent => 0 }
stats['Msie-6.0'] = { :hits => 0, :percent => 0 }
stats['Msie-7.0'] = { :hits => 0, :percent => 0 }
stats['Msie-8.0'] = { :hits => 0, :percent => 0 }
total_stats = {
'Firefox' => 0,
'Msie' => 0
}
File.open('stats.txt', 'r') do |f|
f.readlines.each do |l|
next if l.gsub!(/^\s*|\s*$/, '').empty?
m = /^\s*(\D+)\s+([\d\.]+)\s*\w+\s*(\d+).*$/.match(l)
browser = m[1]
browser_and_version = "#{m[1]}-#{m[2]}"
hits = m[3].to_i
stats.each do |x|
puts "DEBUG: Checking #{browser_and_version} against #{x[0]}" if DEBUG
if browser_and_version.include?(x[0])
puts "DEBUG: #{browser_and_version} matches #{x[0]} - adding #{hits}" if DEBUG
stats[x[0]][:hits] = stats[x[0]][:hits] + hits
total_stats[browser] = total_stats[browser] + hits
break
end
end
end
end
pp total_stats
stats.each do |y|
total_stats.each do |x|
puts "DEBUG: Checking #{y[0]} against #{x[0]}" if DEBUG
if y[0].include?(x[0])
puts "DEBUG: #{y[0]} matches #{x[0]}" if DEBUG
stats[y[0]][:percent] = sprintf('%.2f', (y[1][:hits].to_f / x[1].to_f) * 100)
break
end
end
end
pp stats.sort
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment