Created
December 15, 2010 15:51
-
-
Save asim/742124 to your computer and use it in GitHub Desktop.
stats class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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