lib/rhcf/timeseries/redis.rb in rhcf-timeseries-0.0.3 vs lib/rhcf/timeseries/redis.rb in rhcf-timeseries-0.0.4
- old
+ new
@@ -1,8 +1,5 @@
-#require 'active_support/core_ext/numeric/time'
-
-require 'micon'
require 'date'
class Fixnum
def minutes
self * 60
@@ -40,11 +37,11 @@
module Timeseries
class Result
def initialize(subject, from, to, series)
if from > to
- raise ArgumentError, "Argument 'from' can not be bigger then 'to'"
+ fail ArgumentError, "Argument 'from' can not be bigger then 'to'"
end
@series = series
@subject = subject
@from = from
@to = to
@@ -98,17 +95,16 @@
def better_resolution
span = @to - @from
resolutions = @series.resolutions.sort_by{|h| h[:span]}.reverse
better = resolutions.find{|r| r[:span] < span / 5} || resolutions.last
+ return better
end
end
class Redis
- inject :logger
- inject :redis_connection
RESOLUTIONS_MAP={
:ever => {span:Float::INFINITY, formatter: "ever", ttl: (2 * 366).days},
:year => {span: 365.days,formatter: "%Y", ttl: (2 * 366).days},
:week => {span: 1.week, formatter: "%Y-CW%w", ttl: 90.days},
@@ -121,29 +117,30 @@
:"5minutes" => {span: 5.minutes, formatter: ->(time){ [time.strftime("%Y-%m-%dT%H:") , (time.to_i/60) % 60/5, '*',5].join('') }, ttl: 3.hour},
:"15minutes" => {span: 15.minutes, formatter: ->(time){ [time.strftime("%Y-%m-%dT%H:") , (time.to_i/60) % 60/15, '*',15].join('') }, ttl: 24.hours}
}
DEFAULT_RESOLUTIONS = RESOLUTIONS_MAP.keys
- DEFAULT_MAX_POINTS = 1_024
NAMESPACE_SEPARATOR = '|'
+ attr_reader :logger
- def initialize(options = {})
+ def initialize(logger, redis, options = {})
@resolution_ids = options[:resolutions] || DEFAULT_RESOLUTIONS
- @max_points = options[:max_points] || DEFAULT_MAX_POINTS
@prefix = options[:prefix] || self.class.name
- @connection_to_use = nil
+ @logger = logger
+ @connection_to_use = redis
end
def on_connection(conn)
+ old_connection = @connection_to_use
@connection_to_use = conn
yield self
- @connection_to_use = nil
+ @connection_to_use = old_connection
end
def redis_connection_to_use
- @connection_to_use || redis_connection
+ @connection_to_use || fail("No redis connection given")
end
def store(subject, event_point_hash, moment = Time.now)
resolutions = resolutions_of(moment)
@@ -178,11 +175,11 @@
when String
moment.strftime(time_resolution_formater)
when Proc
time_resolution_formater.call(moment)
else
- raise ArgumentError, "Unexpected moment formater type #{time_resolution_formater.class}"
+ fail ArgumentError, "Unexpected moment formater type #{time_resolution_formater.class}"
end
end
def descend(path, &block)
return if path.empty? or path == "."
@@ -224,11 +221,11 @@
logger.debug("DELETING KEY #{a_key}")
redis_connection_to_use.del(a_key)
end
def keys(*a_key)
- raise "GIVEUP"
+ fail "GIVEUP"
a_key = [@prefix, a_key].flatten.join(NAMESPACE_SEPARATOR)
logger.debug("FINDING KEY #{a_key}")
redis_connection_to_use.keys(a_key).collect{|k| k.split(NAMESPACE_SEPARATOR)[1,1000].join(NAMESPACE_SEPARATOR) }
end
@@ -239,10 +236,10 @@
end
def resolution(id)
res = RESOLUTIONS_MAP[id]
- raise ArgumentError, "Invalid resolution name #{id} for this time series" if res.nil?
+ fail ArgumentError, "Invalid resolution name #{id} for this time series" if res.nil?
res.merge(:id => id)
end
def resolutions
@resolution_ids.collect do |id|
resolution(id)