| Fixed a bug with cumulated reports. To be a true cumulated total, they should … | |
| Log | |
| Files | |
| Refs | |
| README | |
| --- | |
| commit 5af0207e497c3a9cbf3c60dd5d6f0f0914ecef28 | |
| parent c56e40b20ceea3a9098f19c675bf7790c0002569 | |
| Author: Myron Marston <[email protected]> | |
| Date: Sat, 4 Apr 2009 05:48:41 +0800 | |
| Fixed a bug with cumulated reports. To be a true cumulated total, they should … | |
| Signed-off-by: Marco Otte-Witte <[email protected]> | |
| Diffstat: | |
| M lib/kvlr/reports_as_sparkline/cumu… | 12 +++++++++--- | |
| M lib/kvlr/reports_as_sparkline/repo… | 29 ++++++++++++++++++++-------… | |
| M spec/classes/cumulated_report_spec… | 38 +++++++++++++++++++++++++++… | |
| M spec/classes/report_spec.rb | 8 ++++++++ | |
| 4 files changed, 74 insertions(+), 13 deletions(-) | |
| --- | |
| diff --git a/lib/kvlr/reports_as_sparkline/cumulated_report.rb b/lib/kvlr/repor… | |
| @@ -17,13 +17,14 @@ module Kvlr #:nodoc: | |
| # Runs the report (see Kvlr::ReportsAsSparkline::Report#run) | |
| def run(options = {}) | |
| - cumulate(super) | |
| + cumulate(super, options_for_run(options)) | |
| end | |
| protected | |
| - def cumulate(data) #:nodoc: | |
| - acc = 0.0 | |
| + def cumulate(data, options) #:nodoc: | |
| + first_reporting_period = ReportingPeriod.first(options[:grouping], o… | |
| + acc = initial_cumulative_value(first_reporting_period.date_time, opt… | |
| result = [] | |
| data.each do |element| | |
| acc += element[1].to_f | |
| @@ -32,6 +33,11 @@ module Kvlr #:nodoc: | |
| result | |
| end | |
| + def initial_cumulative_value(date, options) | |
| + conditions = setup_conditions(nil, date, options[:conditions]) | |
| + @klass.send(@aggregation, @value_column, :conditions => conditions) | |
| + end | |
| + | |
| end | |
| end | |
| diff --git a/lib/kvlr/reports_as_sparkline/report.rb b/lib/kvlr/reports_as_spar… | |
| @@ -48,11 +48,8 @@ module Kvlr #:nodoc: | |
| # * <tt>:live_data</tt> - Specified whether data for the current reporti… | |
| # * <tt>:end_date</tt> - When specified, the report will be for the peri… | |
| def run(options = {}) | |
| - options = options.dup | |
| - ensure_valid_options(options, :run) | |
| custom_conditions = options.key?(:conditions) | |
| - options.reverse_merge!(@options) | |
| - options[:grouping] = Grouping.new(options[:grouping]) unless options[:… | |
| + options = options_for_run(options) | |
| ReportCache.process(self, options, !custom_conditions) do |begin_at, e… | |
| read_data(begin_at, end_at, options) | |
| end | |
| @@ -60,6 +57,14 @@ module Kvlr #:nodoc: | |
| private | |
| + def options_for_run(options = {}) | |
| + options = options.dup | |
| + ensure_valid_options(options, :run) | |
| + options.reverse_merge!(@options) | |
| + options[:grouping] = Grouping.new(options[:grouping]) unless options… | |
| + return options | |
| + end | |
| + | |
| def read_data(begin_at, end_at, options) | |
| conditions = setup_conditions(begin_at, end_at, options[:conditions]) | |
| @klass.send(@aggregation, | |
| @@ -85,14 +90,20 @@ module Kvlr #:nodoc: | |
| elsif custom_conditions.size > 0 | |
| conditions = [(custom_conditions[0] || ''), *custom_conditions[1..… | |
| end | |
| - conditions[0] += "#{(conditions[0].blank? ? '' : ' AND ') + @date_co… | |
| - conditions << begin_at | |
| + conditions[0] += "#{(conditions[0].blank? ? '' : ' AND ') + @date_co… | |
| - if end_at | |
| - conditions[0].sub!(/>= \?\z/, 'BETWEEN ? AND ?') | |
| - conditions << end_at | |
| + conditions[0] += if begin_at && end_at | |
| + 'BETWEEN ? AND ?' | |
| + elsif begin_at | |
| + '>= ?' | |
| + elsif end_at | |
| + '<= ?' | |
| + else | |
| + raise ArgumentError.new('You must pass either begin_at, end_at or … | |
| end | |
| + conditions << begin_at if begin_at | |
| + conditions << end_at if end_at | |
| conditions | |
| end | |
| diff --git a/spec/classes/cumulated_report_spec.rb b/spec/classes/cumulated_rep… | |
| @@ -25,6 +25,41 @@ describe Kvlr::ReportsAsSparkline::CumulatedReport do | |
| @report.run.length.should == 11 | |
| end | |
| + | |
| + describe "a month report with a limit of 2" do | |
| + before(:all) do | |
| + User.delete_all | |
| + User.create!(:login => 'test 1', :created_at => Time.now, :p… | |
| + User.create!(:login => 'test 2', :created_at => Time.now - 1.month, :p… | |
| + User.create!(:login => 'test 3', :created_at => Time.now - 3.month, :p… | |
| + User.create!(:login => 'test 4', :created_at => Time.now - 3.month, :p… | |
| + User.create!(:login => 'test 5', :created_at => Time.now - 4.month, :p… | |
| + | |
| + @report2 = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :cumula… | |
| + :grouping => :month, | |
| + :limit => 2 | |
| + ) | |
| + | |
| + @one_month_ago = Date.new(DateTime.now.year, DateTime.now.month, 1)… | |
| + @two_months_ago = Date.new(DateTime.now.year, DateTime.now.month, 1)… | |
| + @three_months_ago = Date.new(DateTime.now.year, DateTime.now.month, 1)… | |
| + end | |
| + | |
| + it 'should include the counts from before the first period in the cumula… | |
| + @report2.run.should == [[@two_months_ago, 3.0], [@one_month_ago, 4.0]] | |
| + end | |
| + | |
| + it 'should return the initial count for initial_cumulative_value' do | |
| + options = @report2.send(:options_for_run, {}) | |
| + tomorrow = 1.day.from_now | |
| + @report2.send(:initial_cumulative_value, tomorrow, options)… | |
| + @report2.send(:initial_cumulative_value, tomorrow - 1.month, options)… | |
| + @report2.send(:initial_cumulative_value, tomorrow - 2.months, options)… | |
| + @report2.send(:initial_cumulative_value, tomorrow - 3.months, options)… | |
| + @report2.send(:initial_cumulative_value, tomorrow - 4.months, options)… | |
| + @report2.send(:initial_cumulative_value, tomorrow - 5.months, options)… | |
| + end | |
| + end | |
| for grouping in [:hour, :day, :week, :month] do | |
| @@ -35,6 +70,7 @@ describe Kvlr::ReportsAsSparkline::CumulatedReport do | |
| describe "with :live_data = #{live_data}" do | |
| before(:all) do | |
| + User.delete_all | |
| User.create!(:login => 'test 1', :created_at => Time.now - 1.sen… | |
| User.create!(:login => 'test 2', :created_at => Time.now - 3.sen… | |
| User.create!(:login => 'test 3', :created_at => Time.now - 3.sen… | |
| @@ -159,7 +195,7 @@ describe Kvlr::ReportsAsSparkline::CumulatedReport do | |
| second = Time.now.to_s | |
| data = [[first, 1], [second, 2]] | |
| - @report.send(:cumulate, data).should == [[first, 1.0], [second, 3.0]] | |
| + @report.send(:cumulate, data, @report.send(:options_for_run, {})).should… | |
| end | |
| end | |
| diff --git a/spec/classes/report_spec.rb b/spec/classes/report_spec.rb | |
| @@ -386,6 +386,14 @@ describe Kvlr::ReportsAsSparkline::Report do | |
| it 'should return conditions for date_column >= begin_at when no custom co… | |
| @report.send(:setup_conditions, @begin_at, nil).should == ['created_at >… | |
| end | |
| + | |
| + it 'should return conditions for date_column <= end_at when no custom cond… | |
| + @report.send(:setup_conditions, nil, @end_at).should == ['created_at <= … | |
| + end | |
| + | |
| + it 'should raise an argument error when neither begin_at or end_at are spe… | |
| + lambda {@report.send(:setup_conditions, nil, nil)}.should raise_error(Ar… | |
| + end | |
| it 'should return conditions for date_column BETWEEN begin_at and end_date… | |
| @report.send(:setup_conditions, @begin_at, @end_at, {}).should == ['crea… |