Class: Workarea::Metrics::SearchForLastWeek

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

Class Method Summary collapse

Methods included from Mongoid::Document

#embedded_children

Class Method Details

.add_calculated_fieldsObject



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
143
144
145
146
147
# File 'app/models/workarea/metrics/search_for_last_week.rb', line 106

def add_calculated_fields
  {
    '$addFields' => {
      'query_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' => ['$searches', 0] },
              0,
              { '$divide' => ['$orders', '$searches'] }
            ]
          }
        ]
      }
    }
  }
end

.add_idObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/models/workarea/metrics/search_for_last_week.rb', line 149

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

.add_prior_weekObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/workarea/metrics/search_for_last_week.rb', line 69

def add_prior_week
  {
    '$lookup' => {
      'from' => SearchByWeek.collection.name,
      'let' => { 'query_id' => '$_id' },
      'pipeline' => [
        {
          '$match' => {
            '$expr' => {
              '$and' => [
                { '$eq' => ['$query_id', '$$query_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_searches_percentilesObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/models/workarea/metrics/search_for_last_week.rb', line 173

def add_searches_percentiles
  percentiles = CalculatePercentiles.new(collection, :searches)

  collection.aggregate([
    {
      '$addFields' => {
        'searches_percentile' => {
          '$switch' => {
            'branches' => 99.downto(0).map do |percentile|
              {
                'case' => { '$gte' => ['$searches', 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/search_for_last_week.rb', line 13

def aggregate!
  delete_all
  aggregate_last_week
  add_searches_percentiles
end

.aggregate_last_weekObject



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

def aggregate_last_week
  SearchByDay
    .collection
    .aggregate([
      filter_by_date_range,
      sort_by_most_usable_values,
      group_by_query,
      add_prior_week,
      merge_prior_week_data,
      add_calculated_fields,
      add_id,
      out
    ])
    .first
end

.filter_by_date_rangeObject



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

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_queryObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/workarea/metrics/search_for_last_week.rb', line 50

def group_by_query
  {
    '$group' => {
      '_id' => '$query_id',
      'query_string' => { '$first' => '$query_string' },
      'total_results' => { '$first' => '$total_results' },
      'searches' => { '$sum' => '$searches' },
      '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' }
    }
  }
end

.merge_prior_week_dataObject



93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/workarea/metrics/search_for_last_week.rb', line 93

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

.outObject



169
170
171
# File 'app/models/workarea/metrics/search_for_last_week.rb', line 169

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

.sort_by_most_usable_valuesObject



46
47
48
# File 'app/models/workarea/metrics/search_for_last_week.rb', line 46

def sort_by_most_usable_values
  { '$sort' => { 'query_string' => -1, 'total_results' => -1 } }
end