Class: Workarea::Reports::AverageOrderValue

Inherits:
Object
  • Object
show all
Includes:
GroupByTime, Report
Defined in:
app/queries/workarea/reports/average_order_value.rb

Instance Method Summary collapse

Methods included from GroupByTime

#group_by, #time_group_id

Methods included from Report

#cache_key, #count, #ends_at, #initialize, #limit, #more_results?, #results, #slug, #sort, #sort_by, #sort_direction, #sort_value, #starts_at

Instance Method Details

#add_average_order_valueObject



35
36
37
38
39
40
41
# File 'app/queries/workarea/reports/average_order_value.rb', line 35

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

#aggregationObject



10
11
12
# File 'app/queries/workarea/reports/average_order_value.rb', line 10

def aggregation
  [filter_date_range_and_zeroes, group_by_time, add_average_order_value]
end

#filter_date_range_and_zeroesObject



14
15
16
17
18
19
20
21
22
# File 'app/queries/workarea/reports/average_order_value.rb', line 14

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

#group_by_timeObject



24
25
26
27
28
29
30
31
32
33
# File 'app/queries/workarea/reports/average_order_value.rb', line 24

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