Class: Workarea::Insights::TopDiscounts
- Inherits:
-
Base
- Object
- Base
- Workarea::Insights::TopDiscounts
show all
- Defined in:
- app/models/workarea/insights/top_discounts.rb
Class Method Summary
collapse
Methods inherited from Base
beginning_of_last_month, current, end_of_last_month, generate_daily!, generate_weekly!, #include?, #slug
#releasable?
add_worker, assert_valid_config!, async, caching_classes?, disable, enable, inline, #run_callbacks, workers, workers_list
#embedded_children
Class Method Details
.dashboards ⇒ Object
5
6
7
|
# File 'app/models/workarea/insights/top_discounts.rb', line 5
def dashboards
%w(marketing)
end
|
.find_total_orders ⇒ Object
43
44
45
46
47
|
# File 'app/models/workarea/insights/top_discounts.rb', line 43
def find_total_orders
Metrics::SalesByDay
.by_date_range(starts_at: beginning_of_last_month, ends_at: end_of_last_month)
.sum(:orders) || 0
end
|
.generate_monthly! ⇒ Object
9
10
11
12
|
# File 'app/models/workarea/insights/top_discounts.rb', line 9
def generate_monthly!
results = generate_results
create!(results: results) if results.present?
end
|
.generate_results ⇒ Object
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_discounts.rb', line 14
def generate_results
total_orders = find_total_orders
report
.results
.take(Workarea.config.insights_discounts_list_max_results)
.map do |result|
percent_of_total = if total_orders.zero?
0
else
(result['orders'] / total_orders.to_f) * 100
end
result.merge(
discount_id: result['_id'],
percent_of_total: percent_of_total
)
end
end
|
.report ⇒ Object
34
35
36
37
38
39
40
41
|
# File 'app/models/workarea/insights/top_discounts.rb', line 34
def report
Reports::SalesByDiscount.new(
starts_at: beginning_of_last_month,
ends_at: end_of_last_month,
sort_by: 'revenue',
sort_direction: 'desc'
)
end
|