Class: Restaurants::ReviewStatGenerator

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

Overview

ReviewStatGenerator is responsible for updating and recalculating review statistics for restaurants and branches. It handles review creation, updates, migration, and deletion, and ensures that aggregate statistics (such as star counts and averages) are kept accurate at both the restaurant/branch and global (AdminSetting) levels.

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Instance Method Details

#calculate_stars_count(review, review_stat) ⇒ Hash

Calculates the new star counts for a review_stat after a review is destroyed.

Parameters:

  • review (Review)

    The review being destroyed

  • review_stat (ReviewStat)

    The associated review statistics record

Returns:

  • (Hash)

    Updated star counts



125
126
127
128
129
# File 'app/workers/restaurants/review_stat_generator.rb', line 125

def calculate_stars_count(review, review_stat)
  old_star = rating_to_key(review.rating)

  calculate_star_counts(review_stat, nil, old_star)
end

#migrate_data(id, type = 'Restaurant') ⇒ Object

Generate restaurant's review stat



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/workers/restaurants/review_stat_generator.rb', line 83

def migrate_data(id, type = 'Restaurant')
  case type
  when 'Restaurant'
    restaurant = Restaurant.fetch id
    create_or_update_review_stat(restaurant: restaurant)
    if restaurant.branch_id.present?
      create_or_update_review_stat(branch: restaurant.branch)
    end
  when 'Branch'
    branch = Branch.fetch id
    create_or_update_review_stat(branch: branch)
  else
    raise NotImplementedError
  end
end

#perform(id, changed_attr = {}, action = 'update', type = 'Restaurant') ⇒ Object

Performs the requested action on review statistics.

Parameters:

  • id (Integer)

    Review ID (for update/destroy) or type's ID (for migrate)

  • changed_attr (Hash) (defaults to: {})

    Changed attributes when updating a review

  • action (String) (defaults to: 'update')

    One of “update”, “migrate”, or “destroy”

  • type (String) (defaults to: 'Restaurant')

    “Restaurant” or “Branch”



13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/workers/restaurants/review_stat_generator.rb', line 13

def perform(id, changed_attr = {}, action = 'update', type = 'Restaurant')
  case action
  when 'update'
    update(id, type, changed_attr)
  when 'migrate'
    migrate_data(id, type)
  when 'destroy'
    update_database_on_review_destroy(id, type)
  end

  clear_restaurant_cache(id, type)
end

#recalculate_admin_settings(old_rating) ⇒ Object

Recalculates global review aggregates in AdminSetting after a review is destroyed.

Parameters:

  • old_rating (Symbol)

    The star rating of the destroyed review



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/workers/restaurants/review_stat_generator.rb', line 134

def recalculate_admin_settings(old_rating)
  total_restaurants = Restaurant.active.not_expired.count
  total_reviews = AdminSetting.total_reviews - 1
  all_stars = AdminSetting.all_stars

  %i[one_star two_star three_star four_star five_star].each do |count|
    current_count = all_stars[count].presence || 0
    reduce = old_rating == count ? 1 : 0
    all_stars[count] = current_count - reduce
  end

  AdminSetting.all_stars = all_stars
  AdminSetting.total_reviews = total_reviews
  AdminSetting.avg_restaurants_reviews_count = total_reviews / total_restaurants
  AdminSetting.avg_restaurants_reviews_score = average(all_stars)
end

#update(id, type, changed_attr) ⇒ Object

Updates review statistics for a restaurant or branch when a review is created or updated.

Handles incrementing/decrementing star counts and total reviews, and updates AdminSetting aggregates accordingly.

Parameters:

  • id (Integer)

    Review ID

  • type (String)

    “Restaurant” or “Branch”

  • changed_attr (Hash)

    Changed attributes for the review



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/workers/restaurants/review_stat_generator.rb', line 34

def update(id, type, changed_attr)
  review = Review.find_by(id: id)
  return if review.blank?

  restaurant = review.reservation.restaurant
  branch = restaurant.branch

  review_stat = find_review_stat(restaurant, branch, type)

  if review_stat.blank?
    migrate_data(restaurant.id, type)
    return
  end
  first_state = changed_attr['state'] == 1.0
  new_star = rating_to_key(review.rating)
  old_rating = changed_attr['rating']
  old_star = valid_rating?(old_rating) ? rating_to_key(old_rating) : nil

  stars_count = calculate_star_counts(review_stat, new_star, old_star)

  identifier_data = identifiers(restaurant)
  new_review = first_state ? 1 : 0

  update_review_attributes(review_stat, stars_count, identifier_data, new_review)

  # recalculating all restaurants review data
  total_restaurants = Restaurant.active.not_expired.count
  all_stars = AdminSetting.all_stars

  if first_state
    # Only increment total_reviews if this is a new review
    total_reviews = AdminSetting.total_reviews + 1
    adjust_all_stars!(all_stars, new_star, nil)
  elsif old_star && old_star != new_star
    # If rating changed, adjust star counts accordingly, but do not change total_reviews
    adjust_all_stars!(all_stars, new_star, old_star)
    total_reviews = AdminSetting.total_reviews
  else
    # No change to total_reviews or all_stars
    total_reviews = AdminSetting.total_reviews
  end

  AdminSetting.all_stars = all_stars
  AdminSetting.total_reviews = total_reviews
  AdminSetting.avg_restaurants_reviews_count = total_reviews / total_restaurants
  AdminSetting.avg_restaurants_reviews_score = average(all_stars)
end

#update_database_on_review_destroy(id, type) ⇒ Object

Updates statistics and aggregates when a review is destroyed.

Parameters:

  • id (Integer)

    Review ID

  • type (String)

    “Restaurant” or “Branch”



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/workers/restaurants/review_stat_generator.rb', line 103

def update_database_on_review_destroy(id, type)
  review = Review.find_by(id: id)
  return if review.blank?

  restaurant = review.reservation.restaurant
  branch = restaurant.branch
  review_stat = find_review_stat(restaurant, branch, type)
  return if review_stat.blank?

  stars_count = calculate_stars_count(review, review_stat)
  identifier_data = identifiers(restaurant)
  update_review_attributes(review_stat, stars_count, identifier_data, -1)

  recalculate_admin_settings(review.rating)
  review.destroy
end