Class: Saulabs::Reportable::ReportCache

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/saulabs/reportable/report_cache.rb

Overview

The ReportCache class is a regular ActiveRecord model and represents cached results for single ReportingPeriods. ReportCache instances are identified by the combination of model_class_name, report_name, grouping, aggregation and reporting_period.

Class Method Summary collapse

Class Method Details

.clear_for(klass, report) ⇒ Object

Clears the cache for the specified klass and report

Examples:

Clearing the cache for a report


class User < ActiveRecord::Base
  reportable :registrations
end

Saulabs::Reportable::ReportCache.clear_for(User, :registrations)

Parameters:

  • klass (Class)

    the model the report to clear the cache for works on

  • report (Symbol)

    the name of the report to clear the cache for



42
43
44
# File 'lib/saulabs/reportable/report_cache.rb', line 42

def self.clear_for(klass, report)
  self.where(model_class_name: klass.name, report_name: report.to_s).delete_all
end

.process(report, options, &block) ⇒ ResultSet<Array<DateTime, Float>>

Processes the report using the respective cache.

Parameters:

  • report (Saulabe::Reportable::Report)

    the report to process

  • options (Hash)

    options for the report

Options Hash (options):

  • :grouping (Symbol) — default: :day

    the period records are grouped in (:hour, :day, :week, :month); Beware that reportable treats weeks as starting on monday!

  • :limit (Fixnum) — default: 100

    the number of reporting periods to get (see :grouping)

  • :conditions (Hash) — default: {}

    conditions like in ActiveRecord::Base#find; only records that match these conditions are reported;

  • :live_data (Boolean) — default: false

    specifies whether data for the current reporting period is to be read; if :live_data is true, you will experience a performance hit since the request cannot be satisfied from the cache alone

  • :end_date (DateTime, Boolean) — default: false

    when specified, the report will only include data for the :limit reporting periods until this date.

Returns:

  • (ResultSet<Array<DateTime, Float>>)

    the result of the report as pairs of DateTimes and Floats

Raises:

  • (ArgumentError)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/saulabs/reportable/report_cache.rb', line 67

def self.process(report, options, &block)
  raise ArgumentError.new('A block must be given') unless block_given?

  # If end_date is in the middle of the current reporting period it means it requests live_data.
  # Update the options hash to reflect reality.
  current_reporting_period = ReportingPeriod.new(options[:grouping])
  if options[:end_date] && options[:end_date] > current_reporting_period.date_time
    options[:live_data] = true
    options.delete(:end_date)
  end

  self.transaction do
    cached_data = read_cached_data(report, options)
    new_data = read_new_data(cached_data, options, &block)
    prepare_result(new_data, cached_data, report, options)
  end
end