diff --git a/lib/redistat.rb b/lib/redistat.rb index d718fe3..ae9fd8d 100644 --- a/lib/redistat.rb +++ b/lib/redistat.rb @@ -31,6 +31,8 @@ module Redistat KEY_EVENT = ".event:" KEY_LEBELS = "Redistat.lables:" KEY_EVENT_IDS = ".event_ids" + + class InvalidOptions < ArgumentError; end # Provides access to the Redis database. This is shared accross all models and instances. def redis diff --git a/lib/redistat/collection.rb b/lib/redistat/collection.rb new file mode 100644 index 0000000..eea6891 --- /dev/null +++ b/lib/redistat/collection.rb @@ -0,0 +1,7 @@ +module Redistat + class Collection < Array + + + + end +end \ No newline at end of file diff --git a/lib/redistat/finder.rb b/lib/redistat/finder.rb index 83752e5..b49ba27 100644 --- a/lib/redistat/finder.rb +++ b/lib/redistat/finder.rb @@ -15,11 +15,28 @@ module Redistat def find(options = {}) @options.merge!(options) - return nil if !valid_options? - sets = Finder::DateSet.new(@options[:from], @options[:till], @options[:depth], @options[:interval]) + raise InvalidOptions.new if !valid_options? + if @options[:interval].nil? || !@options[:interval] + find_by_magic + else + find_by_interval + end + end + + def find_by_interval(options = {}) + @options.merge!(options) + raise InvalidOptions.new if !valid_options? + date_sets = Finder::DateSet.new(@options[:from], @options[:till], @options[:depth], @options[:interval]) + + end + + def find_by_magic(options = {}) + @options.merge!(options) + raise InvalidOptions.new if !valid_options? + date_sets = Finder::DateSet.new(@options[:from], @options[:till], @options[:depth], @options[:interval]) key = Key.new(@options[:scope], @options[:label]) total_sum = Result.new - sets.each do |set| + date_sets.each do |set| sum = Result.new sum = summarize_add_keys(set[:add], key, sum) sum = summarize_rem_keys(set[:rem], key, sum) diff --git a/spec/finder_spec.rb b/spec/finder_spec.rb index 6f2a90f..3c76eb1 100644 --- a/spec/finder_spec.rb +++ b/spec/finder_spec.rb @@ -44,12 +44,7 @@ describe Redistat::Finder do end it "should fetch stats properly" do - 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) + create_example_stats three_hours_ago = 3.hours.ago two_hours_from_now = 2.hours.from_now @@ -67,7 +62,34 @@ describe Redistat::Finder do end it "should fetch data per unit when interval option is specified" do + create_example_stats + three_hours_ago = 3.hours.ago + two_hours_from_now = 2.hours.from_now + depth = :hour + + stats = Redistat::Finder.find(:from => 3.hours.ago, :till => 2.hours.ago, :scope => @scope, :label => @label, :depth => :hour, :interval => :hour) + puts "\n>>>>>> stats: " + stats.inspect + "\n" + end + + it "should throw error on invalid options" do + begin + stats = Redistat::Finder.find(:from => 3.hours.ago) + rescue ArgumentError => e + e.class.to_s.should == "Redistat::InvalidOptions" + end + end + + + # helper methods + + def create_example_stats + 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) end end