Class: Workarea::Insights::HotSearches

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

Class Method Summary collapse

Methods inherited from Base

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

Methods included from ApplicationDocument

#releasable?

Methods included from Sidekiq::Callbacks

add_worker, assert_valid_config!, async, caching_classes?, disable, enable, inline, #run_callbacks, workers, workers_list

Methods included from Mongoid::Document

#embedded_children

Class Method Details

.dashboardsObject



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

def dashboards
  %w(search)
end

.find_results(median:, standard_deviation:, min_deviations:) ⇒ Object



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

def find_results(median:, standard_deviation:, min_deviations:)
  min_revenue_change = median + (standard_deviation * min_deviations)
  min_revenue_change = min_revenue_change < 0 ? 0 : min_revenue_change

  Metrics::SearchByWeek
    .last_week
    .where(:revenue_change.gt => min_revenue_change)
    .order_by(revenue_change: :desc, prior_week_revenue: :desc, _id: :asc)
    .limit(Workarea.config.insights_searches_list_max_results)
    .to_a
end

.generate_weekly!Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/workarea/insights/hot_searches.rb', line 9

def generate_weekly!
  median = revenue_change_median
  standard_deviation = revenue_change_standard_deviation

  [2, 1, 0].each do |min_deviations|
    results = find_results(
      median: median,
      standard_deviation: standard_deviation,
      min_deviations: min_deviations
    )

    if results.present?
      create!(results: results.map(&:as_document))
      return
    end
  end
end

.revenue_change_medianObject



39
40
41
# File 'app/models/workarea/insights/hot_searches.rb', line 39

def revenue_change_median
  Metrics::SearchByWeek.last_week.improved_revenue.revenue_change_median
end

.revenue_change_standard_deviationObject



43
44
45
# File 'app/models/workarea/insights/hot_searches.rb', line 43

def revenue_change_standard_deviation
  Metrics::SearchByWeek.last_week.improved_revenue.revenue_change_standard_deviation
end