added key grouping for statistics Hash

Example:
store(“message”, {“count/private” => 1})
store(“message”, {“count/public” => 1})
fetch("message", 2.minutes.ago, Time.now)
  #=> { "count" => 2,
        "count/private" => 1,
        "count/public" => 1 }
This commit is contained in:
2011-03-04 12:54:50 +00:00
parent 968ef47ac5
commit 7b1feda061
3 changed files with 43 additions and 0 deletions

View File

@@ -36,6 +36,7 @@ module Redistat
KEY_EVENT = ".event:"
KEY_LEBELS = "Redistat.lables:"
KEY_EVENT_IDS = ".event_ids"
GROUP_SEPARATOR = "/"
class InvalidOptions < ArgumentError; end
class RedisServerIsTooOld < Exception; end

View File

@@ -4,6 +4,7 @@ module Redistat
def self.update_all(key, stats = {}, depth_limit = nil, connection_ref = nil)
stats ||= {}
stats = inject_group_summaries(stats)
depth_limit ||= key.depth
return nil if stats.size == 0
Date::DEPTHS.each do |depth|
@@ -20,5 +21,25 @@ module Redistat
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

View File

@@ -46,4 +46,25 @@ describe Redistat::Summary do
end
end
it "should inject stats key grouping summaries" do
hash = { "count/hello" => 3, "count/world" => 7,
"death/bomb" => 4, "death/unicorn" => 3,
:"od/sugar" => 7, :"od/meth" => 8 }
res = Redistat::Summary.send(:inject_group_summaries, hash)
res.should == { "count" => 10, "count/hello" => 3, "count/world" => 7,
"death" => 7, "death/bomb" => 4, "death/unicorn" => 3,
"od" => 15, :"od/sugar" => 7, :"od/meth" => 8 }
end
it "should properly store key group summaries" do
stats = {"views" => 3, "visitors/eu" => 2, "visitors/us" => 4}
Redistat::Summary.update_all(@key, stats, :hour)
summary = db.hgetall(@key.to_s(:hour))
summary.should have(4).items
summary["views"].should == "3"
summary["visitors"].should == "6"
summary["visitors/eu"].should == "2"
summary["visitors/us"].should == "4"
end
end