lib/saulabs/reportable/report_cache.rb in reportable-1.1.2 vs lib/saulabs/reportable/report_cache.rb in reportable-1.2.0

- old
+ new

@@ -1,18 +1,19 @@ require 'saulabs/reportable/reporting_period' require 'saulabs/reportable/result_set' +require 'active_record' module Saulabs module Reportable # The +ReportCache+ class is a regular +ActiveRecord+ model and represents cached results for single {Saulabs::Reportable::ReportingPeriod}s. # +ReportCache+ instances are identified by the combination of +model_name+, +report_name+, +grouping+, +aggregation+ and +reporting_period+. # class ReportCache < ActiveRecord::Base - set_table_name :reportable_cache + self.table_name = :reportable_cache validates_presence_of :model_name validates_presence_of :report_name validates_presence_of :grouping validates_presence_of :aggregation @@ -61,11 +62,11 @@ # @option options [Boolean] :live_data (false) # 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. # - # @return [Array<Array<DateTime, Float>>] + # @return [ResultSet<Array<DateTime, Float>>] # the result of the report as pairs of {DateTime}s and {Float}s # def self.process(report, options, &block) raise ArgumentError.new('A block must be given') unless block_given? self.transaction do @@ -108,25 +109,36 @@ self.new( :model_name => report.klass.to_s, :report_name => report.name.to_s, :grouping => grouping.identifier.to_s, :aggregation => report.aggregation.to_s, - :conditions => conditions.to_s, + :conditions => serialize_conditions(conditions), :reporting_period => reporting_period.date_time, :value => value ) end + def self.serialize_conditions(conditions) + if conditions.is_a?(Array) && conditions.any? + conditions.join + elsif conditions.is_a?(Hash) && conditions.any? + conditions.map.sort{|x,y|x.to_s<=>y.to_s}.flatten.join + else + conditions.empty? ? '' : conditions.to_s + end + end + def self.read_cached_data(report, options) + options[:conditions] ||= [] conditions = [ %w(model_name report_name grouping aggregation conditions).map do |column_name| "#{self.connection.quote_column_name(column_name)} = ?" end.join(' AND '), report.klass.to_s, report.name.to_s, options[:grouping].identifier.to_s, report.aggregation.to_s, - options[:conditions].to_s + serialize_conditions(options[:conditions]) ] first_reporting_period = get_first_reporting_period(options) last_reporting_period = get_last_reporting_period(options) if last_reporting_period conditions.first << ' AND reporting_period BETWEEN ? AND ?'