Sha256: 94d4aae8d3ebe21edc0d904ceadc05027e0e494cae2667db7cb08b2ce741ed03

Contents?: true

Size: 1.07 KB

Versions: 49

Compression:

Stored size: 1.07 KB

Contents

module Workarea
  module Reports
    class AverageOrderValue
      include Report
      include GroupByTime

      self.reporting_class = Metrics::SalesByDay
      self.sort_fields = %w(_id revenue orders average_order_value)

      def aggregation
        [filter_date_range_and_zeroes, group_by_time, add_average_order_value]
      end

      def filter_date_range_and_zeroes
        {
          '$match' => {
            'reporting_on' => { '$gte' => starts_at.utc, '$lte' => ends_at.utc },
            'orders' => { '$gt' => 0 },
            'revenue' => { '$gt' => 0 }
          }
        }
      end

      def group_by_time
        {
          '$group' => {
            '_id' => time_group_id,
            'starts_at' => { '$min' => '$reporting_on' },
            'orders' => { '$sum' => '$orders' },
            'revenue' => { '$sum' => '$revenue' }
          }
        }
      end

      def add_average_order_value
        {
          '$addFields' => {
            'average_order_value' => { '$divide' => ['$revenue', '$orders'] }
          }
        }
      end
    end
  end
end

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.5.26 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.4.45 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.5.25 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.5.23 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.4.44 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.5.22 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.4.43 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.5.21 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.4.42 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.5.20 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.4.41 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.5.19 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.4.40 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.5.18 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.4.39 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.5.17 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.4.38 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.5.16 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.4.37 app/queries/workarea/reports/average_order_value.rb