Skip to content

Instantly share code, notes, and snippets.

@asim
Created December 15, 2010 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save asim/742124 to your computer and use it in GitHub Desktop.
Save asim/742124 to your computer and use it in GitHub Desktop.
stats class
class Stats
QUEUES = ["bounced", "connect", "conversations", "deferred", "host", "lost", "sent"]
def self.lookup(pattern)
h = {}
REDIS.keys(pattern).map { |k,v| h[k] = REDIS[k] }
h
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.nil?
count = sum_day_for(ip,queue)
REDIS.setex(:"#{queue}:#{ip}:24", 120, count) if count.class == Fixnum
end
count
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
private_class_method :return_day_for, :sum_day_for
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment