cleaner code in Redistat::Finder

This commit is contained in:
2010-10-18 00:58:11 +01:00
parent 5299b64fbc
commit 44ea115ca7
4 changed files with 41 additions and 39 deletions

View File

@@ -14,6 +14,7 @@ require 'redistat/date'
require 'redistat/event' require 'redistat/event'
require 'redistat/finder' require 'redistat/finder'
require 'redistat/finder/date_set' require 'redistat/finder/date_set'
require 'redistat/hash'
require 'redistat/key' require 'redistat/key'
require 'redistat/label' require 'redistat/label'
require 'redistat/model' require 'redistat/model'

View File

@@ -18,44 +18,31 @@ module Redistat
return nil if !valid_options? return nil if !valid_options?
sets = Finder::DateSet.new(@options[:from], @options[:till], @options[:depth], @options[:interval]) sets = Finder::DateSet.new(@options[:from], @options[:till], @options[:depth], @options[:interval])
key = Key.new(@options[:scope], @options[:label]) key = Key.new(@options[:scope], @options[:label])
sum = {} total_sum = Hash.new
sets.each do |set| sets.each do |set|
set_sum = summarize_add_keys(set[:add], key, {}) sum = Hash.new
set_sum = summarize_sub_keys(set[:sub], key, set_sum) sum = summarize_add_keys(set[:add], key, sum)
set_sum.each do |k, v| sum = summarize_rem_keys(set[:rem], key, sum)
if sum.has_key?(k) sum.each do |k, v|
sum[k] += v.to_i total_sum.set_or_incr(k, v.to_i)
else end
sum[k] = v.to_i end
end total_sum
end
def summarize_add_keys(sets, key, sum)
sets.each do |date|
db.hgetall("#{key.prefix}#{date}").each do |k, v|
sum.set_or_incr(k, v.to_i)
end end
end end
sum sum
end end
def summarize_add_keys(the_sets, key, sum) def summarize_rem_keys(sets, key, sum)
the_sets.each do |date| sets.each do |date|
stat = db.hgetall("#{key.prefix}#{date}") db.hgetall("#{key.prefix}#{date}").each do |k, v|
stat.each do |k, v| sum.set_or_incr(k, -v.to_i)
if sum.has_key?(k)
sum[k] += v.to_i
else
sum[k] = v.to_i
end
end
end
sum
end
def summarize_sub_keys(the_sets, key, sum)
the_sets.each do |date|
stat = db.hgetall("#{key.prefix}#{date}")
stat.each do |k, v|
if sum.has_key?(k)
sum[k] -= v.to_i
else
sum[k] = -v.to_i
end
end end
end end
sum sum

11
lib/redistat/hash.rb Normal file
View File

@@ -0,0 +1,11 @@
module Redistat
class Hash < ::Hash
def set_or_incr(key, value)
self[key] = 0 if !self.has_key?(key)
self[key] += value
self
end
end
end

View File

@@ -5,6 +5,11 @@ describe Redistat::Finder do
before(:each) do before(:each) do
db.flushdb 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 end
it "should initialize properly" do it "should initialize properly" do
@@ -41,12 +46,6 @@ describe Redistat::Finder do
it "should fetch stats properly" do it "should fetch stats properly" do
# pending "needs reimplementation" # pending "needs reimplementation"
@scope = "PageViews"
@label = "about_us"
@date = Time.now
@key = Redistat::Key.new(@scope, @label, @date, {:depth => :day})
@stats = {"views" => 3, "visitors" => 2}
key = Redistat::Key.new(@scope, @label, 2.hours.ago) key = Redistat::Key.new(@scope, @label, 2.hours.ago)
Redistat::Summary.update(key, @stats, :hour) Redistat::Summary.update(key, @stats, :hour)
key = Redistat::Key.new(@scope, @label, 1.hours.ago) key = Redistat::Key.new(@scope, @label, 1.hours.ago)
@@ -54,10 +53,14 @@ describe Redistat::Finder do
key = Redistat::Key.new(@scope, @label, 24.minutes.ago) key = Redistat::Key.new(@scope, @label, 24.minutes.ago)
Redistat::Summary.update(key, @stats, :hour) Redistat::Summary.update(key, @stats, :hour)
# stats = Redistat::Summary.find(key, 3.hours.ago, 2.hour.from_now, :hour)
stats = Redistat::Finder.find({:from => 3.hours.ago, :till => 2.hours.from_now, :scope => @scope, :label => @label, :depth => :hour}) stats = Redistat::Finder.find({:from => 3.hours.ago, :till => 2.hours.from_now, :scope => @scope, :label => @label, :depth => :hour})
stats.should == { "views" => 9, "visitors" => 6 } stats.should == { "views" => 9, "visitors" => 6 }
end end
it "should return empty hash when attempting to fetch non-existent results" do
stats = Redistat::Finder.find({:from => 3.hours.ago, :till => 2.hours.from_now, :scope => @scope, :label => @label, :depth => :hour})
stats.should == {}
end
end end