Class: Workarea::Insights::TrendingProducts

Inherits:
Base
  • Object
show all
Defined in:
app/models/workarea/insights/trending_products.rb

Class Method Summary collapse

Methods inherited from Base

beginning_of_last_month, current, end_of_last_month, generate_daily!, generate_weekly!, #include?, #slug

Methods included from ApplicationDocument

#releasable?

Methods included from Sidekiq::Callbacks

assert_valid_config!, async, disable, enable, inline, #run_callbacks

Methods included from Mongoid::Document

#embedded_children

Class Method Details

.add_improving_weeksObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/workarea/insights/trending_products.rb', line 43

def add_improving_weeks
  {
    '$addFields' => {
      'improving_weeks' => {
        '$size' => {
          '$filter' => {
            'input' => '$revenue_changes',
            'as' => 'revenue_change',
            'cond' => { '$gt' => ['$$revenue_change', 0] }
          }
        }
      }
    }
  }
end

.dashboardsObject



5
6
7
# File 'app/models/workarea/insights/trending_products.rb', line 5

def dashboards
  %w(catalog)
end

.filter_date_rangeObject



21
22
23
24
25
26
27
28
29
30
# File 'app/models/workarea/insights/trending_products.rb', line 21

def filter_date_range
  {
    '$match' => {
      'reporting_on' => {
        '$gte' => beginning_of_last_month.utc,
        '$lte' => end_of_last_month.utc
      }
    }
  }
end

.generate_monthly!Object



9
10
11
12
# File 'app/models/workarea/insights/trending_products.rb', line 9

def generate_monthly!
  results = generate_results.map { |r| r.merge(product_id: r['_id']) }
  create!(results: results) if results.present?
end

.generate_resultsObject



14
15
16
17
18
19
# File 'app/models/workarea/insights/trending_products.rb', line 14

def generate_results
  Metrics::ProductByWeek
    .collection
    .aggregate([filter_date_range, group_by_product, add_improving_weeks, sort, limit])
    .to_a
end

.group_by_productObject



32
33
34
35
36
37
38
39
40
41
# File 'app/models/workarea/insights/trending_products.rb', line 32

def group_by_product
  {
    '$group' => {
      '_id' => '$product_id',
      'improving_weeks' => { '$sum' => 1 },
      'revenue_changes' => { '$push' => '$revenue_change' },
      'orders' => { '$sum' => '$orders' }
    }
  }
end

.limitObject



63
64
65
# File 'app/models/workarea/insights/trending_products.rb', line 63

def limit
  { '$limit' => Workarea.config.insights_products_list_max_results }
end

.sortObject



59
60
61
# File 'app/models/workarea/insights/trending_products.rb', line 59

def sort
  { '$sort' => { 'improving_weeks' => -1, 'orders' => -1 } }
end