From 83b2b5b528250f6c9327512c2670cac893bdf8c2 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Sun, 7 Nov 2010 21:55:46 +0000 Subject: [PATCH 01/16] renamed/moved Finder::DateSet spec --- spec/{finder_date_set_spec.rb => finder/date_set_spec.rb} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename spec/{finder_date_set_spec.rb => finder/date_set_spec.rb} (100%) diff --git a/spec/finder_date_set_spec.rb b/spec/finder/date_set_spec.rb similarity index 100% rename from spec/finder_date_set_spec.rb rename to spec/finder/date_set_spec.rb From b6ceabf8b3f1b6184912f86a68c4a364d5a12ede Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Sun, 7 Nov 2010 22:30:01 +0000 Subject: [PATCH 02/16] Redistat::Date can now take the output from it's own #to_s method as input. --- lib/redistat/date.rb | 1 + spec/date_spec.rb | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/lib/redistat/date.rb b/lib/redistat/date.rb index e4f7a9b..48fb65e 100644 --- a/lib/redistat/date.rb +++ b/lib/redistat/date.rb @@ -78,6 +78,7 @@ module Redistat end def from_string(input) + input += "19700101000000"[input.size..-1] if input =~ /^\d\d\d[\d]+$/i from_time(::Time.parse(input)) end diff --git a/spec/date_spec.rb b/spec/date_spec.rb index 9c5955f..143481f 100644 --- a/spec/date_spec.rb +++ b/spec/date_spec.rb @@ -31,6 +31,14 @@ describe Redistat::Date do [:year, :month, :day, :hour, :min, :sec].each { |k| rdate.send(k).should == now.send(k) } end + it "should initialize from Redistat date String" do + now = Time.now + rdate = Redistat::Date.new(now.to_s) + [:year, :month, :day, :hour, :min, :sec].each { |k| + rdate.to_s(k).should == Redistat::Date.new(rdate.to_s(k)).to_s(k) + } + end + it "should convert to Time object" do now = Time.now rdate = Redistat::Date.new(now) From 426f00642689f83a59139a5460f5b15e418d0cbf Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Sun, 7 Nov 2010 23:32:32 +0000 Subject: [PATCH 03/16] actually require the collection file --- lib/redistat.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/redistat.rb b/lib/redistat.rb index ae9fd8d..143e38f 100644 --- a/lib/redistat.rb +++ b/lib/redistat.rb @@ -9,6 +9,7 @@ require 'time/ext' require 'json' require 'digest/sha1' +require 'redistat/collection' require 'redistat/database' require 'redistat/date' require 'redistat/event' From 6d04d58d827e23d127232fa3e40591167bf92916 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Sun, 7 Nov 2010 23:37:17 +0000 Subject: [PATCH 04/16] Redistat::Finder returns collections with results within it properly, and all specs passing --- lib/redistat/collection.rb | 11 +++++++- lib/redistat/finder.rb | 38 +++++++++++++++++++------ lib/redistat/result.rb | 11 +++++++- spec/collection_spec.rb | 13 +++++++++ spec/finder_spec.rb | 58 +++++++++++++++++++++++--------------- spec/result_spec.rb | 8 ++++-- 6 files changed, 102 insertions(+), 37 deletions(-) create mode 100644 spec/collection_spec.rb diff --git a/lib/redistat/collection.rb b/lib/redistat/collection.rb index eea6891..a8feaf7 100644 --- a/lib/redistat/collection.rb +++ b/lib/redistat/collection.rb @@ -1,7 +1,16 @@ module Redistat - class Collection < Array + class Collection < ::Array + attr_accessor :from + attr_accessor :till + attr_accessor :depth + attr_accessor :total + def initialize(options = {}) + @from = options[:from] ||= nil + @till = options[:till] ||= nil + @depth = options[:depth] ||= nil + end end end \ No newline at end of file diff --git a/lib/redistat/finder.rb b/lib/redistat/finder.rb index b49ba27..77791ba 100644 --- a/lib/redistat/finder.rb +++ b/lib/redistat/finder.rb @@ -26,27 +26,47 @@ module Redistat 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]) - + key = build_key + col = Collection.new(@options) + col.total = Result.new(@options) + build_date_sets.each do |set| + set[:add].each do |date| + result = Result.new + result.date = Date.new(date).to_time + db.hgetall("#{key.prefix}#{date}").each do |k, v| + result[k] = v + col.total.set_or_incr(k, v.to_i) + end + col << result + end + end + col 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 - date_sets.each do |set| + col = Collection.new(@options) + col.total = Result.new(@options) + col << col.total + build_date_sets.each do |set| sum = Result.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) + col.total.set_or_incr(k, v.to_i) end end - total_sum.date = Date.new(@options[:from], @options[:depth]) - total_sum.till = Date.new(@options[:till], @options[:depth]) - total_sum + col + end + + def build_date_sets + Finder::DateSet.new(@options[:from], @options[:till], @options[:depth], @options[:interval]) + end + + def build_key + Key.new(@options[:scope], @options[:label]) end def summarize_add_keys(sets, key, sum) diff --git a/lib/redistat/result.rb b/lib/redistat/result.rb index 1f658d7..e433055 100644 --- a/lib/redistat/result.rb +++ b/lib/redistat/result.rb @@ -1,9 +1,18 @@ module Redistat class Result < ::Hash - attr_accessor :date + attr_accessor :from attr_accessor :till + alias :date :from + alias :date= :from= + + def initialize(options = {}) + @from = options[:from] ||= nil + @till = options[:till] ||= nil + end + + def set_or_incr(key, value) self[key] = 0 if !self.has_key?(key) self[key] += value diff --git a/spec/collection_spec.rb b/spec/collection_spec.rb new file mode 100644 index 0000000..52b8834 --- /dev/null +++ b/spec/collection_spec.rb @@ -0,0 +1,13 @@ +require "spec_helper" + +describe Redistat::Collection do + + it "should should initialize properly" do + options = {:from => "from", :till => "till", :depth => "depth"} + result = Redistat::Collection.new(options) + result.from.should == options[:from] + result.till.should == options[:till] + result.depth.should == options[:depth] + end + +end \ No newline at end of file diff --git a/spec/finder_spec.rb b/spec/finder_spec.rb index 3c76eb1..bc78fed 100644 --- a/spec/finder_spec.rb +++ b/spec/finder_spec.rb @@ -44,32 +44,41 @@ describe Redistat::Finder do end it "should fetch stats properly" do - create_example_stats + first_stat, last_stat = create_example_stats - three_hours_ago = 3.hours.ago - two_hours_from_now = 2.hours.from_now - depth = :hour + stats = Redistat::Finder.find({:from => first_stat, :till => last_stat, :scope => @scope, :label => @label, :depth => :hour}) + stats.from.should == first_stat + stats.till.should == last_stat + stats.depth.should == :hour - stats = Redistat::Finder.find({:from => three_hours_ago, :till => two_hours_from_now, :scope => @scope, :label => @label, :depth => depth}) - stats.should == { "views" => 9, "visitors" => 6 } - stats.date.to_s.should == three_hours_ago.to_rs.to_s(depth) - stats.till.to_s.should == two_hours_from_now.to_rs.to_s(depth) + stats.total.should == { "views" => 12, "visitors" => 8 } + stats.total.from.should == first_stat + stats.total.till.should == last_stat + stats.first.should == stats.total + end + + it "should fetch data per unit when interval option is specified" do + first_stat, last_stat = create_example_stats + + stats = Redistat::Finder.find(:from => first_stat, :till => last_stat, :scope => @scope, :label => @label, :depth => :hour, :interval => :hour) + stats.from.should == first_stat + stats.till.should == last_stat + stats.total.should == { "views" => 12, "visitors" => 8 } + stats[0].should == {} + stats[0].date.should == Time.parse("2010-05-14 12:00") + stats[1].should == {"visitors"=>"4", "views"=>"6"} + stats[1].date.should == Time.parse("2010-05-14 13:00") + stats[2].should == {"visitors"=>"2", "views"=>"3"} + stats[2].date.should == Time.parse("2010-05-14 14:00") + stats[3].should == {"visitors"=>"2", "views"=>"3"} + stats[3].date.should == Time.parse("2010-05-14 15:00") + stats[4].should == {} + stats[4].date.should == Time.parse("2010-05-14 16:00") 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 - - 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" + stats.total.should == {} end it "should throw error on invalid options" do @@ -84,12 +93,15 @@ describe Redistat::Finder do # helper methods def create_example_stats - key = Redistat::Key.new(@scope, @label, 2.hours.ago) + key = Redistat::Key.new(@scope, @label, (first = Time.parse("2010-05-14 13:43"))) Redistat::Summary.update(key, @stats, :hour) - key = Redistat::Key.new(@scope, @label, 1.hours.ago) + key = Redistat::Key.new(@scope, @label, Time.parse("2010-05-14 13:53")) Redistat::Summary.update(key, @stats, :hour) - key = Redistat::Key.new(@scope, @label, 24.minutes.ago) + key = Redistat::Key.new(@scope, @label, Time.parse("2010-05-14 14:32")) Redistat::Summary.update(key, @stats, :hour) + key = Redistat::Key.new(@scope, @label, (last = Time.parse("2010-05-14 15:02"))) + Redistat::Summary.update(key, @stats, :hour) + [first - 1.hour, last + 1.hour] end end diff --git a/spec/result_spec.rb b/spec/result_spec.rb index e26bb19..d069e12 100644 --- a/spec/result_spec.rb +++ b/spec/result_spec.rb @@ -2,9 +2,11 @@ require "spec_helper" describe Redistat::Result do - before(:each) do - @name = "PageViews" - @scope = Redistat::Scope.new(@name) + it "should should initialize properly" do + options = {:from => "from", :till => "till"} + result = Redistat::Result.new(options) + result.from.should == "from" + result.till.should == "till" end it "should have set_or_incr method" do From c1a8feb008118002444a27237e0d6f22fb9731a2 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Mon, 22 Nov 2010 11:17:19 +0000 Subject: [PATCH 05/16] updated Rakefile for RSpec 2.x --- Rakefile | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Rakefile b/Rakefile index c6c9646..17cf04a 100644 --- a/Rakefile +++ b/Rakefile @@ -24,14 +24,13 @@ rescue LoadError end -require 'spec/rake/spectask' -Spec::Rake::SpecTask.new(:spec) do |spec| - spec.libs << 'lib' << 'spec' - spec.spec_files = FileList['spec/**/*_spec.rb'] +# Rspec +require 'rspec/core/rake_task' +RSpec::Core::RakeTask.new(:spec) do |spec| + spec.pattern = 'spec/**/*_spec.rb' end -Spec::Rake::SpecTask.new(:rcov) do |spec| - spec.libs << 'lib' << 'spec' +RSpec::Core::RakeTask.new(:rcov) do |spec| spec.pattern = 'spec/**/*_spec.rb' spec.rcov = true end From 79648f48c83c300b5cbb12b2d0e8ec5461842502 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Mon, 22 Nov 2010 11:22:15 +0000 Subject: [PATCH 06/16] added .bundle to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 43e89b1..5709751 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ rdoc pkg ## PROJECT::SPECIFIC +.bundle/* .yardoc/* spec/db/* doc From 0e2c336f41a74630cb394a01793a2c3a76b51655 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Mon, 22 Nov 2010 11:22:29 +0000 Subject: [PATCH 07/16] cleaned up Rakefile a bit --- Rakefile | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/Rakefile b/Rakefile index 17cf04a..408da13 100644 --- a/Rakefile +++ b/Rakefile @@ -4,19 +4,18 @@ require 'rake' begin require 'jeweler' Jeweler::Tasks.new do |gem| - gem.name = "redistat" - gem.summary = %Q{TODO: one-line summary of your gem} - gem.description = %Q{TODO: longer description of your gem} - gem.email = "contact@jimeh.me" - gem.homepage = "http://github.com/jimeh/redistat" - gem.authors = ["Jim Myhrberg"] - gem.add_dependency "activesupport", ">= 2.3.0" - gem.add_dependency "json", ">= 1.0.0" - gem.add_dependency "redis", ">= 2.0.0" - gem.add_dependency "time_ext", ">= 0.2.6" - gem.add_development_dependency "rspec", ">= 1.2.9" - gem.add_development_dependency "yard", ">= 0" - # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings + gem.name = 'redistat' + gem.summary = 'TODO: one-line summary of your gem' + gem.description = 'TODO: longer description of your gem' + gem.email = 'contact@jimeh.me' + gem.homepage = 'http://github.com/jimeh/redistat' + gem.authors = ['Jim Myhrberg'] + gem.add_dependency 'activesupport', '>= 2.3.0' + gem.add_dependency 'json', '>= 1.0.0' + gem.add_dependency 'redis', '>= 2.0.0' + gem.add_dependency 'time_ext', '>= 0.2.6' + gem.add_development_dependency 'rspec', '>= 2.0.1' + gem.add_development_dependency 'yard', '>= 0.6.1' end Jeweler::GemcutterTasks.new rescue LoadError From 286b3ef851ba60597bbc3bcae9d89acd69e441fb Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Mon, 22 Nov 2010 11:22:45 +0000 Subject: [PATCH 08/16] added Gemfile for easy installation of all dependencies --- Gemfile | 11 +++++++++++ Gemfile.lock | 29 +++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 Gemfile create mode 100644 Gemfile.lock diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..3e997ea --- /dev/null +++ b/Gemfile @@ -0,0 +1,11 @@ +source 'http://rubygems.org/' + +gem 'activesupport', '>= 2.3.0' +gem 'json', '>= 1.0.0' +gem 'redis', '>= 2.0.0' +gem 'time_ext', '>= 0.2.6' + +group :development do + gem 'rspec', '>= 2.0.1' + gem 'yard', '>= 0.6.1' +end \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..3ec2c87 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,29 @@ +GEM + remote: http://rubygems.org/ + specs: + activesupport (3.0.3) + diff-lcs (1.1.2) + json (1.4.6) + redis (2.1.1) + rspec (2.1.0) + rspec-core (~> 2.1.0) + rspec-expectations (~> 2.1.0) + rspec-mocks (~> 2.1.0) + rspec-core (2.1.0) + rspec-expectations (2.1.0) + diff-lcs (~> 1.1.2) + rspec-mocks (2.1.0) + time_ext (0.2.6) + activesupport (>= 2.3.0) + yard (0.6.3) + +PLATFORMS + ruby + +DEPENDENCIES + activesupport (>= 2.3.0) + json (>= 1.0.0) + redis (>= 2.0.0) + rspec (>= 2.0.1) + time_ext (>= 0.2.6) + yard (>= 0.6.1) From 00131d5ed6277808431c90713e4379be5f8a3264 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Mon, 22 Nov 2010 11:27:10 +0000 Subject: [PATCH 09/16] small spec cleanup --- spec/finder_spec.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/spec/finder_spec.rb b/spec/finder_spec.rb index bc78fed..1200319 100644 --- a/spec/finder_spec.rb +++ b/spec/finder_spec.rb @@ -82,11 +82,7 @@ describe Redistat::Finder do 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 + lambda { Redistat::Finder.find(:from => 3.hours.ago) }.should raise_error(Redistat::InvalidOptions) end From aaf98a8a62ec653c1ea67b17d97cc2b401991204 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Mon, 22 Nov 2010 12:09:36 +0000 Subject: [PATCH 10/16] reordered the argument order to Event.new --- lib/redistat/event.rb | 2 +- spec/event_spec.rb | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/redistat/event.rb b/lib/redistat/event.rb index 9b55933..7ba3967 100644 --- a/lib/redistat/event.rb +++ b/lib/redistat/event.rb @@ -9,7 +9,7 @@ module Redistat attr_accessor :meta attr_accessor :options - def initialize(scope, label = nil, date = nil, stats = {}, meta = {}, options = {}, is_new = true) + def initialize(scope, label = nil, date = nil, stats = {}, options = {}, meta = {}, is_new = true) @options = default_options.merge(options) @key = Key.new(scope, label, date, @options) @stats = stats ||= {} diff --git a/spec/event_spec.rb b/spec/event_spec.rb index 55cd2fa..14fd9e5 100644 --- a/spec/event_spec.rb +++ b/spec/event_spec.rb @@ -12,7 +12,7 @@ describe Redistat::Event do @meta = {:user_id => 239} @options = {:depth => :hour} @date = Time.now - @event = Redistat::Event.new(@scope, @label, @date, @stats, @meta, @options) + @event = Redistat::Event.new(@scope, @label, @date, @stats, @options, @meta) end it "should initialize properly" do @@ -43,7 +43,7 @@ describe Redistat::Event do end it "should increment next_id" do - event = Redistat::Event.new("VisitorCount", @label, @date, @stats, @meta, @options) + event = Redistat::Event.new("VisitorCount", @label, @date, @stats, @options, @meta) @event.next_id.should == 1 event.next_id.should == 1 @event.next_id.should == 2 @@ -51,7 +51,7 @@ describe Redistat::Event do end it "should store event properly" do - @event = Redistat::Event.new(@scope, @label, @date, @stats, @meta, @options.merge({:store_event => true})) + @event = Redistat::Event.new(@scope, @label, @date, @stats, @options.merge({:store_event => true}), @meta) @event.new?.should be_true @event.save @event.new?.should be_false @@ -61,7 +61,7 @@ describe Redistat::Event do end it "should find event by id" do - @event = Redistat::Event.new(@scope, @label, @date, @stats, @meta, @options.merge({:store_event => true})).save + @event = Redistat::Event.new(@scope, @label, @date, @stats, @options.merge({:store_event => true}), @meta).save fetched = Redistat::Event.find(@scope, @event.id) @event.scope.should == fetched.scope @event.label.should == fetched.label @@ -70,7 +70,7 @@ describe Redistat::Event do it "should store summarized statistics" do 2.times do |i| - @event = Redistat::Event.new(@scope, @label, @date, @stats, @meta, @options).save + @event = Redistat::Event.new(@scope, @label, @date, @stats, @options, @meta).save Redistat::Date::DEPTHS.each do |depth| summary = db.hgetall @event.key.to_s(depth) summary.should have_at_least(1).items From 9c850417061b18bca8cfdecb134d860fc7ae0532 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Mon, 22 Nov 2010 12:10:47 +0000 Subject: [PATCH 11/16] Redistat::Model is working --- lib/redistat/model.rb | 45 +++++++++++++++++++++++++++++--- spec/model_helper.rb | 15 +++++++++++ spec/model_spec.rb | 60 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 spec/model_helper.rb create mode 100644 spec/model_spec.rb diff --git a/lib/redistat/model.rb b/lib/redistat/model.rb index 07bd330..677c524 100644 --- a/lib/redistat/model.rb +++ b/lib/redistat/model.rb @@ -1,13 +1,50 @@ module Redistat - class Model + module Model - def self.create(*args) - Event.new(self.name, self.options, *args) + def self.included(base) + base.extend(self) end - def self.options + def store(label, stats = {}, date = nil, meta = {}, opts = {}) + Event.new(name, label, date, stats, options.merge(opts), meta).save + end + alias :event :store + + def fetch(label, from, till, opts = {}) + Finder.find({ + :scope => name, + :label => label, + :from => from, + :till => till + }.merge(options.merge(opts))) + end + alias :find :fetch + + def depth(depth = nil) + if !depth.nil? + options[:depth] = depth + else + options[:depth] || nil + end + end + + def store_event(boolean = nil) + if !boolean.nil? + options[:store_event] = boolean + else + options[:store_event] || nil + end + end + + def options @options ||= {} end + private + + def name + @name ||= self.to_s + end + end end \ No newline at end of file diff --git a/spec/model_helper.rb b/spec/model_helper.rb new file mode 100644 index 0000000..fd31251 --- /dev/null +++ b/spec/model_helper.rb @@ -0,0 +1,15 @@ +require "redistat" + +class ModelHelper + include Redistat::Model + + +end + +class ModelHelper2 + include Redistat::Model + + depth :day + store_event true + +end \ No newline at end of file diff --git a/spec/model_spec.rb b/spec/model_spec.rb new file mode 100644 index 0000000..87fbfdc --- /dev/null +++ b/spec/model_spec.rb @@ -0,0 +1,60 @@ +require "spec_helper" +require "model_helper" + +describe Redistat::Model do + include Redistat::Database + + before(:each) do + db.flushdb + end + + it "should should name itself correctly" do + ModelHelper.send(:name).should == "ModelHelper" + ModelHelper2.send(:name).should == "ModelHelper2" + end + + it "should listen to model-defined options" do + ModelHelper2.depth.should == :day + ModelHelper2.store_event.should == true + + ModelHelper.depth.should == nil + ModelHelper.store_event.should == nil + ModelHelper.depth(:hour) + ModelHelper.depth.should == :hour + ModelHelper.store_event(true) + ModelHelper.store_event.should == true + ModelHelper.options[:depth] = nil + ModelHelper.options[:store_event] = nil + ModelHelper.depth.should == nil + ModelHelper.store_event.should == nil + end + + it "should store and fetch stats" do + ModelHelper.store("sheep.black", {:count => 6, :weight => 461}, 4.hours.ago) + ModelHelper.store("sheep.black", {:count => 2, :weight => 156}) + + stats = ModelHelper.fetch("sheep.black", 2.hours.ago, 1.hour.from_now) + stats.total["count"].should == 2 + stats.total["weight"].should == 156 + stats.first.should == stats.total + + stats = ModelHelper.fetch("sheep.black", 5.hours.ago, 1.hour.from_now) + stats.total["count"].should == 8 + stats.total["weight"].should == 617 + stats.first.should == stats.total + + ModelHelper.store("sheep.white", {:count => 5, :weight => 393}, 4.hours.ago) + ModelHelper.store("sheep.white", {:count => 4, :weight => 316}) + + stats = ModelHelper.fetch("sheep.white", 2.hours.ago, 1.hour.from_now) + stats.total["count"].should == 4 + stats.total["weight"].should == 316 + stats.first.should == stats.total + + stats = ModelHelper.fetch("sheep.white", 5.hours.ago, 1.hour.from_now) + stats.total["count"].should == 9 + stats.total["weight"].should == 709 + stats.first.should == stats.total + end + +end \ No newline at end of file From e6f860a2a68577a3a534f0d06b7de64f43fbdbc1 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Mon, 22 Nov 2010 12:11:09 +0000 Subject: [PATCH 12/16] added i18n to :development group in Gemfile cause rspec was complaining --- Gemfile | 1 + Gemfile.lock | 2 ++ 2 files changed, 3 insertions(+) diff --git a/Gemfile b/Gemfile index 3e997ea..5fafe05 100644 --- a/Gemfile +++ b/Gemfile @@ -8,4 +8,5 @@ gem 'time_ext', '>= 0.2.6' group :development do gem 'rspec', '>= 2.0.1' gem 'yard', '>= 0.6.1' + gem 'i18n' end \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 3ec2c87..0a8a4bc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -3,6 +3,7 @@ GEM specs: activesupport (3.0.3) diff-lcs (1.1.2) + i18n (0.4.2) json (1.4.6) redis (2.1.1) rspec (2.1.0) @@ -22,6 +23,7 @@ PLATFORMS DEPENDENCIES activesupport (>= 2.3.0) + i18n json (>= 1.0.0) redis (>= 2.0.0) rspec (>= 2.0.1) From aaec15880c96f7f63defc06b72ac8299801f743e Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Mon, 22 Nov 2010 12:22:18 +0000 Subject: [PATCH 13/16] active_support/time is already required by time_ext --- lib/redistat.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/redistat.rb b/lib/redistat.rb index 143e38f..68ff931 100644 --- a/lib/redistat.rb +++ b/lib/redistat.rb @@ -1,11 +1,10 @@ require 'rubygems' require 'active_support' -require 'active_support/time' if !Time.respond_to?(:days_in_month) # Active Support 2.x and 3.x require 'redis' require 'date' require 'time' -require 'time/ext' +require 'time_ext' require 'json' require 'digest/sha1' From b554b617f2143b08706f9e081ef52301547da944 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Mon, 22 Nov 2010 12:22:56 +0000 Subject: [PATCH 14/16] Redistat::Result is now a HashWithIndifferentAccess instead of a regular Hash --- lib/redistat.rb | 1 + lib/redistat/result.rb | 2 +- spec/model_spec.rb | 12 ++++++------ 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/redistat.rb b/lib/redistat.rb index 68ff931..4d52389 100644 --- a/lib/redistat.rb +++ b/lib/redistat.rb @@ -1,6 +1,7 @@ require 'rubygems' require 'active_support' +require 'active_support/hash_with_indifferent_access' if !Hash.respond_to?(:with_indifferent_access) # Active Support 2.x and 3.x require 'redis' require 'date' require 'time' diff --git a/lib/redistat/result.rb b/lib/redistat/result.rb index e433055..68dea24 100644 --- a/lib/redistat/result.rb +++ b/lib/redistat/result.rb @@ -1,5 +1,5 @@ module Redistat - class Result < ::Hash + class Result < ::ActiveSupport::HashWithIndifferentAccess attr_accessor :from attr_accessor :till diff --git a/spec/model_spec.rb b/spec/model_spec.rb index 87fbfdc..3894cf8 100644 --- a/spec/model_spec.rb +++ b/spec/model_spec.rb @@ -39,21 +39,21 @@ describe Redistat::Model do stats.first.should == stats.total stats = ModelHelper.fetch("sheep.black", 5.hours.ago, 1.hour.from_now) - stats.total["count"].should == 8 - stats.total["weight"].should == 617 + stats.total[:count].should == 8 + stats.total[:weight].should == 617 stats.first.should == stats.total ModelHelper.store("sheep.white", {:count => 5, :weight => 393}, 4.hours.ago) ModelHelper.store("sheep.white", {:count => 4, :weight => 316}) stats = ModelHelper.fetch("sheep.white", 2.hours.ago, 1.hour.from_now) - stats.total["count"].should == 4 - stats.total["weight"].should == 316 + stats.total[:count].should == 4 + stats.total[:weight].should == 316 stats.first.should == stats.total stats = ModelHelper.fetch("sheep.white", 5.hours.ago, 1.hour.from_now) - stats.total["count"].should == 9 - stats.total["weight"].should == 709 + stats.total[:count].should == 9 + stats.total[:weight].should == 709 stats.first.should == stats.total end From 737e0b7d41b7ca5e43f0fedee59ba170204a8b1d Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Mon, 22 Nov 2010 12:28:17 +0000 Subject: [PATCH 15/16] updated readme --- README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ff2f3ef..caf19e1 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,12 @@ -# redistat +# Redistat -Description goes here. +A Redis-backed statistics storage and querying library written in Ruby. + +## Early Beta + +Currently this is an early beta release. Readme and documentation is forthcoming. + +For now, please check `spec/model_spec.rb` and `spec/model_helper.rb` to get started with how to use Redistat. ## Note on Patches/Pull Requests From dd41e5c7457663260718eaafc9b1f4f1ae0a92a9 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Mon, 22 Nov 2010 12:31:01 +0000 Subject: [PATCH 16/16] Version bump to 0.0.1 --- VERSION | 1 + 1 file changed, 1 insertion(+) create mode 100644 VERSION diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..8acdd82 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +0.0.1