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;

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/saulabs/reportable/report.rb', line 59

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,
    :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



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

def aggregation
  @aggregation
end

#date_columnObject (readonly)

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



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

def date_column
  @date_column
end

#klassObject (readonly)

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



11
12
13
# File 'lib/saulabs/reportable/report.rb', line 11

def klass
  @klass
end

#nameObject (readonly)

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



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

def name
  @name
end

#optionsObject (readonly)

options for the report



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

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



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

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



96
97
98
99
100
101
# File 'lib/saulabs/reportable/report.rb', line 96

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