Module: Workarea::Metrics::UpdateUserAggregations
- Extended by:
- UpdateUserAggregations
- Included in:
- UpdateUserAggregations
- Defined in:
- app/models/workarea/metrics/update_user_aggregations.rb
Instance Method Summary collapse
- #update! ⇒ Object
- #update_calculated_fields! ⇒ Object
- #update_percentiles! ⇒ Object
- #update_percentiles_expression(field) ⇒ Object
Instance Method Details
#update! ⇒ Object
6 7 8 9 |
# File 'app/models/workarea/metrics/update_user_aggregations.rb', line 6 def update! update_calculated_fields! update_percentiles! end |
#update_calculated_fields! ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'app/models/workarea/metrics/update_user_aggregations.rb', line 11 def update_calculated_fields! User.collection.aggregate([ { '$addFields' => { 'frequency' => { '$cond' => [ { '$eq' => ['$orders', 0] }, nil, { '$divide' => [ '$orders', { '$subtract' => [Time.current.utc, '$first_order_at'] } ] } ] }, 'average_order_value' => { '$cond' => [ { '$eq' => ['$orders', 0] }, nil, { '$divide' => ['$revenue', '$orders'] } ] } } }, { '$out' => User.collection.name } ]).first end |
#update_percentiles! ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/models/workarea/metrics/update_user_aggregations.rb', line 40 def update_percentiles! User.collection.aggregate([ { '$addFields' => { 'orders_percentile' => update_percentiles_expression(:orders), 'frequency_percentile' => update_percentiles_expression(:frequency), 'revenue_percentile' => update_percentiles_expression(:revenue), 'average_order_value_percentile' => update_percentiles_expression(:average_order_value) } }, { '$out' => User.collection.name } ]).first end |
#update_percentiles_expression(field) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/models/workarea/metrics/update_user_aggregations.rb', line 54 def update_percentiles_expression(field) percentiles = CalculatePercentiles.new(User.collection, field) { '$cond' => [ { '$eq' => ['$orders', 0] }, nil, { '$switch' => { 'branches' => 99.downto(1).map do |percentile| { 'case' => { '$gte' => ["$#{field}", percentiles[percentile.to_s]] }, 'then' => percentile + 1 } end, 'default' => 1 } } ] } end |