Class: Workarea::Metrics::ProductForLastWeek

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Attributes::Dynamic, Mongoid::Document
Defined in:
app/models/workarea/metrics/product_for_last_week.rb

Class Method Summary collapse

Methods included from Mongoid::Document

#embedded_children

Class Method Details

.add_calculated_fieldsObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/models/workarea/metrics/product_for_last_week.rb', line 101

def add_calculated_fields
  {
    '$addFields' => {
      'product_id' => '$_id',
      'prior_week_revenue' => { '$ifNull' => ['$prior_week_revenue', 0] },
      'revenue_change' => {
        '$subtract' => ['$revenue', '$ifNull' => ['$prior_week_revenue', 0]]
      },
      'average_discount' => {
        '$cond' => [
          { '$eq' => ['$merchandise', 0] },
          0,
          { '$divide' => [{ '$abs' => '$discounts' }, '$merchandise'] }
        ]
      },
      'discount_rate' => {
        '$multiply' => [
          100,
          {
            '$cond' => [
              { '$eq' => ['$units_sold', 0] },
              0,
              { '$divide' => ['$discounted_units_sold', '$units_sold'] }
            ]
          }
        ]
      },
      'conversion_rate' => {
        '$multiply' => [
          100,
          {
            '$cond' => [
              { '$eq' => ['$views', 0] },
              0,
              { '$divide' => ['$orders', '$views'] }
            ]
          }
        ]
      }
    }
  }
end

.add_idObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/models/workarea/metrics/product_for_last_week.rb', line 144

def add_id
  {
    '$addFields' => {
      '_id' => {
        '$concat' => [
          {
            '$dateToString' => {
              'format' => '%Y%m%d',
              'date' => '$reporting_on',
              'timezone' => Time.zone.tzinfo.name
            }
          },
          '-',
          '$product_id'
        ]
      }
    }
  }
end

.add_prior_weekObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/workarea/metrics/product_for_last_week.rb', line 64

def add_prior_week
  {
    '$lookup' => {
      'from' => ProductByWeek.collection.name,
      'let' => { 'product_id' => '$_id' },
      'pipeline' => [
        {
          '$match' => {
            '$expr' => {
              '$and' => [
                { '$eq' => ['$product_id', '$$product_id'] },
                { '$gte' => ['$reporting_on', (Time.current.last_week - 1.week).utc] },
                { '$lte' => ['$reporting_on', (Time.current.last_week.end_of_week - 1.week).utc] }
              ]
            }
          }
        },
        { '$project' => { 'prior_week_revenue' => '$revenue' } }
      ],
      'as' => 'prior_week'
    }
  }
end

.add_view_percentilesObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/models/workarea/metrics/product_for_last_week.rb', line 168

def add_view_percentiles
  percentiles = CalculatePercentiles.new(collection, :views)

  collection.aggregate([
    {
      '$addFields' => {
        'views_percentile' => {
          '$switch' => {
            'branches' => 99.downto(0).map do |percentile|
              {
                'case' => { '$gte' => ['$views', percentiles[percentile.to_s]] },
                'then' => percentile + 1
              }
            end,
            'default' => 0
          }
        }
      }
    },
    out
  ]).first
end

.aggregate!Object



13
14
15
16
17
# File 'app/models/workarea/metrics/product_for_last_week.rb', line 13

def aggregate!
  delete_all
  aggregate_last_week
  add_view_percentiles
end

.aggregate_last_weekObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/workarea/metrics/product_for_last_week.rb', line 19

def aggregate_last_week
  ProductByDay
    .collection
    .aggregate([
      filter_by_date_range,
      group_by_product,
      add_prior_week,
      merge_prior_week_data,
      add_calculated_fields,
      add_id,
      out
    ])
    .first
end

.filter_by_date_rangeObject



34
35
36
37
38
39
40
41
42
43
# File 'app/models/workarea/metrics/product_for_last_week.rb', line 34

def filter_by_date_range
  {
    '$match' => {
      'reporting_on' => {
        '$gte' => Time.current.last_week.utc,
        '$lte' => Time.current.last_week.end_of_week.utc
      }
    }
  }
end

.group_by_productObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/workarea/metrics/product_for_last_week.rb', line 45

def group_by_product
  {
    '$group' => {
      '_id' => '$product_id',
      'views' => { '$sum' => '$views' },
      'orders' => { '$sum' => '$orders' },
      'units_sold' => { '$sum' => '$units_sold' },
      'discounted_units_sold' => { '$sum' => '$discounted_units_sold' },
      'merchandise' => { '$sum' => '$merchandise' },
      'discounts' => { '$sum' => '$discounts' },
      'tax' => { '$sum' => '$tax' },
      'revenue' => { '$sum' => '$revenue' },
      'reporting_on' => { '$min' => '$reporting_on' },
      'units_canceled' => { '$sum' => '$units_canceled' },
      'refund' => { '$sum' => '$refund' }
    }
  }
end

.merge_prior_week_dataObject



88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/workarea/metrics/product_for_last_week.rb', line 88

def merge_prior_week_data
  {
    '$replaceRoot' => {
      'newRoot' => {
        '$mergeObjects' => [
          { '$arrayElemAt' => ['$prior_week', 0] },
          '$$ROOT'
        ]
      }
    }
  }
end

.outObject



164
165
166
# File 'app/models/workarea/metrics/product_for_last_week.rb', line 164

def out
  { '$out' => collection.name }
end