From 1c818024e5f69667738a1fe8e677f2bc9f73939d Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Fri, 23 Jul 2010 23:27:55 +0300 Subject: [PATCH] created Redistat::Summary object --- lib/redistat.rb | 1 + lib/redistat/summary.rb | 29 ++++++++++++++++++++++ spec/summary_spec.rb | 54 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 lib/redistat/summary.rb create mode 100644 spec/summary_spec.rb diff --git a/lib/redistat.rb b/lib/redistat.rb index 0227ca8..331282f 100644 --- a/lib/redistat.rb +++ b/lib/redistat.rb @@ -12,6 +12,7 @@ require "redistat/key" require "redistat/label" require "redistat/model" require "redistat/scope" +require "redistat/summary" module Redistat diff --git a/lib/redistat/summary.rb b/lib/redistat/summary.rb new file mode 100644 index 0000000..53338cc --- /dev/null +++ b/lib/redistat/summary.rb @@ -0,0 +1,29 @@ +module Redistat + class Summary + include Database + extend Database + + def self.update_all(key, stats = {}, depth_limit = nil) + stats ||= {} + depth_limit ||= key.depth + return false if stats.size == 0 + depths.each do |depth| + update(key, stats, depth) + break if depth == depth_limit + end + end + + private + + def self.update(key, stats, depth) + stats.each do |field, value| + db.hincrby key.to_s(depth), field, value + end + end + + def self.depths + [:year, :month, :day, :hour, :min, :sec, :usec] + end + + end +end \ No newline at end of file diff --git a/spec/summary_spec.rb b/spec/summary_spec.rb new file mode 100644 index 0000000..f355863 --- /dev/null +++ b/spec/summary_spec.rb @@ -0,0 +1,54 @@ +require "spec_helper" + +describe Redistat::Label do + + before(:each) do + db.flushdb + @scope = "PageViews" + @label = "about_us" + @date = Time.now + @key = Redistat::Key.new(@scope, @label, @date, {:depth => :day}) + @stats = {"views" => 3, "visitors" => 2} + end + + it "should update a single summary properly" do + Redistat::Summary.update(@key, @stats, :hour) + summary = db.hgetall(@key.to_s(:hour)) + summary.should have(2).items + summary["views"].should == "3" + summary["visitors"].should == "2" + + Redistat::Summary.update(@key, @stats, :hour) + summary = db.hgetall(@key.to_s(:hour)) + summary.should have(2).items + summary["views"].should == "6" + summary["visitors"].should == "4" + + Redistat::Summary.update(@key, {"views" => -4, "visitors" => -3}, :hour) + summary = db.hgetall(@key.to_s(:hour)) + summary.should have(2).items + summary["views"].should == "2" + summary["visitors"].should == "1" + end + + it "should update all summaries properly" do + Redistat::Summary.update_all(@key, @stats, :sec) + [:year, :month, :day, :hour, :min, :sec, :usec].each do |depth| + summary = db.hgetall(@key.to_s(depth)) + if depth != :usec + summary.should have(2).items + summary["views"].should == "3" + summary["visitors"].should == "2" + else + summary.should have(0).items + end + end + end + + it "should fetch summary collections for date ranges" + + def db + Redistat.redis + end + +end \ No newline at end of file