class SnappyStats
Constants
- GRANULARITIES
- VERSION
Attributes
config[RW]
Public Class Methods
new(options = {})
click to toggle source
# File lib/snappy_stats.rb, line 32 def initialize(options = {}) @config = SnappyStats::Config.new @config.redis = options[:redis] end
Public Instance Methods
configure() { |config| ... }
click to toggle source
# File lib/snappy_stats.rb, line 41 def configure yield(config) end
connection()
click to toggle source
# File lib/snappy_stats.rb, line 45 def connection @connection ||= config.redis end
get(gran, from, to, key)
click to toggle source
# File lib/snappy_stats.rb, line 84 def get(gran, from, to, key) granularity = GRANULARITIES[gran] size = granularity[:size] factor = granularity[:factor] from = getFactoredTimestamp( from, factor ) to = getFactoredTimestamp( to, factor ) ts = from i = 0 results = {} current_key = "" data = nil while ts <= to tsround = getRoundedTimestamp( ts, size * factor ) redis_key = "#{hash_key}:#{key}:#{gran}:#{tsround}" if(current_key != redis_key) data = connection.hgetall( redis_key ) current_key = redis_key end results[ts] = data[ ts.to_s ] i = i+1 ts = ts + GRANULARITIES[gran][:factor] end results end
getFactoredTimestamp( ts_seconds, factor )
click to toggle source
# File lib/snappy_stats.rb, line 59 def getFactoredTimestamp( ts_seconds, factor ) ts_seconds = ts_seconds || self.getSecondsTimestamp ( ts_seconds / factor ).floor * factor end
getRoundedTimestamp( ts , precision )
click to toggle source
# File lib/snappy_stats.rb, line 53 def getRoundedTimestamp( ts , precision ) ts = ts || self.getSecondsTimestamp precision = precision || 1 ( ts / precision ).floor * precision end
getSecondsTimestamp()
click to toggle source
# File lib/snappy_stats.rb, line 49 def getSecondsTimestamp Time.now.getutc.to_i end
hash_key()
click to toggle source
# File lib/snappy_stats.rb, line 37 def hash_key "#{config.namespace}" end
recordHit( time, key )
click to toggle source
# File lib/snappy_stats.rb, line 68 def recordHit( time, key ) GRANULARITIES.keys.each do | gran | granularity = GRANULARITIES[gran] size = granularity[:size] factor = granularity[:factor] ttl = granularity[:ttl] tsround = getRoundedTimestamp(time, size * factor) redis_key = "#{hash_key}:#{key}:#{gran}:#{tsround}" ts = getFactoredTimestamp time, factor connection.pipelined{ connection.hincrby redis_key, ts, 1 connection.expireat redis_key, tsround + ttl } end end
recordHitNow(key)
click to toggle source
# File lib/snappy_stats.rb, line 64 def recordHitNow(key) recordHit(Time.now.utc.to_i, key) end