| when end date is specified, data for the corresponding reporting period is now … | |
| Log | |
| Files | |
| Refs | |
| README | |
| --- | |
| commit 929610c2345e54b6f1740f0f2e545440d9c2d3a4 | |
| parent ca12152ec76255deacf609fed517a71f7901013b | |
| Author: Marco Otte-Witte <[email protected]> | |
| Date: Wed, 29 Apr 2009 18:32:17 +0200 | |
| when end date is specified, data for the corresponding reporting period is now … | |
| Diffstat: | |
| M lib/simplabs/reports_as_sparkline/… | 3 ++- | |
| M lib/simplabs/reports_as_sparkline/… | 10 +++++----- | |
| M lib/simplabs/reports_as_sparkline/… | 16 +++++++++++++++- | |
| M spec/classes/report_cache_spec.rb | 2 +- | |
| M spec/classes/report_spec.rb | 94 ++++++++++++++++++++++++++---… | |
| M spec/classes/reporting_period_spec… | 62 +++++++++++++++++++++++++++… | |
| 6 files changed, 159 insertions(+), 28 deletions(-) | |
| --- | |
| diff --git a/lib/simplabs/reports_as_sparkline/report.rb b/lib/simplabs/reports… | |
| @@ -108,7 +108,8 @@ module Simplabs #:nodoc: | |
| raise ArgumentError.new('Options :live_data and :end_date may not bo… | |
| raise ArgumentError.new("Invalid grouping #{options[:grouping]}!") i… | |
| raise ArgumentError.new("Invalid conditions: #{options[:conditions].… | |
| - raise ArgumentError.new("Invalid end date: #{options[:end_date].insp… | |
| + raise ArgumentError.new("Invalid end date: #{options[:end_date].insp… | |
| + raise ArgumentError.new('End date may not be in the future!') if opt… | |
| end | |
| end | |
| diff --git a/lib/simplabs/reports_as_sparkline/report_cache.rb b/lib/simplabs/r… | |
| @@ -10,7 +10,7 @@ module Simplabs #:nodoc: | |
| raise ArgumentError.new('A block must be given') unless block_given? | |
| self.transaction do | |
| cached_data = [] | |
| - first_reporting_period = ReportingPeriod.first(options[:grouping], o… | |
| + first_reporting_period = ReportingPeriod.first(options[:grouping], (… | |
| last_reporting_period = options[:end_date] ? ReportingPeriod.new(opt… | |
| if cache | |
| @@ -22,7 +22,7 @@ module Simplabs #:nodoc: | |
| new_data = if !options[:live_data] && last_cached_reporting_period =… | |
| [] | |
| else | |
| - end_date = options[:live_data] ? nil : last_reporting_period && la… | |
| + end_date = options[:live_data] ? nil : (options[:end_date] ? last_… | |
| yield((last_cached_reporting_period.next rescue first_reporting_pe… | |
| end | |
| @@ -35,9 +35,9 @@ module Simplabs #:nodoc: | |
| def self.prepare_result(new_data, cached_data, report, options, cache … | |
| new_data = new_data.map { |data| [ReportingPeriod.from_db_string(opt… | |
| result = cached_data.map { |cached| [cached.reporting_period, cached… | |
| - last_reporting_period = ReportingPeriod.new(options[:grouping], opti… | |
| - reporting_period = cached_data.empty? ? ReportingPeriod.first(option… | |
| - while reporting_period < last_reporting_period | |
| + last_reporting_period = ReportingPeriod.new(options[:grouping]) | |
| + reporting_period = cached_data.empty? ? ReportingPeriod.first(option… | |
| + while reporting_period < (options[:end_date] ? ReportingPeriod.new(o… | |
| cached = build_cached_data(report, options[:grouping], options[:li… | |
| cached.save! if cache | |
| result << [reporting_period.date_time, cached.value] | |
| diff --git a/lib/simplabs/reports_as_sparkline/reporting_period.rb b/lib/simpla… | |
| @@ -71,10 +71,24 @@ module Simplabs #:nodoc: | |
| raise ArgumentError.new("Can only compare instances of #{Simplabs::Rep… | |
| end | |
| + def last_date_time #:nodoc: | |
| + case @grouping.identifier | |
| + when :hour | |
| + DateTime.new(@date_time.year, @date_time.month, @date_time.day, @d… | |
| + when :day | |
| + DateTime.new(@date_time.year, @date_time.month, @date_time.day, 23… | |
| + when :week | |
| + date_time = (@date_time - @date_time.wday.days) + 7.days | |
| + Date.new(date_time.year, date_time.month, date_time.day) | |
| + when :month | |
| + Date.new(@date_time.year, @date_time.month, (Date.new(@date_time.y… | |
| + end | |
| + end | |
| + | |
| private | |
| def parse_date_time(date_time) | |
| - return case @grouping.identifier | |
| + case @grouping.identifier | |
| when :hour | |
| DateTime.new(date_time.year, date_time.month, date_time.day, dat… | |
| when :day | |
| diff --git a/spec/classes/report_cache_spec.rb b/spec/classes/report_cache_spec… | |
| @@ -120,7 +120,7 @@ describe Simplabs::ReportsAsSparkline::ReportCache do | |
| @report.options[:grouping].identifier.to_s, | |
| @report.aggregation.to_s, | |
| 10, | |
| - Simplabs::ReportsAsSparkline::ReportingPeriod.first(@report.options[… | |
| + Simplabs::ReportsAsSparkline::ReportingPeriod.first(@report.options[… | |
| Simplabs::ReportsAsSparkline::ReportingPeriod.new(@report.options[:g… | |
| ], | |
| :limit => 10, | |
| diff --git a/spec/classes/report_spec.rb b/spec/classes/report_spec.rb | |
| @@ -70,26 +70,53 @@ describe Simplabs::ReportsAsSparkline::Report do | |
| for grouping in [:hour, :day, :week, :month] do | |
| - describe "for grouping #{grouping.to_s}" do | |
| + describe "for grouping :#{grouping.to_s}" do | |
| - [true, false].each do |live_data| | |
| + before(:all) do | |
| + User.create!(:login => 'test 1', :created_at => Time.now, … | |
| + User.create!(:login => 'test 2', :created_at => Time.now - 1.send(gr… | |
| + User.create!(:login => 'test 3', :created_at => Time.now - 3.send(gr… | |
| + User.create!(:login => 'test 4', :created_at => Time.now - 3.send(gr… | |
| + end | |
| - describe "with :live_data = #{live_data}" do | |
| + describe 'when :end_date is specified' do | |
| + | |
| + describe 'the returned result' do | |
| + | |
| + before do | |
| + @end_date = DateTime.now - 1.send(grouping) | |
| + @grouping = Simplabs::ReportsAsSparkline::Grouping.new(grouping) | |
| + @report = Simplabs::ReportsAsSparkline::Report.new(User, :regist… | |
| + :grouping => grouping, | |
| + :limit => 10, | |
| + :end_date => @end_date | |
| + ) | |
| + @result = @report.run | |
| + end | |
| - before(:all) do | |
| - User.create!(:login => 'test 1', :created_at => Time.now, … | |
| - User.create!(:login => 'test 2', :created_at => Time.now - 1.sen… | |
| - User.create!(:login => 'test 3', :created_at => Time.now - 3.sen… | |
| - User.create!(:login => 'test 4', :created_at => Time.now - 3.sen… | |
| + it "should start with the reporting period (end_date - limit.#{gro… | |
| + @result.first[0].should == Simplabs::ReportsAsSparkline::Reporti… | |
| end | |
| + it "should end with the reporting period of the specified end date… | |
| + @result.last[0].should == Simplabs::ReportsAsSparkline::Reportin… | |
| + end | |
| + | |
| + end | |
| + | |
| + end | |
| + | |
| + [true, false].each do |live_data| | |
| + | |
| + describe "with :live_data = #{live_data}" do | |
| + | |
| describe 'the returned result' do | |
| before do | |
| @grouping = Simplabs::ReportsAsSparkline::Grouping.new(groupin… | |
| @report = Simplabs::ReportsAsSparkline::Report.new(User, :regi… | |
| - :grouping => grouping, | |
| - :limit => 10, | |
| + :grouping => grouping, | |
| + :limit => 10, | |
| :live_data => live_data | |
| ) | |
| @result = @report.run | |
| @@ -228,7 +255,7 @@ describe Simplabs::ReportsAsSparkline::Report do | |
| result[6][1].should == 0.0 | |
| end | |
| - it 'should return correct results when run twice with different li… | |
| + it 'should return correct results when run twice in a row with a h… | |
| @report = Simplabs::ReportsAsSparkline::Report.new(User, :regist… | |
| :aggregation => :count, | |
| :grouping => grouping, | |
| @@ -250,14 +277,49 @@ describe Simplabs::ReportsAsSparkline::Report do | |
| result[6][1].should == 0.0 | |
| end | |
| - end | |
| + unless live_data | |
| + | |
| + it 'should return correct data for aggregation :count when :end_… | |
| + @report = Simplabs::ReportsAsSparkline::Report.new(User, :regi… | |
| + :aggregation => :count, | |
| + :grouping => grouping, | |
| + :limit => 10, | |
| + :end_date => Time.now - 3.send(grouping) | |
| + ) | |
| + result = @report.run.to_a | |
| + | |
| + result[9][1].should == 2.0 | |
| + result[8][1].should == 0.0 | |
| + result[7][1].should == 0.0 | |
| + result[6][1].should == 0.0 | |
| + end | |
| + | |
| + it 'should return correct data for aggregation :sum when :end_da… | |
| + @report = Simplabs::ReportsAsSparkline::Report.new(User, :regi… | |
| + :aggregation => :sum, | |
| + :grouping => grouping, | |
| + :value_column => :profile_visits, | |
| + :limit => 10, | |
| + :end_date => Time.now - 3.send(grouping) | |
| + ) | |
| + result = @report.run.to_a | |
| + | |
| + result[9][1].should == 5.0 | |
| + result[8][1].should == 0.0 | |
| + result[7][1].should == 0.0 | |
| + result[6][1].should == 0.0 | |
| + end | |
| + | |
| + end | |
| - after(:all) do | |
| - User.destroy_all | |
| end | |
| end | |
| + after(:all) do | |
| + User.destroy_all | |
| + end | |
| + | |
| end | |
| end | |
| @@ -425,6 +487,10 @@ describe Simplabs::ReportsAsSparkline::Report do | |
| lambda { @report.send(:ensure_valid_options, { :end_date => 'today' }) }… | |
| end | |
| + it 'should raise an error if an end date is specified that is in the futur… | |
| + lambda { @report.send(:ensure_valid_options, { :end_date => (DateTime.no… | |
| + end | |
| + | |
| it 'should raise an error if both an end date and :live_data = true are sp… | |
| lambda { @report.send(:ensure_valid_options, { :end_date => DateTime.now… | |
| end | |
| diff --git a/spec/classes/reporting_period_spec.rb b/spec/classes/reporting_per… | |
| @@ -28,24 +28,24 @@ describe Simplabs::ReportsAsSparkline::ReportingPeriod do | |
| end | |
| it 'should return the date of the monday of the week date_time is in whe… | |
| - date_time = DateTime.new(2008, 11, 24) #this is a monday already, shou… | |
| + date_time = DateTime.new(2008, 11, 24) | |
| reporting_period = Simplabs::ReportsAsSparkline::ReportingPeriod.new(S… | |
| - reporting_period.date_time.should == Date.new(date_time.year, date_tim… | |
| + reporting_period.date_time.should == Date.new(date_time.year, date_tim… | |
| end | |
| it 'should return the date of the monday of the week date_time is in whe… | |
| - date_time = DateTime.new(2008, 11, 1) #this is a saturday | |
| + date_time = DateTime.new(2008, 11, 1) | |
| reporting_period = Simplabs::ReportsAsSparkline::ReportingPeriod.new(S… | |
| - reporting_period.date_time.should == Date.new(2008, 10, 27) # expect t… | |
| + reporting_period.date_time.should == Date.new(2008, 10, 27) | |
| end | |
| it 'should return the date of the monday of the week date_time is in whe… | |
| - date_time = DateTime.new(2009, 1, 1) #this is a thursday | |
| + date_time = DateTime.new(2009, 1, 1) | |
| reporting_period = Simplabs::ReportsAsSparkline::ReportingPeriod.new(S… | |
| - reporting_period.date_time.should == Date.new(2008, 12, 29) # expect t… | |
| + reporting_period.date_time.should == Date.new(2008, 12, 29) | |
| end | |
| end | |
| @@ -59,6 +59,56 @@ describe Simplabs::ReportsAsSparkline::ReportingPeriod do | |
| end | |
| + describe '#last_date_time' do | |
| + | |
| + it 'should return the date and time with minutes = seconds = 59 for groupi… | |
| + date_time = DateTime.now | |
| + reporting_period = Simplabs::ReportsAsSparkline::ReportingPeriod.new(Sim… | |
| + | |
| + reporting_period.last_date_time.should == DateTime.new(date_time.year, d… | |
| + end | |
| + | |
| + it 'should return the date part with hour = 23 and minute = seconds = 59 f… | |
| + date_time = DateTime.now | |
| + reporting_period = Simplabs::ReportsAsSparkline::ReportingPeriod.new(Sim… | |
| + | |
| + reporting_period.last_date_time.should == DateTime.new(date_time.year, d… | |
| + end | |
| + | |
| + describe 'for grouping :week' do | |
| + | |
| + it 'should return the date of the sunday of the week date_time is in for… | |
| + date_time = DateTime.new(2008, 11, 27) | |
| + reporting_period = Simplabs::ReportsAsSparkline::ReportingPeriod.new(S… | |
| + | |
| + reporting_period.last_date_time.should == Date.new(date_time.year, dat… | |
| + end | |
| + | |
| + it 'should return the date of the sunday of the week date_time is in whe… | |
| + date_time = DateTime.new(2008, 10, 30) | |
| + reporting_period = Simplabs::ReportsAsSparkline::ReportingPeriod.new(S… | |
| + | |
| + reporting_period.last_date_time.should == Date.new(2008, 11, 2) | |
| + end | |
| + | |
| + it 'should return the date of the sunday of the week date_time is in whe… | |
| + date_time = DateTime.new(2008, 12, 29) | |
| + reporting_period = Simplabs::ReportsAsSparkline::ReportingPeriod.new(S… | |
| + | |
| + reporting_period.last_date_time.should == Date.new(2009, 1, 4) | |
| + end | |
| + | |
| + end | |
| + | |
| + it 'should return the date of the last day of the month for grouping :mont… | |
| + date_time = DateTime.new(2009, 4, 29) | |
| + reporting_period = Simplabs::ReportsAsSparkline::ReportingPeriod.new(Sim… | |
| + | |
| + reporting_period.last_date_time.should == Date.new(date_time.year, date_… | |
| + end | |
| + | |
| + end | |
| + | |
| describe '.from_db_string' do | |
| it 'should return a reporting period with the correct date and time and wi… |