lib/rhcf/timeseries/redis.rb in rhcf-timeseries-0.0.5 vs lib/rhcf/timeseries/redis.rb in rhcf-timeseries-0.0.6
- old
+ new
@@ -103,14 +103,18 @@
rescue FloatDomainError
# OK
end
def better_resolution
- span = @to - @from
+ span = @to.to_time - @from.to_time
+
resolutions = @series.resolutions.sort_by{|h| h[:span]}.reverse
- better = resolutions.find{|r| r[:span] < span / 5} || resolutions.last
- return better
+ 5.downto(1) do |div|
+ res = resolutions.find{|r| r[:span] < span / div }
+ return res if res
+ end
+ return nil
end
end
class Redis
@@ -150,27 +154,25 @@
def redis_connection_to_use
@connection_to_use || fail("No redis connection given")
end
- def store(subject, event_point_hash, moment = Time.now)
+ def store(subject, event_point_hash, moment = Time.now, descend_subject = true, descend_event = true)
resolutions = resolutions_of(moment)
- descend(subject) do |subject_path|
+ descend(subject, descend_subject) do |subject_path|
event_point_hash.each do |event, point_value|
- descend(event) do |event_path|
+ descend(event, descend_event) do |event_path|
resolutions.each do |res|
resolution_name, resolution_value = *res
store_point_value(subject_path, event_path, resolution_name, resolution_value, point_value)
end
end
end
end
end
-
-
def resolutions_of(moment)
@resolution_ids.collect do |res_id|
[res_id, resolution_value_at(moment, res_id)]
end
end
@@ -190,14 +192,14 @@
else
fail ArgumentError, "Unexpected moment formater type #{time_resolution_formater.class}"
end
end
- def descend(path, &block)
+ def descend(path, do_descend = true , &block)
return if path.empty? or path == "."
block.call(path)
- descend(File.dirname(path), &block)
+ descend(File.dirname(path), do_descend, &block) if do_descend
end
def store_point_event( resolution_name, resolution_value, subject_path, event_path)
key = [@prefix, 'event_set', resolution_name, resolution_value, subject_path].join(NAMESPACE_SEPARATOR)
logger.debug("EVENTSET SADD #{key} -> #{event_path}")
@@ -250,15 +252,13 @@
def resolution(id)
res = RESOLUTIONS_MAP[id]
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)
- end
+ @_resolutions ||= @resolution_ids.map { |id| resolution(id) }
end
-
def events_for_subject_on(subject, point, resolution_id)
key = [@prefix, 'event_set', resolution_id, point, subject].join(NAMESPACE_SEPARATOR)
logger.debug("EVENTSET SMEMBERS #{key}")
redis_connection_to_use.smembers(key)