Skip to content

Instantly share code, notes, and snippets.

@asim
Created December 15, 2010 15:51
Show Gist options
  • Save asim/742124 to your computer and use it in GitHub Desktop.
Save asim/742124 to your computer and use it in GitHub Desktop.

Revisions

  1. Asim created this gist Dec 15, 2010.
    32 changes: 32 additions & 0 deletions stats.rb
    Original 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