Class: Workarea::Insights::TrendingSearches

Inherits:
Base
  • Object
show all
Defined in:
app/models/workarea/insights/trending_searches.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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/workarea/insights/trending_searches.rb', line 50

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_searches.rb', line 5

def dashboards
  %w(search)
end

.filter_date_rangeObject



27
28
29
30
31
32
33
34
35
36
# File 'app/models/workarea/insights/trending_searches.rb', line 27

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
13
14
15
16
17
18
# File 'app/models/workarea/insights/trending_searches.rb', line 9

def generate_monthly!
  results = generate_results.map do |result|
    result.merge(
      query_id: result['_id'],
      query_string: result['query_string'].presence || result['_id']
    )
  end

  create!(results: results) if results.present?
end

.generate_resultsObject



20
21
22
23
24
25
# File 'app/models/workarea/insights/trending_searches.rb', line 20

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

.group_by_queryObject



38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/workarea/insights/trending_searches.rb', line 38

def group_by_query
  {
    '$group' => {
      '_id' => '$query_id',
      'query_string' => { '$first' => '$query_string' },
      'improving_weeks' => { '$sum' => 1 },
      'revenue_changes' => { '$push' => '$revenue_change' },
      'orders' => { '$sum' => '$orders' }
    }
  }
end

.limitObject



70
71
72
# File 'app/models/workarea/insights/trending_searches.rb', line 70

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

.sortObject



66
67
68
# File 'app/models/workarea/insights/trending_searches.rb', line 66

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