Introduction
Introduction Statistics Contact Development Disclaimer Help
included run limit in cache identifier (closes #3) - reportable - Fork of repor…
Log
Files
Refs
README
---
commit 063ae7ebfb466b15d8fdb0347f9c4eaeff7775b2
parent a8fd0e703fd9b424cc69125c89722d85664bb5da
Author: Marco Otte-Witte <[email protected]>
Date: Wed, 29 Apr 2009 15:00:55 +0200
included run limit in cache identifier (closes #3)
Diffstat:
M generators/reports_as_sparkline_mi… | 15 +++++++++------
M lib/simplabs/reports_as_sparkline/… | 52 ++++++++++-----------------…
M spec/classes/report_cache_spec.rb | 48 +++++++++++++++--------------…
M spec/classes/report_spec.rb | 22 ++++++++++++++++++++++
M spec/db/schema.rb | 11 +++++++----
5 files changed, 77 insertions(+), 71 deletions(-)
---
diff --git a/generators/reports_as_sparkline_migration/templates/migration.erb …
@@ -8,6 +8,7 @@ class <%= class_name %> < ActiveRecord::Migration
t.string :aggregation, :null => false
t.float :value, :null => false, :default => 0
t.datetime :reporting_period, :null => false
+ t.integer :run_limit, :null => false
t.timestamps
end
@@ -16,20 +17,22 @@ class <%= class_name %> < ActiveRecord::Migration
:model_name,
:report_name,
:grouping,
- :aggregation
- ], :name => :name_model_grouping_agregation
+ :aggregation,
+ :run_limit
+ ], :name => :name_model_grouping_agregation_run_limit
add_index :reports_as_sparkline_cache, [
:model_name,
:report_name,
:grouping,
:aggregation,
- :reporting_period
- ], :unique => true, :name => :name_model_grouping_aggregation_period
+ :reporting_period,
+ :run_limit
+ ], :unique => true, :name => :name_model_grouping_aggregation_period_run_l…
end
def self.down
- remove_index :reports_as_sparkline_cache, :name => :name_model_grouping_ag…
- remove_index :reports_as_sparkline_cache, :name => :name_model_grouping_ag…
+ remove_index :reports_as_sparkline_cache, :name => :name_model_grouping_ag…
+ remove_index :reports_as_sparkline_cache, :name => :name_model_grouping_ag…
drop_table :reports_as_sparkline_cache
end
diff --git a/lib/simplabs/reports_as_sparkline/report_cache.rb b/lib/simplabs/r…
@@ -12,61 +12,39 @@ module Simplabs #:nodoc:
cached_data = []
first_reporting_period = ReportingPeriod.first(options[:grouping], o…
last_reporting_period = options[:end_date] ? ReportingPeriod.new(opt…
+
if cache
cached_data = find_cached_data(report, options, first_reporting_pe…
first_cached_reporting_period = cached_data.empty? ? nil : Reporti…
last_cached_reporting_period = cached_data.empty? ? nil : Reportin…
end
- new_after_cache_data = if !options[:live_data] && last_cached_report…
+
+ new_data = if !options[:live_data] && last_cached_reporting_period =…
[]
else
end_date = options[:live_data] ? nil : last_reporting_period && la…
yield((last_cached_reporting_period.next rescue first_reporting_pe…
end
- new_before_cache_data = if cached_data.empty? || first_cached_report…
- []
- else
- yield(first_reporting_period.date_time, first_cached_reporting_per…
- end
- prepare_result(new_before_cache_data, new_after_cache_data, cached_d…
+
+ prepare_result(new_data, cached_data, report, options, cache)
end
end
private
- def self.prepare_result(new_before_cache_data, new_after_cache_data, c…
- cache_map_proc = lambda { |data| [ReportingPeriod.from_db_string(opt…
-
- new_after_cache_data = new_after_cache_data.map &cache_map_proc
- new_before_cache_data = new_before_cache_data.map &cache_map_proc
+ 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…
-
- first_reporting_period = ReportingPeriod.first(options[:grouping], o…
last_reporting_period = ReportingPeriod.new(options[:grouping], opti…
- first_cached_reporting_period = cached_data.empty? ? nil : Reporting…
- last_cached_reporting_period = cached_data.empty? ? nil : ReportingP…
-
- if first_cached_reporting_period
- reporting_period = first_reporting_period
- while reporting_period < first_cached_reporting_period
- cached = build_cached_data(report, options[:grouping], reporting…
- cached.save! if cache
- result.insert(0, [reporting_period.date_time, cached.value])
- reporting_period = reporting_period.next
- end
- end
-
- reporting_period = cached_data.empty? ? first_reporting_period : las…
-
+ reporting_period = cached_data.empty? ? ReportingPeriod.first(option…
while reporting_period < last_reporting_period
- cached = build_cached_data(report, options[:grouping], reporting_p…
+ cached = build_cached_data(report, options[:grouping], options[:li…
cached.save! if cache
result << [reporting_period.date_time, cached.value]
reporting_period = reporting_period.next
end
-
if options[:live_data]
- result << [last_reporting_period.date_time, find_value(new_after_c…
+ result << [last_reporting_period.date_time, find_value(new_data, l…
end
result
end
@@ -76,24 +54,26 @@ module Simplabs #:nodoc:
data ? data[1] : 0.0
end
- def self.build_cached_data(report, grouping, reporting_period, value)
+ def self.build_cached_data(report, grouping, limit, reporting_period, …
self.new(
:model_name => report.klass.to_s,
:report_name => report.name.to_s,
:grouping => grouping.identifier.to_s,
:aggregation => report.aggregation.to_s,
:reporting_period => reporting_period.date_time,
- :value => value
+ :value => value,
+ :run_limit => limit
)
end
def self.find_cached_data(report, options, first_reporting_period, las…
conditions = [
- 'model_name = ? AND report_name = ? AND grouping = ? AND aggregati…
+ 'model_name = ? AND report_name = ? AND grouping = ? AND aggregati…
report.klass.to_s,
report.name.to_s,
options[:grouping].identifier.to_s,
- report.aggregation.to_s
+ report.aggregation.to_s,
+ options[:limit]
]
if last_reporting_period
conditions.first << ' AND reporting_period BETWEEN ? AND ?'
diff --git a/spec/classes/report_cache_spec.rb b/spec/classes/report_cache_spec…
@@ -37,7 +37,7 @@ describe Simplabs::ReportsAsSparkline::ReportCache do
}.should raise_error(YieldMatchException)
end
- it 'should yield the reporting period after the last one in the cache, a…
+ it 'should yield the reporting period after the last one in the cache if…
reporting_period = Simplabs::ReportsAsSparkline::ReportingPeriod.new(
@report.options[:grouping],
Time.now - 3.send(@report.options[:grouping].identifier)
@@ -46,15 +46,11 @@ describe Simplabs::ReportsAsSparkline::ReportCache do
cached.stub!(:reporting_period).and_return(reporting_period.date_time)
Simplabs::ReportsAsSparkline::ReportCache.stub!(:find).and_return([cac…
- expected_dates = [[reporting_period.next.date_time, nil], [reporting_p…
- yield_count = 0
Simplabs::ReportsAsSparkline::ReportCache.process(@report, @options) d…
- [begin_at, end_at].should == expected_dates[yield_count]
- yield_count += 1
+ begin_at.should == reporting_period.next.date_time
+ end_at.should == nil
[]
end
-
- yield_count.should == 2
end
end
@@ -76,7 +72,7 @@ describe Simplabs::ReportsAsSparkline::ReportCache do
}.should raise_error(YieldMatchException)
end
- it 'should yield the reporting period after the last one in the cache, a…
+ it 'should yield the reporting period after the last one in the cache if…
reporting_period = Simplabs::ReportsAsSparkline::ReportingPeriod.new(
@report.options[:grouping],
Time.now - 3.send(@report.options[:grouping].identifier)
@@ -85,15 +81,11 @@ describe Simplabs::ReportsAsSparkline::ReportCache do
cached.stub!(:reporting_period).and_return(reporting_period.date_time)
Simplabs::ReportsAsSparkline::ReportCache.stub!(:find).and_return([cac…
- expected_dates = [[reporting_period.next.date_time, nil], [reporting_p…
- yield_count = 0
Simplabs::ReportsAsSparkline::ReportCache.process(@report, @report.opt…
- [begin_at, end_at].should == expected_dates[yield_count]
- yield_count += 1
+ begin_at.should == reporting_period.next.date_time
+ end_at.should == nil
[]
end
-
- yield_count.should == 2
end
end
@@ -102,11 +94,12 @@ describe Simplabs::ReportsAsSparkline::ReportCache do
Simplabs::ReportsAsSparkline::ReportCache.should_receive(:find).once.wit…
:all,
:conditions => [
- 'model_name = ? AND report_name = ? AND grouping = ? AND aggregation…
+ 'model_name = ? AND report_name = ? AND grouping = ? AND aggregation…
@report.klass.to_s,
@report.name.to_s,
@report.options[:grouping].identifier.to_s,
@report.aggregation.to_s,
+ 10,
Simplabs::ReportsAsSparkline::ReportingPeriod.first(@report.options[…
],
:limit => 10,
@@ -121,11 +114,12 @@ describe Simplabs::ReportsAsSparkline::ReportCache do
Simplabs::ReportsAsSparkline::ReportCache.should_receive(:find).once.wit…
:all,
:conditions => [
- 'model_name = ? AND report_name = ? AND grouping = ? AND aggregation…
+ 'model_name = ? AND report_name = ? AND grouping = ? AND aggregation…
@report.klass.to_s,
@report.name.to_s,
@report.options[:grouping].identifier.to_s,
@report.aggregation.to_s,
+ 10,
Simplabs::ReportsAsSparkline::ReportingPeriod.first(@report.options[…
Simplabs::ReportsAsSparkline::ReportingPeriod.new(@report.options[:g…
],
@@ -141,11 +135,12 @@ describe Simplabs::ReportsAsSparkline::ReportCache do
Simplabs::ReportsAsSparkline::ReportCache.should_receive(:find).once.wit…
:all,
:conditions => [
- 'model_name = ? AND report_name = ? AND grouping = ? AND aggregation…
+ 'model_name = ? AND report_name = ? AND grouping = ? AND aggregation…
@report.klass.to_s,
@report.name.to_s,
grouping.identifier.to_s,
@report.aggregation.to_s,
+ 10,
Simplabs::ReportsAsSparkline::ReportingPeriod.first(grouping, 10).da…
],
:limit => 10,
@@ -187,7 +182,7 @@ describe Simplabs::ReportsAsSparkline::ReportCache do
before do
@current_reporting_period = Simplabs::ReportsAsSparkline::ReportingPerio…
- @new_after_cache_data = [[@current_reporting_period.previous.date_time, …
+ @new_data = [[@current_reporting_period.previous.date_time, 1.0]]
Simplabs::ReportsAsSparkline::ReportingPeriod.stub!(:from_db_string).and…
@cached = Simplabs::ReportsAsSparkline::ReportCache.new
@cached.stub!(:save!)
@@ -198,38 +193,41 @@ describe Simplabs::ReportsAsSparkline::ReportCache do
Simplabs::ReportsAsSparkline::ReportCache.should_receive(:build_cached_d…
@report,
@report.options[:grouping],
+ 10,
anything(),
0.0
).and_return(@cached)
- Simplabs::ReportsAsSparkline::ReportCache.send(:prepare_result, [], [], …
+ Simplabs::ReportsAsSparkline::ReportCache.send(:prepare_result, [], [], …
end
it 'should create a new Simplabs::ReportsAsSparkline::ReportCache with the…
Simplabs::ReportsAsSparkline::ReportCache.should_receive(:build_cached_d…
@report,
@report.options[:grouping],
+ 10,
anything(),
0.0
).and_return(@cached)
Simplabs::ReportsAsSparkline::ReportCache.should_receive(:build_cached_d…
@report,
@report.options[:grouping],
+ 10,
@current_reporting_period.previous,
1.0
).and_return(@cached)
- Simplabs::ReportsAsSparkline::ReportCache.send(:prepare_result, [], @new…
+ Simplabs::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_dat…
end
it 'should save the created Simplabs::ReportsAsSparkline::ReportCache' do
@cached.should_receive(:save!).once
- Simplabs::ReportsAsSparkline::ReportCache.send(:prepare_result, [], @new…
+ Simplabs::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_dat…
end
it 'should return an array of arrays of Dates and Floats' do
- result = Simplabs::ReportsAsSparkline::ReportCache.send(:prepare_result,…
+ result = Simplabs::ReportsAsSparkline::ReportCache.send(:prepare_result,…
result.should be_kind_of(Array)
result[0].should be_kind_of(Array)
@@ -240,7 +238,7 @@ describe Simplabs::ReportsAsSparkline::ReportCache do
describe 'with :live_data = false' do
before do
- @result = Simplabs::ReportsAsSparkline::ReportCache.send(:prepare_resu…
+ @result = Simplabs::ReportsAsSparkline::ReportCache.send(:prepare_resu…
end
it 'should return an array of length :limit' do
@@ -257,7 +255,7 @@ describe Simplabs::ReportsAsSparkline::ReportCache do
before do
options = @report.options.merge(:live_data => true)
- @result = Simplabs::ReportsAsSparkline::ReportCache.send(:prepare_resu…
+ @result = Simplabs::ReportsAsSparkline::ReportCache.send(:prepare_resu…
end
it 'should return an array of length (:limit + 1)' do
@@ -275,7 +273,7 @@ describe Simplabs::ReportsAsSparkline::ReportCache do
it 'should not save the created Simplabs::ReportsAsSparkline::ReportCach…
@cached.should_not_receive(:save!)
- Simplabs::ReportsAsSparkline::ReportCache.send(:prepare_result, [], @n…
+ Simplabs::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_d…
end
end
diff --git a/spec/classes/report_spec.rb b/spec/classes/report_spec.rb
@@ -228,6 +228,28 @@ describe Simplabs::ReportsAsSparkline::Report do
result[6][1].should == 0.0
end
+ it 'should return correct results when run twice with different li…
+ @report = Simplabs::ReportsAsSparkline::Report.new(User, :regist…
+ :aggregation => :count,
+ :grouping => grouping,
+ :limit => 10,
+ :live_data => live_data
+ )
+ result = @report.run(:limit => 2).to_a
+
+ result[2][1].should == 1.0 if live_data
+ result[1][1].should == 1.0
+ result[0][1].should == 0.0
+
+ result = @report.run(:limit => 10).to_a
+
+ result[10][1].should == 1.0 if live_data
+ result[9][1].should == 1.0
+ result[8][1].should == 0.0
+ result[7][1].should == 2.0
+ result[6][1].should == 0.0
+ end
+
end
after(:all) do
diff --git a/spec/db/schema.rb b/spec/db/schema.rb
@@ -15,6 +15,7 @@ ActiveRecord::Schema.define(:version => 1) do
t.string :aggregation, :null => false
t.float :value, :null => false, :default => 0
t.datetime :reporting_period, :null => false
+ t.integer :run_limit, :null => false
t.timestamps
end
@@ -22,14 +23,16 @@ ActiveRecord::Schema.define(:version => 1) do
:model_name,
:report_name,
:grouping,
- :aggregation
- ], :name => :name_model_grouping_agregation
+ :aggregation,
+ :run_limit
+ ], :name => :name_model_grouping_agregation_run_limit
add_index :reports_as_sparkline_cache, [
:model_name,
:report_name,
:grouping,
:aggregation,
- :reporting_period
- ], :unique => true, :name => :name_model_grouping_aggregation_period
+ :reporting_period,
+ :run_limit
+ ], :unique => true, :name => :name_model_grouping_aggregation_period_run_lim…
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.