Skip to content

Instantly share code, notes, and snippets.

@daveyeu
Created October 24, 2011 17:38
Show Gist options
  • Save daveyeu/1309599 to your computer and use it in GitHub Desktop.
Save daveyeu/1309599 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "rubygems"
require "eventmachine"
$search_terms = ARGV
if $search_terms.empty?
puts "Hey now, add some search terms for your buckets."
exit 1
end
$count = {}
$regex = {}
$search_terms.each do |term|
$count[term] = 0
$regex[term] = Regexp.new(term)
end
class Rollup < EM::Connection
def receive_data(data)
data.each_line do |line|
$search_terms.each do |term|
$count[term] += 1 if line =~ $regex[term]
end
end
end
def unbind
puts "Connection closed"
EM.stop
end
end
EM.run do
EM.attach $stdin, Rollup
format = $count.map { "%s : (%12d, %2.2f%%)" }.join(", ")
EM.add_periodic_timer(5) do
total = $count.each_value.inject(0) { |acc,v| acc += v }
values = $count.map do |term, count|
[term, count, ((count.to_f / total) * 100)]
end
puts format % values.flatten
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment