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)