Class: Workarea::Insights::TopCategories

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

.dashboardsObject



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

def dashboards
  %w(catalog)
end

.find_total_revenueObject



43
44
45
46
47
# File 'app/models/workarea/insights/top_categories.rb', line 43

def find_total_revenue
  Metrics::SalesByDay
    .by_date_range(starts_at: beginning_of_last_month, ends_at: end_of_last_month)
    .sum(:revenue) || 0
end

.generate_monthly!Object



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

def generate_monthly!
  results = generate_results
  create!(results: results) if results.present?
end

.generate_resultsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/workarea/insights/top_categories.rb', line 14

def generate_results
  total_revenue = find_total_revenue

  report
    .results
    .take(Workarea.config.insights_categories_list_max_results)
    .map do |result|
      percent_of_total = if total_revenue.zero?
        0
      else
        (result['revenue'] / total_revenue.to_f) * 100
      end

      result.merge(
        category_id: result['_id'],
        percent_of_total: percent_of_total
      )
    end
end

.reportObject



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

def report
  Reports::SalesByCategory.new(
    starts_at: beginning_of_last_month,
    ends_at: end_of_last_month,
    sort_by: 'revenue',
    sort_direction: 'desc'
  )
end