mirror of
https://github.com/jimeh/redistat.git
synced 2026-02-19 13:26:39 +00:00
added label grouping to Key and Summary classes
This commit is contained in:
@@ -39,10 +39,20 @@ module Redistat
|
|||||||
@label.hash
|
@label.hash
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def label_groups
|
||||||
|
@label.groups
|
||||||
|
end
|
||||||
|
|
||||||
def label=(input)
|
def label=(input)
|
||||||
@label = (input.instance_of?(Redistat::Label)) ? input : Label.create(input, @options)
|
@label = (input.instance_of?(Redistat::Label)) ? input : Label.create(input, @options)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def groups
|
||||||
|
@groups ||= label_groups.map do |label_name|
|
||||||
|
self.class.new(@scope, label_name, self.date, @options)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def to_s(depth = nil)
|
def to_s(depth = nil)
|
||||||
depth ||= @options[:depth]
|
depth ||= @options[:depth]
|
||||||
key = self.prefix
|
key = self.prefix
|
||||||
|
|||||||
@@ -4,18 +4,30 @@ module Redistat
|
|||||||
|
|
||||||
def self.update_all(key, stats = {}, depth_limit = nil, connection_ref = nil, enable_grouping = nil)
|
def self.update_all(key, stats = {}, depth_limit = nil, connection_ref = nil, enable_grouping = nil)
|
||||||
stats ||= {}
|
stats ||= {}
|
||||||
enable_grouping = true if enable_grouping.nil?
|
|
||||||
stats = inject_group_summaries(stats) if enable_grouping
|
|
||||||
depth_limit ||= key.depth
|
|
||||||
return nil if stats.size == 0
|
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|
|
Date::DEPTHS.each do |depth|
|
||||||
update(key, stats, depth, connection_ref)
|
update(key, stats, depth, connection_ref)
|
||||||
break if depth == depth_limit
|
break if depth == depth_limit
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def self.update(key, stats, depth, connection_ref = nil)
|
def self.update(key, stats, depth, connection_ref = nil)
|
||||||
stats.each do |field, value|
|
stats.each do |field, value|
|
||||||
db(connection_ref).hincrby key.to_s(depth), field, value
|
db(connection_ref).hincrby key.to_s(depth), field, value
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ describe Redistat::Key do
|
|||||||
@key.scope.should == @scope
|
@key.scope.should == @scope
|
||||||
@key.label.should == @label
|
@key.label.should == @label
|
||||||
@key.label_hash.should == @label_hash
|
@key.label_hash.should == @label_hash
|
||||||
|
@key.label_groups.should == @key.instance_variable_get("@label").groups
|
||||||
@key.date.should be_instance_of(Redistat::Date)
|
@key.date.should be_instance_of(Redistat::Date)
|
||||||
@key.date.to_time.to_s.should == @date.to_s
|
@key.date.to_time.to_s.should == @date.to_s
|
||||||
end
|
end
|
||||||
@@ -60,4 +61,16 @@ describe Redistat::Key do
|
|||||||
@key.label_hash == @label_hash
|
@key.label_hash == @label_hash
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should create a group of keys from label group" do
|
||||||
|
label = 'message/public/offensive'
|
||||||
|
result = [ "message/public/offensive",
|
||||||
|
"message/public",
|
||||||
|
"message" ]
|
||||||
|
|
||||||
|
key = Redistat::Key.new(@scope, label, @date, {:depth => :hour})
|
||||||
|
|
||||||
|
key.label_groups.should == result
|
||||||
|
key.groups.map { |k| k.label }.should == result
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
@@ -52,8 +52,8 @@ describe Redistat::Summary do
|
|||||||
:"od/sugar" => 7, :"od/meth" => 8 }
|
:"od/sugar" => 7, :"od/meth" => 8 }
|
||||||
res = Redistat::Summary.send(:inject_group_summaries, hash)
|
res = Redistat::Summary.send(:inject_group_summaries, hash)
|
||||||
res.should == { "count" => 10, "count/hello" => 3, "count/world" => 7,
|
res.should == { "count" => 10, "count/hello" => 3, "count/world" => 7,
|
||||||
"death" => 7, "death/bomb" => 4, "death/unicorn" => 3,
|
"death" => 7, "death/bomb" => 4, "death/unicorn" => 3,
|
||||||
"od" => 15, :"od/sugar" => 7, :"od/meth" => 8 }
|
"od" => 15, :"od/sugar" => 7, :"od/meth" => 8 }
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should properly store key group summaries" do
|
it "should properly store key group summaries" do
|
||||||
@@ -77,4 +77,47 @@ describe Redistat::Summary do
|
|||||||
summary["visitors/us"].should == "4"
|
summary["visitors/us"].should == "4"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should store label-based grouping enabled stats" do
|
||||||
|
stats = {"views" => 3, "visitors/eu" => 2, "visitors/us" => 4}
|
||||||
|
label = "views/about_us"
|
||||||
|
key = Redistat::Key.new(@scope, label, @date)
|
||||||
|
Redistat::Summary.update_all(key, stats, :hour)
|
||||||
|
|
||||||
|
key.groups[0].label.should == "views/about_us"
|
||||||
|
key.groups[1].label.should == "views"
|
||||||
|
child1 = key.groups[0]
|
||||||
|
parent = key.groups[1]
|
||||||
|
|
||||||
|
label = "views/contact"
|
||||||
|
key = Redistat::Key.new(@scope, label, @date)
|
||||||
|
Redistat::Summary.update_all(key, stats, :hour)
|
||||||
|
|
||||||
|
key.groups[0].label.should == "views/contact"
|
||||||
|
key.groups[1].label.should == "views"
|
||||||
|
child2 = key.groups[0]
|
||||||
|
|
||||||
|
summary = db.hgetall(child1.to_s(:hour))
|
||||||
|
summary["views"].should == "3"
|
||||||
|
summary["visitors/eu"].should == "2"
|
||||||
|
summary["visitors/us"].should == "4"
|
||||||
|
|
||||||
|
summary = db.hgetall(child2.to_s(:hour))
|
||||||
|
summary["views"].should == "3"
|
||||||
|
summary["visitors/eu"].should == "2"
|
||||||
|
summary["visitors/us"].should == "4"
|
||||||
|
|
||||||
|
summary = db.hgetall(parent.to_s(:hour))
|
||||||
|
summary["views"].should == "6"
|
||||||
|
summary["visitors/eu"].should == "4"
|
||||||
|
summary["visitors/us"].should == "8"
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user