@@ -16,28 +16,33 @@ def perform
1616 AccountSummary . refresh
1717 FollowRecommendation . refresh
1818
19- fallback_recommendations = FollowRecommendation . order ( rank : :desc ) . limit ( SET_SIZE ) . index_by ( & :account_id )
19+ fallback_recommendations = FollowRecommendation . order ( rank : :desc ) . limit ( SET_SIZE )
2020
21- I18n . available_locales . each do |locale |
21+ I18n . available_locales . map { | locale | locale . to_s . split ( /[_-]/ ) . first } . uniq . each do |locale |
2222 recommendations = begin
2323 if AccountSummary . safe . filtered . localized ( locale ) . exists? # We can skip the work if no accounts with that language exist
24- FollowRecommendation . localized ( locale ) . order ( rank : :desc ) . limit ( SET_SIZE ) . index_by ( & : account_id)
24+ FollowRecommendation . localized ( locale ) . order ( rank : :desc ) . limit ( SET_SIZE ) . map { | recommendation | [ recommendation . account_id , recommendation . rank ] }
2525 else
26- { }
26+ [ ]
2727 end
2828 end
2929
3030 # Use language-agnostic results if there are not enough language-specific ones
31- missing = SET_SIZE - recommendations . keys . size
31+ missing = SET_SIZE - recommendations . size
32+
33+ if missing . positive? && fallback_recommendations . size . positive?
34+ max_fallback_rank = fallback_recommendations . first . rank || 0
35+
36+ # Language-specific results should be above language-agnostic ones,
37+ # otherwise language-agnostic ones will always overshadow them
38+ recommendations . map! { |( account_id , rank ) | [ account_id , rank + max_fallback_rank ] }
3239
33- if missing . positive?
3440 added = 0
3541
36- # Avoid duplicate results
37- fallback_recommendations . each_value do |recommendation |
38- next if recommendations . key? ( recommendation . account_id )
42+ fallback_recommendations . each do |recommendation |
43+ next if recommendations . any? { |( account_id , _ ) | account_id == recommendation . account_id }
3944
40- recommendations [ recommendation . account_id ] = recommendation
45+ recommendations << [ recommendation . account_id , recommendation . rank ]
4146 added += 1
4247
4348 break if added >= missing
@@ -47,8 +52,8 @@ def perform
4752 redis . pipelined do
4853 redis . del ( key ( locale ) )
4954
50- recommendations . each_value do |recommendation |
51- redis . zadd ( key ( locale ) , recommendation . rank , recommendation . account_id )
55+ recommendations . each do |( account_id , rank ) |
56+ redis . zadd ( key ( locale ) , rank , account_id )
5257 end
5358 end
5459 end
0 commit comments