mirror of
https://github.com/jimeh/redistat.git
synced 2026-02-19 05:16:39 +00:00
created Redistat::Summary object
This commit is contained in:
@@ -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
29
lib/redistat/summary.rb
Normal 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
54
spec/summary_spec.rb
Normal 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
|
||||
Reference in New Issue
Block a user