Class: Restaurants::WeightedScoreWorker

Inherits:
ApplicationWorker show all
Defined in:
app/workers/restaurants/weighted_score_worker.rb

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Instance Method Details

#perform(restaurant_id) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/workers/restaurants/weighted_score_worker.rb', line 3

def perform(restaurant_id)
  restaurant = Restaurant.find_by(id: restaurant_id)
  return if restaurant.blank?

  branch = restaurant.branch

  review_stat = if branch.present?
                  Restaurants::ReviewStat.find_by branch: branch
                else
                  Restaurants::ReviewStat.find_by restaurant: restaurant
                end

  return if review_stat.blank?

  # restaurants = Restaurant.active.not_expired

  # a = restaurants.count
  # v = count of restaurant reviews
  v = review_stat.total
  # m = average count of all restaurant reviews
  m = AdminSetting.avg_restaurants_reviews_count
  # R = restaurant review average
  r = review_stat.average
  # C = average rating of all restaurant reviews
  c = AdminSetting.avg_restaurants_reviews_score

  # (v / (v + m)) * r + (m / (v + m)) * c
  weighted_score = (v.to_f / (v.to_f + m.to_f)) * r.to_f + (m.to_f / (v.to_f + m.to_f)) * c.to_f

  restaurant.update(weighted_score: weighted_score.round(2))
  Restaurants::UpdateAttributesWorker.perform_async restaurant.id
end