From 44ea115ca7af2b55da8daaff43f24cefeefa98d7 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Mon, 18 Oct 2010 00:58:11 +0100 Subject: [PATCH] cleaner code in Redistat::Finder --- lib/redistat.rb | 1 + lib/redistat/finder.rb | 51 ++++++++++++++++-------------------------- lib/redistat/hash.rb | 11 +++++++++ spec/finder_spec.rb | 17 ++++++++------ 4 files changed, 41 insertions(+), 39 deletions(-) create mode 100644 lib/redistat/hash.rb diff --git a/lib/redistat.rb b/lib/redistat.rb index df7e130..2d6cd95 100644 --- a/lib/redistat.rb +++ b/lib/redistat.rb @@ -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' diff --git a/lib/redistat/finder.rb b/lib/redistat/finder.rb index 10d2d19..0bb9807 100644 --- a/lib/redistat/finder.rb +++ b/lib/redistat/finder.rb @@ -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 diff --git a/lib/redistat/hash.rb b/lib/redistat/hash.rb new file mode 100644 index 0000000..5192e0d --- /dev/null +++ b/lib/redistat/hash.rb @@ -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 \ No newline at end of file diff --git a/spec/finder_spec.rb b/spec/finder_spec.rb index 5ac6e50..76642e5 100644 --- a/spec/finder_spec.rb +++ b/spec/finder_spec.rb @@ -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 \ No newline at end of file