lib/redistat/summary.rb in redistat-0.0.9 vs lib/redistat/summary.rb in redistat-0.1.0
- old
+ new
@@ -1,24 +1,58 @@
module Redistat
class Summary
include Database
- def self.update_all(key, stats = {}, depth_limit = nil, connection_ref = nil)
+ def self.update_all(key, stats = {}, depth_limit = nil, connection_ref = nil, enable_grouping = nil)
stats ||= {}
- depth_limit ||= key.depth
return nil if stats.size == 0
+
+ depth_limit ||= key.depth
+ enable_grouping = true if enable_grouping.nil?
+
+ if enable_grouping
+ stats = inject_group_summaries(stats)
+ key.groups.each { |k|
+ update_key(k, stats, depth_limit, connection_ref)
+ }
+ else
+ update_key(key, stats, depth_limit, connection_ref)
+ end
+ end
+
+ private
+
+ def self.update_key(key, stats, depth_limit, connection_ref)
Date::DEPTHS.each do |depth|
update(key, stats, depth, connection_ref)
break if depth == depth_limit
end
end
- private
-
def self.update(key, stats, depth, connection_ref = nil)
stats.each do |field, value|
db(connection_ref).hincrby key.to_s(depth), field, value
end
+ end
+
+ def self.inject_group_summaries!(stats)
+ stats.each do |key, value|
+ parts = key.to_s.split(GROUP_SEPARATOR)
+ parts.pop
+ if parts.size > 0
+ sum_parts = []
+ parts.each do |part|
+ sum_parts << part
+ sum_key = sum_parts.join(GROUP_SEPARATOR)
+ (stats.has_key?(sum_key)) ? stats[sum_key] += value : stats[sum_key] = value
+ end
+ end
+ end
+ stats
+ end
+
+ def self.inject_group_summaries(stats)
+ inject_group_summaries!(stats.clone)
end
end
end
\ No newline at end of file