Skip to content

Instantly share code, notes, and snippets.

@mudge
Forked from asim/stats.rb
Created December 15, 2010 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mudge/742158 to your computer and use it in GitHub Desktop.
Save mudge/742158 to your computer and use it in GitHub Desktop.
class Stats
QUEUES = [:bounced, :connect, :conversations, :deferred, :host, :lost, :sent]
def self.lookup(pattern)
keys = REDIS.keys(pattern)
REDIS.mapped_mget(*keys)
end
def self.day_for(ip)
# method should return a hash of count queues for 24 hours
h = {}
Stats::QUEUES.each do |queue|
h[:"#{queue}:#{ip}:24"] = return_day_for(ip,queue)
end
h
end
def self.return_day_for(ip, queue)
count = REDIS[:"#{queue}:#{ip}:24"]
if !count
new_count = sum_day_for(ip, queue)
REDIS.setex(:"#{queue}:#{ip}:24", 120, new_count) if new_count
new_count
else
count
end
end
def self.sum_day_for(ip, queue)
keys = REDIS.keys(:"#{queue}:#{ip}:*")
REDIS.mget(*keys).inject(0) { |sum, n| sum + n.to_i } unless keys.empty?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment