Created
December 15, 2010 15:51
-
-
Save asim/742124 to your computer and use it in GitHub Desktop.
Revisions
-
Asim created this gist
Dec 15, 2010 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,32 @@ 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