Module: SnappyStats
- Defined in:
- lib/snappy_stats.rb,
lib/snappy_stats/config.rb,
lib/snappy_stats/version.rb
Defined Under Namespace
Modules: Config
Constant Summary collapse
- GRANULARITIES =
{ # Available for 24 hours minute: { size: 1440, ttl: 172800, factor: 60 }, hour: { # Available for 7 days size: 168, ttl: 1209600, factor: 3600 }, day: { # Available for 24 months size: 365, ttl: 63113880, factor: 86400 } }
- VERSION =
"0.0.1"
Class Method Summary collapse
- .config ⇒ Object
- .configure {|config| ... } ⇒ Object
- .connection ⇒ Object
- .get(gran, from, to, key) ⇒ Object
- .getFactoredTimestamp(ts_seconds, factor) ⇒ Object
- .getRoundedTimestamp(ts, precision) ⇒ Object
- .getSecondsTimestamp ⇒ Object
- .hash_key ⇒ Object
- .recordHit(time, key) ⇒ Object
- .recordHitNow(key) ⇒ Object
Class Method Details
.config ⇒ Object
38 39 40 |
# File 'lib/snappy_stats.rb', line 38 def self.config Config end |
.configure {|config| ... } ⇒ Object
34 35 36 |
# File 'lib/snappy_stats.rb', line 34 def self.configure yield(config) end |
.connection ⇒ Object
42 43 44 |
# File 'lib/snappy_stats.rb', line 42 def self.connection @connection ||= config.redis end |
.get(gran, from, to, key) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/snappy_stats.rb', line 81 def self.get(gran, from, to, key) granularity = GRANULARITIES[gran] size = granularity[:size] factor = granularity[:factor] from = self.getFactoredTimestamp( from, factor ) to = self.getFactoredTimestamp( to, factor ) ts = from i = 0 results = {} while ts <= to tsround = getRoundedTimestamp( ts, size * factor ) redis_key = "#{hash_key}:#{key}:#{gran}:#{tsround}" results[ts] = SnappyStats.connection.hget( redis_key, ts ) i = i+1 ts = ts + GRANULARITIES[gran][:factor] end results end |
.getFactoredTimestamp(ts_seconds, factor) ⇒ Object
56 57 58 59 |
# File 'lib/snappy_stats.rb', line 56 def self.getFactoredTimestamp( ts_seconds, factor ) ts_seconds = ts_seconds || self.getSecondsTimestamp ( ts_seconds / factor ).floor * factor end |
.getRoundedTimestamp(ts, precision) ⇒ Object
50 51 52 53 54 |
# File 'lib/snappy_stats.rb', line 50 def self.getRoundedTimestamp( ts , precision ) ts = ts || self.getSecondsTimestamp precision = precision || 1 ( ts / precision ).floor * precision end |
.getSecondsTimestamp ⇒ Object
46 47 48 |
# File 'lib/snappy_stats.rb', line 46 def self.getSecondsTimestamp Time.now.getutc.to_i end |
.hash_key ⇒ Object
30 31 32 |
# File 'lib/snappy_stats.rb', line 30 def self.hash_key "#{SnappyStats.config.namespace}" end |
.recordHit(time, key) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/snappy_stats.rb', line 65 def self.recordHit( time, key ) GRANULARITIES.keys.each do | gran | granularity = GRANULARITIES[gran] size = granularity[:size] factor = granularity[:factor] ttl = granularity[:ttl] tsround = SnappyStats.getRoundedTimestamp(time, size * factor) redis_key = "#{hash_key}:#{key}:#{gran}:#{tsround}" ts = getFactoredTimestamp time, factor val = SnappyStats.connection.hincrby redis_key, ts, 1 puts val puts "#{redis_key}, #{ts},#{SnappyStats.connection.hgetall(redis_key)}" SnappyStats.connection.expireat redis_key, tsround + ttl end end |
.recordHitNow(key) ⇒ Object
61 62 63 |
# File 'lib/snappy_stats.rb', line 61 def self.recordHitNow(key) self.recordHit(Time.now.utc.to_i, key) end |