Sha256: 444e0e375c036b29d6758728dcbdf0d9464e2493e96e8f9c277c017ebb73b34a
Contents?: true
Size: 1.42 KB
Versions: 13
Compression:
Stored size: 1.42 KB
Contents
module Workarea module Reports class FirstTimeVsReturningSales include Report include GroupByTime self.reporting_class = Metrics::SalesByDay self.sort_fields = %w(_id orders first_time_orders returning_orders percent_returning) def aggregation [filter, project_used_fields, group_by_time, add_calculated_fields] end def filter { '$match' => { 'reporting_on' => { '$gte' => starts_at, '$lte' => ends_at }, 'orders' => { '$gt' => 0 } } } end def project_used_fields { '$project' => { 'reporting_on' => 1, 'orders' => 1, 'returning_orders' => 1 } } end def group_by_time { '$group' => { '_id' => time_group_id, 'starts_at' => { '$min' => '$reporting_on' }, 'orders' => { '$sum' => '$orders' }, 'returning_orders' => { '$sum' => '$returning_orders' }, } } end def add_calculated_fields { '$addFields' => { 'first_time_orders' => { '$subtract' => ['$orders', '$returning_orders'] }, 'percent_returning' => { '$multiply' => [ { '$divide' => ['$returning_orders', '$orders'] }, 100 ] } } } end end end end
Version data entries
13 entries across 13 versions & 1 rubygems