Class: Workarea::Insights::CustomersAtRisk

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

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

def dashboards
  %w(people)
end

.find_resultsObject



14
15
16
17
18
19
20
21
22
# File 'app/models/workarea/insights/customers_at_risk.rb', line 14

def find_results
  Metrics::User
    .with_purchases
    .by_revenue_percentile(81..100)
    .where(:last_order_at.gte => 1.year.ago, :last_order_at.lt => 30.days.ago)
    .order_by(revenue: :desc, last_order_at: :asc, id: :asc)
    .limit(Workarea.config.insights_users_list_max_results)
    .to_a
end

.generate_monthly!Object



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

def generate_monthly!
  results = find_results
  create!(results: results.map(&:as_document)) if results.present?
end