Sha256: e34c1700bf8190f1d7105f3353c2e941e2556f612522289cc742fe3f6f382326
Contents?: true
Size: 1.89 KB
Versions: 23
Compression:
Stored size: 1.89 KB
Contents
module Workarea module Reports class SalesOverTime include Report include GroupByTime self.reporting_class = Metrics::SalesByDay self.sort_fields = %w(_id units_sold orders customers merchandise discounts shipping tax revenue) def aggregation [filter, project_used_fields, group_by_time, add_aov] end def filter { '$match' => { 'reporting_on' => { '$gte' => starts_at.utc, '$lte' => ends_at.utc }, 'orders' => { '$gt' => 0 } } } end def project_used_fields { '$project' => { 'reporting_on' => 1, 'orders' => 1, 'returning_orders' => 1, 'customers' => 1, 'units_sold' => 1, 'merchandise' => 1, 'shipping' => 1, 'discounts' => 1, 'tax' => 1, 'revenue' => 1 } } end def group_by_time { '$group' => { '_id' => time_group_id, 'starts_at' => { '$min' => '$reporting_on' }, 'orders' => { '$sum' => '$orders' }, 'returning_orders' => { '$sum' => '$returning_orders' }, 'customers' => { '$sum' => '$customers' }, 'units_sold' => { '$sum' => '$units_sold' }, 'merchandise' => { '$sum' => '$merchandise' }, 'shipping' => { '$sum' => '$shipping' }, 'discounts' => { '$sum' => '$discounts' }, 'tax' => { '$sum' => '$tax' }, 'revenue' => { '$sum' => '$revenue' } } } end def add_aov { '$addFields' => { 'aov' => { '$cond' => [ { '$eq' => ['$orders', 0] }, 0, { '$divide' => ['$revenue', '$orders'] } ] } } } end end end end
Version data entries
23 entries across 23 versions & 1 rubygems