Merge branch 'add-cacheable-option' - reportable - Fork of reportable required by WarVox, from hdm/reportable.
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
(DIR) commit f8532eeb704c511bb75b5bd4fd9656243c9b118b
(DIR) parent 96961296ad28658ef018bea8bafb27fd5fe4db00
(HTM) Author: HD Moore <hd_moore@rapid7.com>
Date: Sat, 12 Jan 2013 20:41:04 -0600
Merge branch 'add-cacheable-option'
Diffstat:
M lib/saulabs/reportable/report.rb | 10 ++++++++--
M lib/saulabs/reportable/report_cach… | 14 ++++++++++----
M spec/classes/report_spec.rb | 6 +++---
3 files changed, 21 insertions(+), 9 deletions(-)
---
(DIR) diff --git a/lib/saulabs/reportable/report.rb b/lib/saulabs/reportable/report.rb
@@ -58,6 +58,8 @@ module Saulabs
# specifies whether data for the current reporting period is to be read; <b>if +:live_data+ is +true+, you will experience a performance hit since the request cannot be satisfied from the cache alone</b>
# @option options [DateTime, Boolean] :end_date (false)
# when specified, the report will only include data for the +:limit+ reporting periods until this date.
+ # @option options [Boolean] :cacheable (true)
+ # when set to false, the report will never use the cache, which allows reuse of a named report with different conditions
#
def initialize(klass, name, options = {})
ensure_valid_options(options)
@@ -72,7 +74,8 @@ module Saulabs
:conditions => options[:conditions] || [],
:grouping => Grouping.new(options[:grouping] || :day),
:live_data => options[:live_data] || false,
- :end_date => options[:end_date] || false
+ :end_date => options[:end_date] || false,
+ :cacheable => ( options[:cacheable] == false ? false : true )
}
@options.merge!(options)
@options.freeze
@@ -94,6 +97,9 @@ module Saulabs
# @option options [DateTime, Boolean] :end_date (false)
# when specified, the report will only include data for the +:limit+ reporting periods until this date.
#
+ # @option options [Boolean] :cacheable (true)
+ # when set to false, the report will never use the cache, which allows reuse of a named report with different conditions
+ #
# @return [Array<Array<DateTime, Float>>]
# the result of the report as pairs of {DateTime}s and {Float}s
#
@@ -147,7 +153,7 @@ module Saulabs
case context
when :initialize
options.each_key do |k|
- raise ArgumentError.new("Invalid option #{k}!") unless [:limit, :aggregation, :grouping, :distinct, :date_column, :value_column, :conditions, :live_data, :end_date].include?(k)
+ raise ArgumentError.new("Invalid option #{k}!") unless [:limit, :aggregation, :grouping, :distinct, :date_column, :value_column, :conditions, :live_data, :end_date, :cacheable].include?(k)
end
raise ArgumentError.new("Invalid aggregation #{options[:aggregation]}!") if options[:aggregation] && ![:count, :sum, :maximum, :minimum, :average].include?(options[:aggregation])
raise ArgumentError.new('The name of the column holding the value to sum has to be specified for aggregation :sum!') if [:sum, :maximum, :minimum, :average].include?(options[:aggregation]) && !options.key?(:value_column)
(DIR) diff --git a/lib/saulabs/reportable/report_cache.rb b/lib/saulabs/reportable/report_cache.rb
@@ -63,6 +63,8 @@ module Saulabs
# specifies whether data for the current reporting period is to be read; <b>if +:live_data+ is +true+, you will experience a performance hit since the request cannot be satisfied from the cache alone</b>
# @option options [DateTime, Boolean] :end_date (false)
# when specified, the report will only include data for the +:limit+ reporting periods until this date.
+ # @option options [Boolean] :cacheable (true)
+ # when set to false, the report will never use the cache, which allows reuse of a named report with different conditions
#
# @return [ResultSet<Array<DateTime, Float>>]
# the result of the report as pairs of {DateTime}s and {Float}s
@@ -94,12 +96,15 @@ module Saulabs
reporting_period = get_first_reporting_period(options)
result = []
while reporting_period < (options[:end_date] ? ReportingPeriod.new(options[:grouping], options[:end_date]).next : current_reporting_period)
- if cached = cached_data.find { |cached| reporting_period == cached[0] }
+ if options[:cacheable] and cached = cached_data.find { |cached| reporting_period == cached[0] }
result << [cached[0].date_time, cached[1]]
else
- new_cached = build_cached_data(report, options[:grouping], options[:conditions], reporting_period, find_value(new_data, reporting_period))
- new_cached.save!
- result << [reporting_period.date_time, new_cached.value]
+ value = find_value(new_data, reporting_period)
+ if options[:cacheable]
+ new_cached = build_cached_data(report, options[:grouping], options[:conditions], reporting_period, value)
+ new_cached.save! if options[:cacheable]
+ end
+ result << [reporting_period.date_time, value]
end
reporting_period = reporting_period.next
end
@@ -137,6 +142,7 @@ module Saulabs
end
def self.read_cached_data(report, options)
+ return [] if not options[:cacheable]
options[:conditions] ||= []
conditions = [
%w(model_name report_name grouping aggregation conditions).map do |column_name|
(DIR) diff --git a/spec/classes/report_spec.rb b/spec/classes/report_spec.rb
@@ -21,7 +21,7 @@ describe Saulabs::Reportable::Report do
it 'should process the data with the report cache' do
Saulabs::Reportable::ReportCache.should_receive(:process).once.with(
@report,
- { :limit => 100, :grouping => @report.options[:grouping], :conditions => [], :live_data => false, :end_date => false, :distinct => false }
+ { :limit => 100, :grouping => @report.options[:grouping], :conditions => [], :live_data => false, :end_date => false, :distinct => false, :cacheable => true }
)
@report.run
@@ -30,7 +30,7 @@ describe Saulabs::Reportable::Report do
it 'should process the data with the report cache when custom conditions are given' do
Saulabs::Reportable::ReportCache.should_receive(:process).once.with(
@report,
- { :limit => 100, :grouping => @report.options[:grouping], :conditions => { :some => :condition }, :live_data => false, :end_date => false, :distinct => false }
+ { :limit => 100, :grouping => @report.options[:grouping], :conditions => { :some => :condition }, :live_data => false, :end_date => false, :distinct => false, :cacheable => true }
)
@report.run(:conditions => { :some => :condition })
@@ -47,7 +47,7 @@ describe Saulabs::Reportable::Report do
Saulabs::Reportable::Grouping.should_receive(:new).once.with(:month).and_return(grouping)
Saulabs::Reportable::ReportCache.should_receive(:process).once.with(
@report,
- { :limit => 100, :grouping => grouping, :conditions => [], :live_data => false, :end_date => false, :distinct => false }
+ { :limit => 100, :grouping => grouping, :conditions => [], :live_data => false, :end_date => false, :distinct => false, :cacheable => true }
)
@report.run(:grouping => :month)