Sha256: 6e9b3a791e29d658051a85de81ee5384bf7465f930e86787007e99fc0f59de58

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

require 'saulabs/reportable/report'

module Saulabs

  module Reportable

    # A special report class that cumulates all data (see {Saulabs::Reportable::Report})
    #
    # @example Cumulated reports as opposed to regular reports
    #
    #  [[<DateTime today>, 1], [<DateTime yesterday>, 2], [<DateTime two days ago>, 4], etc.] # result of a regular report 
    #  [[<DateTime today>, 7], [<DateTime yesterday>, 6], [<DateTime two days ago>, 4], etc.] # result of a cumulated report for the same dataset
    #
    class CumulatedReport < Report

      # Runs the report (see {Saulabs::Reportable::Report#run})
      #
      def run(options = {})
        cumulate(super, options_for_run(options))
      end

      private

        def cumulate(data, options)
          first_reporting_period = ReportingPeriod.first(options[:grouping], options[:limit], options[:end_date])
          acc = initial_cumulative_value(first_reporting_period.date_time, options)
          result = []
          data.each do |element|
            acc += element[1].to_f
            result << [element[0], acc]
          end
          result
        end

        def initial_cumulative_value(date, options)
          conditions = setup_conditions(nil, date, options[:conditions])
          @klass.send(@aggregation, @value_column, :conditions => conditions)
        end

    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
reportable-1.1.2 lib/saulabs/reportable/cumulated_report.rb
reportable-1.1.0 lib/saulabs/reportable/cumulated_report.rb