mirror of
https://github.com/jimeh/redistat.git
synced 2026-02-19 05:16:39 +00:00
cleaner code in Redistat::Finder
This commit is contained in:
@@ -14,6 +14,7 @@ require 'redistat/date'
|
||||
require 'redistat/event'
|
||||
require 'redistat/finder'
|
||||
require 'redistat/finder/date_set'
|
||||
require 'redistat/hash'
|
||||
require 'redistat/key'
|
||||
require 'redistat/label'
|
||||
require 'redistat/model'
|
||||
|
||||
@@ -18,44 +18,31 @@ module Redistat
|
||||
return nil if !valid_options?
|
||||
sets = Finder::DateSet.new(@options[:from], @options[:till], @options[:depth], @options[:interval])
|
||||
key = Key.new(@options[:scope], @options[:label])
|
||||
sum = {}
|
||||
total_sum = Hash.new
|
||||
sets.each do |set|
|
||||
set_sum = summarize_add_keys(set[:add], key, {})
|
||||
set_sum = summarize_sub_keys(set[:sub], key, set_sum)
|
||||
set_sum.each do |k, v|
|
||||
if sum.has_key?(k)
|
||||
sum[k] += v.to_i
|
||||
else
|
||||
sum[k] = v.to_i
|
||||
end
|
||||
sum = Hash.new
|
||||
sum = summarize_add_keys(set[:add], key, sum)
|
||||
sum = summarize_rem_keys(set[:rem], key, sum)
|
||||
sum.each do |k, v|
|
||||
total_sum.set_or_incr(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
|
||||
sum
|
||||
end
|
||||
|
||||
def summarize_add_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
|
||||
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
|
||||
def summarize_rem_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
|
||||
sum
|
||||
|
||||
11
lib/redistat/hash.rb
Normal file
11
lib/redistat/hash.rb
Normal 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
|
||||
@@ -5,6 +5,11 @@ describe Redistat::Finder 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 initialize properly" do
|
||||
@@ -41,12 +46,6 @@ describe Redistat::Finder do
|
||||
it "should fetch stats properly" do
|
||||
# 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)
|
||||
Redistat::Summary.update(key, @stats, :hour)
|
||||
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)
|
||||
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.should == { "views" => 9, "visitors" => 6 }
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user