Introduction
Introduction Statistics Contact Development Disclaimer Help
refactored ReportCache#prepare_values - reportable - Fork of reportable require…
Log
Files
Refs
README
---
commit 1a5f067f3b860469357f8a05db8efcbc2ca3ca0a
parent 0080b2cf29f53a7f20991bab2056766fd6d5bf27
Author: Marco Otte-Witte <[email protected]>
Date: Tue, 13 Jan 2009 23:29:27 +0800
refactored ReportCache#prepare_values
Signed-off-by: Marco Otte-Witte <[email protected]>
Diffstat:
M lib/kvlr/reports_as_sparkline/cumu… | 3 +--
M lib/kvlr/reports_as_sparkline/repo… | 2 +-
M lib/kvlr/reports_as_sparkline/repo… | 31 +++++++++++++--------------…
M lib/kvlr/reports_as_sparkline/repo… | 30 +++++++++++++++++++--------…
M spec/models/report_method_spec.rb | 8 ++++----
M spec/other/cumulated_report_spec.rb | 46 ++++++++++++++++-------------…
M spec/other/report_cache_spec.rb | 71 +++++++++++++++++------------…
M spec/other/report_spec.rb | 58 ++++++++++++++++++++---------…
M spec/other/reporting_period_spec.rb | 44 ++++++++++++++++-------------…
9 files changed, 160 insertions(+), 133 deletions(-)
---
diff --git a/lib/kvlr/reports_as_sparkline/cumulated_report.rb b/lib/kvlr/repor…
@@ -25,11 +25,10 @@ module Kvlr #:nodoc:
def cumulate(data) #:nodoc:
acc = 0.0
result = []
- data.reverse_each do |element|
+ data.each do |element|
acc += element[1].to_f
result << [element[0], acc]
end
- result.reverse!
result
end
diff --git a/lib/kvlr/reports_as_sparkline/report.rb b/lib/kvlr/reports_as_spar…
@@ -56,7 +56,7 @@ module Kvlr #:nodoc:
@value_column,
:conditions => conditions,
:group => @grouping.to_sql(@date_column),
- :order => "#{@grouping.to_sql(@date_column)} DESC"
+ :order => "#{@grouping.to_sql(@date_column)} ASC"
)
end
diff --git a/lib/kvlr/reports_as_sparkline/report_cache.rb b/lib/kvlr/reports_a…
@@ -23,7 +23,7 @@ module Kvlr #:nodoc:
:limit => limit,
:order => 'reporting_period ASC'
)
- last_reporting_period_to_read = ReportingPeriod.new(report.groupin…
+ last_reporting_period_to_read = ReportingPeriod.new(report.groupin…
end
new_data = yield(last_reporting_period_to_read.date_time)
prepare_result(new_data, cached_data, last_reporting_period_to_read,…
@@ -34,29 +34,24 @@ module Kvlr #:nodoc:
def self.prepare_result(new_data, cached_data, last_reporting_period_t…
new_data.map! { |data| [ReportingPeriod.from_db_string(report.groupi…
- result = []
- reporting_period = ReportingPeriod.new(report.grouping)
- while reporting_period != last_reporting_period_to_read
- data = new_data.detect { |data| data[0] == reporting_period }
- cached = build_cached_data(report, reporting_period, data ? data[1…
+ result = cached_data.map { |cached| [cached.reporting_period, cached…
+ current_reporting_period = ReportingPeriod.new(report.grouping)
+ reporting_period = last_reporting_period_to_read
+ while reporting_period < current_reporting_period
+ cached = build_cached_data(report, reporting_period, find_value(ne…
cached.save! unless no_cache
result << [reporting_period.date_time, cached.value]
- reporting_period = reporting_period.previous
+ reporting_period = reporting_period.next
end
- data = (new_data.first && new_data.first[0] == last_reporting_period…
- unless no_cache
- if data && cached = cached_data.last
- cached.update_attributes!(:value => data[1])
- else
- cached = build_cached_data(report, last_reporting_period_to_read…
- cached.save!
- result << [last_reporting_period_to_read.date_time, cached.value]
- end
- end
- result += (cached_data.map { |cached| [cached.reporting_period, cach…
+ result << [current_reporting_period.date_time, find_value(new_data, …
result
end
+ def self.find_value(data, reporting_period)
+ data = data.detect { |d| d[0] == reporting_period }
+ data ? data[1] : 0.0
+ end
+
def self.build_cached_data(report, reporting_period, value)
self.new(
:model_name => report.klass.to_s,
diff --git a/lib/kvlr/reports_as_sparkline/reporting_period.rb b/lib/kvlr/repor…
@@ -15,7 +15,8 @@ module Kvlr #:nodoc:
@date_time = parse_date_time(date_time)
end
- # Returns the first reporting period for a grouping and a limit; e.g. th…
+ # Returns the first reporting period for a grouping and a limit; e.g. th…
+ # (since limit is 2, 2 reporting periods are included in the range, that…
#
# ==== Parameters
# * <tt>grouping</tt> - The Kvlr::ReportsAsSparkline::Grouping of the re…
@@ -23,13 +24,13 @@ module Kvlr #:nodoc:
def self.first(grouping, limit)
return case grouping.identifier
when :hour
- self.new(grouping, DateTime.now - limit.hours)
+ self.new(grouping, DateTime.now - (limit - 1).hours)
when :day
- self.new(grouping, DateTime.now - limit.days)
+ self.new(grouping, DateTime.now - (limit - 1).days)
when :week
- self.new(grouping, DateTime.now - limit.weeks)
+ self.new(grouping, DateTime.now - (limit - 1).weeks)
when :month
- date = DateTime.now - limit.months
+ date = DateTime.now - (limit - 1).months
self.new(grouping, Date.new(date.year, date.month, 1))
end
end
@@ -49,17 +50,17 @@ module Kvlr #:nodoc:
result
end
- # Returns the previous reporting period
- def previous
+ # Returns the next reporting period (that is next hour/day/month/year)
+ def next
return case @grouping.identifier
when :hour
- self.class.new(@grouping, @date_time - 1.hour)
+ self.class.new(@grouping, @date_time + 1.hour)
when :day
- self.class.new(@grouping, @date_time - 1.day)
+ self.class.new(@grouping, @date_time + 1.day)
when :week
- self.class.new(@grouping, @date_time - 1.week)
+ self.class.new(@grouping, @date_time + 1.week)
when :month
- self.class.new(@grouping, @date_time - 1.month)
+ self.class.new(@grouping, @date_time + 1.month)
end
end
@@ -70,6 +71,13 @@ module Kvlr #:nodoc:
false
end
+ def <(other) #:nodoc:
+ if other.class == Kvlr::ReportsAsSparkline::ReportingPeriod
+ return @date_time < other.date_time
+ end
+ raise ArgumentError.new("Can only compare instances of #{Kvlr::Reports…
+ end
+
private
def parse_date_time(date_time)
diff --git a/spec/models/report_method_spec.rb b/spec/models/report_method_spec…
@@ -12,17 +12,17 @@ describe Kvlr::ReportsAsSparkline do
it 'should include all data when invoked on the base model class' do
result = User.registrations_report.to_a
-
+puts result.inspect
result.length.should == 10
- result[1][1].should == 1
- result[2][1].should == 2
+ result[8][1].should == 1.0
+ result[7][1].should == 2.0
end
it 'should include only data for instances of the inherited model when inv…
result = SpecialUser.registrations_report.to_a
result.length.should == 10
- result[2][1].should == 1
+ result[7][1].should == 1.0
end
after(:all) do
diff --git a/spec/other/cumulated_report_spec.rb b/spec/other/cumulated_report_…
@@ -38,12 +38,12 @@ describe Kvlr::ReportsAsSparkline::CumulatedReport do
@result = @report.run
end
- it "should return data starting with the current reporting period" do
- @result.first[0].should == Kvlr::ReportsAsSparkline::ReportingPeri…
+ it "should return an array starting reporting period (Time.now - (li…
+ @result.first[0].should == Kvlr::ReportsAsSparkline::ReportingPeri…
end
- it "should return data ending with reporting period (Time.now - (lim…
- @result.last[0].should == Kvlr::ReportsAsSparkline::ReportingPerio…
+ it "should return data ending with with the current reporting period…
+ @result.last[0].should == Kvlr::ReportsAsSparkline::ReportingPerio…
end
end
@@ -52,40 +52,40 @@ describe Kvlr::ReportsAsSparkline::CumulatedReport do
@report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :regis…
result = @report.run
- result[0][1].should == 3
- result[1][1].should == 3
- result[2][1].should == 2
- result[3][1].should == 2
+ result[9][1].should == 3.0
+ result[8][1].should == 3.0
+ result[7][1].should == 2.0
+ result[6][1].should == 2.0
end
it 'should return correct data for aggregation :sum' do
@report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :regis…
result = @report.run()
- result[0][1].should == 6
- result[1][1].should == 6
- result[2][1].should == 5
- result[3][1].should == 5
+ result[9][1].should == 6.0
+ result[8][1].should == 6.0
+ result[7][1].should == 5.0
+ result[6][1].should == 5.0
end
it 'should return correct data for aggregation :count when custom cond…
@report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :regis…
result = @report.run(:conditions => ['login IN (?)', ['test 1', 'tes…
- result[0][1].should == 2
- result[1][1].should == 2
- result[2][1].should == 1
- result[3][1].should == 1
+ result[9][1].should == 2.0
+ result[8][1].should == 2.0
+ result[7][1].should == 1.0
+ result[6][1].should == 1.0
end
it 'should return correct data for aggregation :sum when custom condit…
@report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :regis…
result = @report.run(:conditions => ['login IN (?)', ['test 1', 'tes…
- result[0][1].should == 3
- result[1][1].should == 3
- result[2][1].should == 2
- result[3][1].should == 2
+ result[9][1].should == 3.0
+ result[8][1].should == 3.0
+ result[7][1].should == 2.0
+ result[6][1].should == 2.0
end
after(:all) do
@@ -105,11 +105,11 @@ describe Kvlr::ReportsAsSparkline::CumulatedReport do
describe '.cumulate' do
it 'should correctly cumulate the given data' do
- first = Time.now.to_s
- second = (Time.now - 1.week).to_s
+ first = (Time.now - 1.week).to_s
+ second = Time.now.to_s
data = [[first, 1], [second, 2]]
- @report.send(:cumulate, data).should == [[first, 3.0], [second, 2.0]]
+ @report.send(:cumulate, data).should == [[first, 1.0], [second, 3.0]]
end
end
diff --git a/spec/other/report_cache_spec.rb b/spec/other/report_cache_spec.rb
@@ -6,7 +6,7 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
@report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations)
end
- describe '#process' do
+ describe '.process' do
before do
Kvlr::ReportsAsSparkline::ReportCache.stub!(:find).and_return([])
@@ -46,7 +46,7 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
:order => 'reporting_period ASC'
)
- puts Kvlr::ReportsAsSparkline::ReportCache.process(@report, 10) { [] }
+ Kvlr::ReportsAsSparkline::ReportCache.process(@report, 10) { [] }
end
it 'should prepare the results before it returns them' do
@@ -66,7 +66,7 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
end
end
- it 'should yield the last reporting period in the cache if the cache is no…
+ it 'should yield the reporting period after the last one in the cache if t…
reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(@report…
cached = Kvlr::ReportsAsSparkline::ReportCache.new({
:model_name => @report.klass,
@@ -79,7 +79,7 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
Kvlr::ReportsAsSparkline::ReportCache.stub!(:find).and_return([cached])
Kvlr::ReportsAsSparkline::ReportCache.process(@report, 10) do |begin_at|
- begin_at.should == reporting_period.date_time
+ begin_at.should == reporting_period.next.date_time
[]
end
end
@@ -103,16 +103,15 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
end
- describe '#prepare_result' do
+ describe '.prepare_result' do
before do
@last_reporting_period_to_read = Kvlr::ReportsAsSparkline::ReportingPeri…
- @new_data = [['2008/12', 1.0]]
- Kvlr::ReportsAsSparkline::ReportingPeriod.stub!(:from_db_string).and_ret…
+ @new_data = [[@last_reporting_period_to_read.date_time, 1.0]]
+ Kvlr::ReportsAsSparkline::ReportingPeriod.stub!(:from_db_string).and_ret…
@cached = Kvlr::ReportsAsSparkline::ReportCache.new
@cached.stub!(:save!)
- @cached.stub!(:reporting_period).and_return(Kvlr::ReportsAsSparkline::Re…
- Kvlr::ReportsAsSparkline::ReportCache.stub!(:new).and_return(@cached)
+ Kvlr::ReportsAsSparkline::ReportCache.stub!(:build_cached_data).and_retu…
end
it 'should convert the date strings from the newly read data to reporting …
@@ -121,27 +120,26 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, […
end
- it 'should create a new Kvlr::ReportsAsSparkline::ReportCache with the cor…
- Kvlr::ReportsAsSparkline::ReportCache.should_receive(:new).once.with(
- :model_name => @report.klass.to_s,
- :report_name => @report.name.to_s,
- :grouping => @report.grouping.identifier.to_s,
- :aggregation => @report.aggregation.to_s,
- :reporting_period => anything(),
- :value => 0
+ it 'should create (:limit - 1) instances of Kvlr::ReportsAsSparkline::Repo…
+ Kvlr::ReportsAsSparkline::ReportCache.should_receive(:build_cached_data)…
+ @report,
+ anything(),
+ 0.0
).and_return(@cached)
Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, [], [], @las…
end
- it 'should create a new Kvlr::ReportsAsSparkline::ReportCache with the cor…
- Kvlr::ReportsAsSparkline::ReportCache.should_receive(:new).once.with(
- :model_name => @report.klass.to_s,
- :report_name => @report.name.to_s,
- :grouping => @report.grouping.identifier.to_s,
- :aggregation => @report.aggregation.to_s,
- :reporting_period => anything(),
- :value => 1.0
+ it 'should create a new Kvlr::ReportsAsSparkline::ReportCache with the cor…
+ Kvlr::ReportsAsSparkline::ReportCache.should_receive(:build_cached_data)…
+ @report,
+ anything(),
+ 0.0
+ ).and_return(@cached)
+ Kvlr::ReportsAsSparkline::ReportCache.should_receive(:build_cached_data)…
+ @report,
+ @last_reporting_period_to_read,
+ 1.0
).and_return(@cached)
Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, […
@@ -162,13 +160,6 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
result[0][1].should be_kind_of(Float)
end
- it 'should update the last cached record if new data has been read for the…
- Kvlr::ReportsAsSparkline::ReportingPeriod.stub!(:from_db_string).and_ret…
- @cached.should_receive(:update_attributes!).once.with(:value => 1.0)
-
- Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, […
- end
-
describe 'with no_cache = true' do
it 'should not save the created Kvlr::ReportsAsSparkline::ReportCache' do
@@ -188,4 +179,20 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
end
+ describe '.find_value' do
+
+ before do
+ @data = [[Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsS…
+ end
+
+ it 'should return the correct value when new data has been read for the re…
+ Kvlr::ReportsAsSparkline::ReportCache.send(:find_value, @data, @data[0][…
+ end
+
+ it 'should return 0.0 when no data has been read for the reporting period'…
+ Kvlr::ReportsAsSparkline::ReportCache.send(:find_value, @data, @data[0][…
+ end
+
+ end
+
end
diff --git a/spec/other/report_spec.rb b/spec/other/report_spec.rb
@@ -36,44 +36,62 @@ describe Kvlr::ReportsAsSparkline::Report do
User.create!(:login => 'test 3', :created_at => Time.now - 3.send(gr…
end
+ describe do
+
+ before do
+ @grouping = Kvlr::ReportsAsSparkline::Grouping.new(grouping)
+ @report = Kvlr::ReportsAsSparkline::Report.new(User, :registration…
+ @result = @report.run
+ end
+
+ it "should return an array starting reporting period (Time.now - (li…
+ @result.first[0].should == Kvlr::ReportsAsSparkline::ReportingPeri…
+ end
+
+ it "should return data ending with with the current reporting period…
+ @result.last[0].should == Kvlr::ReportsAsSparkline::ReportingPerio…
+ end
+
+ end
+
it 'should return correct data for aggregation :count' do
@report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations,…
result = @report.run.to_a
- result[0][1].should == 0
- result[1][1].should == 1
- result[2][1].should == 0
- result[3][1].should == 2
+ result[9][1].should == 0.0
+ result[8][1].should == 1.0
+ result[7][1].should == 0.0
+ result[6][1].should == 2.0
end
it 'should return correct data for aggregation :sum' do
@report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations,…
result = @report.run().to_a
- result[0][1].should == 0
- result[1][1].should == 1
- result[2][1].should == 0
- result[3][1].should == 5
+ result[9][1].should == 0.0
+ result[8][1].should == 1.0
+ result[7][1].should == 0.0
+ result[6][1].should == 5.0
end
it 'should return correct data for aggregation :count when custom cond…
@report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations,…
result = @report.run(:conditions => ['login IN (?)', ['test 1', 'tes…
- result[0][1].should == 0
- result[1][1].should == 1
- result[2][1].should == 0
- result[3][1].should == 1
+ result[9][1].should == 0.0
+ result[8][1].should == 1.0
+ result[7][1].should == 0.0
+ result[6][1].should == 1.0
end
it 'should return correct data for aggregation :sum when custom condit…
@report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations,…
result = @report.run(:conditions => ['login IN (?)', ['test 1', 'tes…
- result[0][1].should == 0
- result[1][1].should == 1
- result[2][1].should == 0
- result[3][1].should == 2
+ result[9][1].should == 0.0
+ result[8][1].should == 1.0
+ result[7][1].should == 0.0
+ result[6][1].should == 2.0
end
after(:all) do
@@ -173,12 +191,12 @@ describe Kvlr::ReportsAsSparkline::Report do
it 'should not raise an error if valid options are specified' do
lambda { @report.send(:ensure_valid_options, {
- :limit => 100,
- :aggregation => :count,
- :grouping => :day,
+ :limit => 100,
+ :aggregation => :count,
+ :grouping => :day,
:date_column => :created_at,
:value_column => :id,
- :conditions => []
+ :conditions => []
})
}.should_not raise_error(ArgumentError)
end
diff --git a/spec/other/reporting_period_spec.rb b/spec/other/reporting_period_…
@@ -91,38 +91,38 @@ describe Kvlr::ReportsAsSparkline::ReportingPeriod do
end
- describe '.previous' do
+ describe '.next' do
- it 'should return a reporting period with date and time one hour before th…
+ it 'should return a reporting period with date and time one hour after the…
now = Time.now
reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::R…
- expected = now - 1.hour
+ expected = now + 1.hour
- reporting_period.previous.date_time.should == DateTime.new(expected.year…
+ reporting_period.next.date_time.should == DateTime.new(expected.year, ex…
end
- it 'should return a reporting period with date one day before the current …
+ it 'should return a reporting period with date one day after the current p…
now = Time.now
reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::R…
- expected = now - 1.day
+ expected = now + 1.day
- reporting_period.previous.date_time.should == Date.new(expected.year, ex…
+ reporting_period.next.date_time.should == Date.new(expected.year, expect…
end
- it 'should return a reporting period with date one week before the current…
+ it 'should return a reporting period with date one week after the current …
now = DateTime.now
reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::R…
- expected = reporting_period.date_time - 1.week
+ expected = reporting_period.date_time + 1.week
- reporting_period.previous.date_time.should == Date.new(expected.year, ex…
+ reporting_period.next.date_time.should == Date.new(expected.year, expect…
end
- it 'should return a reporting period with date of the first day in the mon…
+ it 'should return a reporting period with date of the first day in the mon…
now = Time.now
reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::R…
- expected = reporting_period.date_time - 1.month
+ expected = reporting_period.date_time + 1.month
- reporting_period.previous.date_time.should == Date.new(expected.year, ex…
+ reporting_period.next.date_time.should == Date.new(expected.year, expect…
end
end
@@ -161,39 +161,39 @@ describe Kvlr::ReportsAsSparkline::ReportingPeriod do
end
- describe '#first' do
+ describe '.first' do
before do
@now = DateTime.now
DateTime.stub!(:now).and_return(@now)
end
- it 'should return a reporting period with the date part of (DateTime.now -…
+ it 'should return a reporting period with the date part of (DateTime.now -…
reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.first(Kvlr:…
- expected = @now - 3.hours
+ expected = @now - 2.hours
reporting_period.date_time.should == DateTime.new(expected.year, expecte…
end
- it 'should return a reporting period with the date part of (DateTime.now -…
+ it 'should return a reporting period with the date part of (DateTime.now -…
reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.first(Kvlr:…
- expected = @now - 3.days
+ expected = @now - 2.days
reporting_period.date_time.should == Date.new(expected.year, expected.mo…
end
- it 'should return a reporting period with the date of the first day of the…
+ it 'should return a reporting period with the date of the first day of the…
DateTime.stub!(:now).and_return(DateTime.new(2008, 12, 31, 0, 0, 0))
reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.first(Kvlr:…
- reporting_period.date_time.should == DateTime.new(2008, 9, 1)
+ reporting_period.date_time.should == DateTime.new(2008, 10, 1)
end
- it 'should return a reporting period with the date of the monday of the we…
+ it 'should return a reporting period with the date of the monday of the we…
DateTime.stub!(:now).and_return(DateTime.new(2008, 12, 31, 0, 0, 0)) #we…
reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.first(Kvlr:…
- reporting_period.date_time.should == DateTime.new(2008, 12, 8) #the mond…
+ reporting_period.date_time.should == DateTime.new(2008, 12, 15) #the mon…
end
end
You are viewing proxied material from jay.scot. The copyright of proxied material belongs to its original authors. Any comments or complaints in relation to proxied material should be directed to the original authors of the content concerned. Please see the disclaimer for more details.