Cache results even if conditions are given. - reportable - Unnamed repository; … | |
Log | |
Files | |
Refs | |
README | |
--- | |
commit 3259cb4e113ad36fcd708d896622deb54ae9c608 | |
parent 51b49004e197edbced94eb626c6eed58cc7a4e72 | |
Author: Eric Lindvall <[email protected]> | |
Date: Wed, 23 Dec 2009 16:47:50 +0800 | |
Cache results even if conditions are given. | |
This adds a column to the migration that will need to be added for this plugin | |
to work. | |
Diffstat: | |
M generators/reports_as_sparkline_mi… | 5 ++++- | |
M lib/simplabs/reports_as_sparkline/… | 5 ++--- | |
M lib/simplabs/reports_as_sparkline/… | 23 +++++++++++------------ | |
M spec/classes/report_cache_spec.rb | 48 ++++++++---------------------… | |
M spec/classes/report_spec.rb | 11 ++++------- | |
M spec/db/schema.rb | 9 ++++++--- | |
6 files changed, 39 insertions(+), 62 deletions(-) | |
--- | |
diff --git a/generators/reports_as_sparkline_migration/templates/migration.erb … | |
@@ -6,6 +6,7 @@ class <%= class_name %> < ActiveRecord::Migration | |
t.string :report_name, :null => false | |
t.string :grouping, :null => false | |
t.string :aggregation, :null => false | |
+ t.string :condition, :null => false | |
t.float :value, :null => false, :default => 0 | |
t.datetime :reporting_period, :null => false | |
@@ -16,13 +17,15 @@ class <%= class_name %> < ActiveRecord::Migration | |
:model_name, | |
:report_name, | |
:grouping, | |
- :aggregation | |
+ :aggregation, | |
+ :condition | |
], :name => :name_model_grouping_agregation | |
add_index :reports_as_sparkline_cache, [ | |
:model_name, | |
:report_name, | |
:grouping, | |
:aggregation, | |
+ :condition, | |
:reporting_period | |
], :unique => true, :name => :name_model_grouping_aggregation_period | |
end | |
diff --git a/lib/simplabs/reports_as_sparkline/report.rb b/lib/simplabs/reports… | |
@@ -44,13 +44,12 @@ module Simplabs #:nodoc: | |
# ==== Options | |
# * <tt>:grouping</tt> - The period records are grouped on (<tt>:hour</t… | |
# * <tt>:limit</tt> - The number of reporting periods to get (see <tt>:g… | |
- # * <tt>:conditions</tt> - Conditions like in <tt>ActiveRecord::Base#fin… | |
+ # * <tt>:conditions</tt> - Conditions like in <tt>ActiveRecord::Base#fin… | |
# * <tt>:live_data</tt> - Specifies whether data for the current reporti… | |
# * <tt>:end_date</tt> - When specified, the report will only include da… | |
def run(options = {}) | |
- custom_conditions = options.key?(:conditions) | |
options = options_for_run(options) | |
- ReportCache.process(self, options, !custom_conditions) do |begin_at, e… | |
+ ReportCache.process(self, options) do |begin_at, end_at| | |
read_data(begin_at, end_at, options) | |
end | |
end | |
diff --git a/lib/simplabs/reports_as_sparkline/report_cache.rb b/lib/simplabs/r… | |
@@ -30,21 +30,18 @@ module Simplabs #:nodoc: | |
}) | |
end | |
- def self.process(report, options, cache = true, &block) #:nodoc: | |
+ def self.process(report, options, &block) #:nodoc: | |
raise ArgumentError.new('A block must be given') unless block_given? | |
self.transaction do | |
- cached_data = [] | |
- if cache | |
- cached_data = read_cached_data(report, options) | |
- end | |
+ cached_data = read_cached_data(report, options) | |
new_data = read_new_data(cached_data, options, &block) | |
- prepare_result(new_data, cached_data, report, options, cache) | |
+ prepare_result(new_data, cached_data, report, options) | |
end | |
end | |
private | |
- def self.prepare_result(new_data, cached_data, report, options, cache … | |
+ def self.prepare_result(new_data, cached_data, report, options) | |
new_data = new_data.map { |data| [ReportingPeriod.from_db_string(opt… | |
cached_data.map! { |cached| [ReportingPeriod.new(options[:grouping],… | |
current_reporting_period = ReportingPeriod.current(options[:grouping… | |
@@ -54,8 +51,8 @@ module Simplabs #:nodoc: | |
if cached = cached_data.find { |cached| reporting_period == cached… | |
result << [cached[0].date_time, cached[1]] | |
else | |
- new_cached = build_cached_data(report, options[:grouping], repor… | |
- new_cached.save! if cache | |
+ new_cached = build_cached_data(report, options[:grouping], optio… | |
+ new_cached.save! | |
result << [reporting_period.date_time, new_cached.value] | |
end | |
reporting_period = reporting_period.next | |
@@ -71,12 +68,13 @@ 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, condition, reporting_peri… | |
self.new( | |
:model_name => report.klass.to_s, | |
:report_name => report.name.to_s, | |
:grouping => grouping.identifier.to_s, | |
:aggregation => report.aggregation.to_s, | |
+ :condition => condition.to_s, | |
:reporting_period => reporting_period.date_time, | |
:value => value | |
) | |
@@ -84,11 +82,12 @@ module Simplabs #:nodoc: | |
def self.read_cached_data(report, options) | |
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[:conditions].to_s | |
] | |
first_reporting_period = get_first_reporting_period(options) | |
last_reporting_period = get_last_reporting_period(options) | |
diff --git a/spec/classes/report_cache_spec.rb b/spec/classes/report_cache_spec… | |
@@ -122,11 +122,12 @@ describe Simplabs::ReportsAsSparkline::ReportCache do | |
it 'should read existing data from the cache' do | |
Simplabs::ReportsAsSparkline::ReportCache.should_receive(:all).once.with( | |
: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, | |
+ @report.options[:conditions].to_s, | |
Simplabs::ReportsAsSparkline::ReportingPeriod.first(@report.options[… | |
], | |
:limit => 10, | |
@@ -140,11 +141,12 @@ describe Simplabs::ReportsAsSparkline::ReportCache do | |
end_date = Time.now | |
Simplabs::ReportsAsSparkline::ReportCache.should_receive(:all).once.with( | |
: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, | |
+ @report.options[:conditions].to_s, | |
Simplabs::ReportsAsSparkline::ReportingPeriod.first(@report.options[… | |
Simplabs::ReportsAsSparkline::ReportingPeriod.new(@report.options[:g… | |
], | |
@@ -160,11 +162,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, | |
+ @report.options[:conditions].to_s, | |
Simplabs::ReportsAsSparkline::ReportingPeriod.first(grouping, 10).da… | |
], | |
:limit => 10, | |
@@ -181,25 +184,6 @@ describe Simplabs::ReportsAsSparkline::ReportCache do | |
[] | |
end | |
end | |
- | |
- describe 'with cache = false' do | |
- | |
- it 'should not read any data from cache' do | |
- Simplabs::ReportsAsSparkline::ReportCache.should_not_receive(:find) | |
- | |
- Simplabs::ReportsAsSparkline::ReportCache.process(@report, @report.opt… | |
- end | |
- | |
- it 'should yield the first reporting period' do | |
- Simplabs::ReportsAsSparkline::ReportCache.process(@report, @report.opt… | |
- begin_at.should == Simplabs::ReportsAsSparkline::ReportingPeriod.fir… | |
- end_at.should == nil | |
- [] | |
- end | |
- end | |
- | |
- end | |
- | |
end | |
describe '.prepare_result' do | |
@@ -217,6 +201,7 @@ describe Simplabs::ReportsAsSparkline::ReportCache do | |
Simplabs::ReportsAsSparkline::ReportCache.should_receive(:build_cached_d… | |
@report, | |
@report.options[:grouping], | |
+ @report.options[:conditions], | |
anything(), | |
0.0 | |
).and_return(@cached) | |
@@ -228,12 +213,14 @@ describe Simplabs::ReportsAsSparkline::ReportCache do | |
Simplabs::ReportsAsSparkline::ReportCache.should_receive(:build_cached_d… | |
@report, | |
@report.options[:grouping], | |
+ @report.options[:conditions], | |
anything(), | |
0.0 | |
).and_return(@cached) | |
Simplabs::ReportsAsSparkline::ReportCache.should_receive(:build_cached_d… | |
@report, | |
@report.options[:grouping], | |
+ @report.options[:conditions], | |
@current_reporting_period.previous, | |
1.0 | |
).and_return(@cached) | |
@@ -248,7 +235,7 @@ describe Simplabs::ReportsAsSparkline::ReportCache do | |
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) | |
@@ -259,7 +246,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 | |
@@ -276,7 +263,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 | |
@@ -288,17 +275,6 @@ describe Simplabs::ReportsAsSparkline::ReportCache do | |
end | |
end | |
- | |
- describe 'with cache = false' do | |
- | |
- it 'should not save the created Simplabs::ReportsAsSparkline::ReportCach… | |
- @cached.should_not_receive(:save!) | |
- | |
- Simplabs::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_d… | |
- end | |
- | |
- end | |
- | |
end | |
describe '.find_value' do | |
diff --git a/spec/classes/report_spec.rb b/spec/classes/report_spec.rb | |
@@ -21,18 +21,16 @@ describe Simplabs::ReportsAsSparkline::Report do | |
it 'should process the data with the report cache' do | |
Simplabs::ReportsAsSparkline::ReportCache.should_receive(:process).once.… | |
@report, | |
- { :limit => 100, :grouping => @report.options[:grouping], :conditions … | |
- true | |
+ { :limit => 100, :grouping => @report.options[:grouping], :conditions … | |
) | |
@report.run | |
end | |
- it 'should process the data with the report cache and specify cache = fals… | |
+ it 'should process the data with the report cache when custom conditions a… | |
Simplabs::ReportsAsSparkline::ReportCache.should_receive(:process).once.… | |
@report, | |
- { :limit => 100, :grouping => @report.options[:grouping], :conditions … | |
- false | |
+ { :limit => 100, :grouping => @report.options[:grouping], :conditions … | |
) | |
@report.run(:conditions => { :some => :condition }) | |
@@ -49,8 +47,7 @@ describe Simplabs::ReportsAsSparkline::Report do | |
Simplabs::ReportsAsSparkline::Grouping.should_receive(:new).once.with(:m… | |
Simplabs::ReportsAsSparkline::ReportCache.should_receive(:process).once.… | |
@report, | |
- { :limit => 100, :grouping => grouping, :conditions => [], :live_data … | |
- true | |
+ { :limit => 100, :grouping => grouping, :conditions => [], :live_data … | |
) | |
@report.run(:grouping => :month) | |
diff --git a/spec/db/schema.rb b/spec/db/schema.rb | |
@@ -13,6 +13,7 @@ ActiveRecord::Schema.define(:version => 1) do | |
t.string :report_name, :null => false | |
t.string :grouping, :null => false | |
t.string :aggregation, :null => false | |
+ t.string :condition, :null => false | |
t.float :value, :null => false, :default => 0 | |
t.datetime :reporting_period, :null => false | |
@@ -22,14 +23,16 @@ ActiveRecord::Schema.define(:version => 1) do | |
:model_name, | |
:report_name, | |
:grouping, | |
- :aggregation | |
- ], :name => :name_model_grouping_agregation_run_limit | |
+ :aggregation, | |
+ :condition | |
+ ], :name => :name_model_grouping_agregation | |
add_index :reports_as_sparkline_cache, [ | |
:model_name, | |
:report_name, | |
:grouping, | |
:aggregation, | |
+ :condition, | |
:reporting_period | |
- ], :unique => true, :name => :name_model_grouping_aggregation_period_run_lim… | |
+ ], :unique => true, :name => :name_model_grouping_aggregation_period | |
end |