created Redistat::Summary object

This commit is contained in:
2010-07-23 23:27:55 +03:00
parent 0f11eb66e1
commit 1c818024e5
3 changed files with 84 additions and 0 deletions

View File

@@ -12,6 +12,7 @@ require "redistat/key"
require "redistat/label"
require "redistat/model"
require "redistat/scope"
require "redistat/summary"
module Redistat

29
lib/redistat/summary.rb Normal file
View File

@@ -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

54
spec/summary_spec.rb Normal file
View File

@@ -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