finder and it's specs work!

This commit is contained in:
2010-10-15 01:47:16 +01:00
parent f2b0bd7f47
commit b6aa9c65ac
2 changed files with 80 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
module Redistat
class Finder
include Database
attr_reader :options
@@ -7,16 +8,64 @@ module Redistat
@options = options
end
def builder
end
def valid_options?
return true if !@options[:scope].blank? && !@options[:label].blank? && !@options[:from].blank? && !@options[:till].blank?
false
end
def find(options = {})
@options.merge!(options)
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 = {}
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
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
end
end
sum
end
class << self
def find(*args)
new.find(*args)
end
def scope(scope)
new.scope(scope)

View File

@@ -1,6 +1,11 @@
require "spec_helper"
describe Redistat::Finder do
include Redistat::Database
before(:each) do
db.flushdb
end
it "should initialize properly" do
two_hours_ago = 2.hours.ago
@@ -33,4 +38,26 @@ describe Redistat::Finder do
end
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)
Redistat::Summary.update(key, @stats, :hour)
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
end