Class: Saulabs::Reportable::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/saulabs/reportable/report.rb

Overview

The Report class that does all the data retrieval and calculations.

Direct Known Subclasses

CumulatedReport

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, name, options = {}) ⇒ Report

Initializes a new Saulabs::Reportable::Report

Parameters:

  • klass (Class)

    the model the report works on (This is the class you invoke ClassMethods#reportable on)

  • name (String)

    the name of the report (as in ClassMethods#reportable)

  • options (Hash) (defaults to: {})

    options for the report creation

Options Hash (options):

  • :date_column (Symbol) — default: created_at

    the name of the date column over that the records are aggregated

  • :value_column (String, Symbol) — default: :id

    the name of the column that holds the values to aggregate when using a calculation aggregation like :sum

  • :aggregation (Symbol) — default: :count

    the aggregation to use (one of :count, :sum, :minimum, :maximum or :average); when using anything other than :count, :value_column must also be specified

  • :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;

  • :include (Hash) — default: {}

    include like in ActiveRecord::Base#find; names associations that should be loaded alongside; the symbols named refer to already defined associations

  • :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.



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

def initialize(klass, name, options = {})
  ensure_valid_options(options)
  @klass        = klass
  @name         = name
  @date_column  = (options[:date_column] || 'created_at').to_s
  @aggregation  = options[:aggregation] || :count
  @value_column = (options[:value_column] || (@aggregation == :count ? 'id' : name)).to_s
  @options = {
    :limit      => options[:limit] || 100,
    :distinct   => options[:distinct] || false,
    :include    => options[:include] || [],
    :conditions => options[:conditions] || [],
    :grouping   => Grouping.new(options[:grouping] || :day),
    :live_data  => options[:live_data] || false,
    :end_date   => options[:end_date] || false
  }
  @options.merge!(options)
  @options.freeze
end

Instance Attribute Details

#aggregationObject (readonly)

the aggregation to use (one of :count, :sum, :minimum, :maximum or :average); when using anything other than :count, :value_column must also be specified



30
31
32
# File 'lib/saulabs/reportable/report.rb', line 30

def aggregation
  @aggregation
end

#date_columnObject (readonly)

the name of the date column over that the records are aggregated



22
23
24
# File 'lib/saulabs/reportable/report.rb', line 22

def date_column
  @date_column
end

#klassObject (readonly)

the model the report works on (This is the class you invoke ClassMethods#reportable on)



14
15
16
# File 'lib/saulabs/reportable/report.rb', line 14

def klass
  @klass
end

#nameObject (readonly)

the name of the report (as in ClassMethods#reportable)



18
19
20
# File 'lib/saulabs/reportable/report.rb', line 18

def name
  @name
end

#optionsObject (readonly)

options for the report



34
35
36
# File 'lib/saulabs/reportable/report.rb', line 34

def options
  @options
end

#value_columnObject (readonly)

the name of the column that holds the values to aggregate when using a calculation aggregation like :sum



26
27
28
# File 'lib/saulabs/reportable/report.rb', line 26

def value_column
  @value_column
end

Instance Method Details

#run(options = {}) ⇒ Array<Array<DateTime, Float>>

Runs the report and returns an array of array of DateTimes and Floats

Parameters:

  • options (Hash) (defaults to: {})

    options to run the report with

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:

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

    the result of the report as pairs of DateTimes and Floats



103
104
105
106
107
108
# File 'lib/saulabs/reportable/report.rb', line 103

def run(options = {})
  options = options_for_run(options)
  ReportCache.process(self, options) do |begin_at, end_at|
    read_data(begin_at, end_at, options)
  end
end