2021-04-12 19:37:14 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Scheduler::FollowRecommendationsScheduler
|
|
|
|
include Sidekiq::Worker
|
|
|
|
include Redisable
|
|
|
|
|
|
|
|
sidekiq_options retry: 0
|
|
|
|
|
|
|
|
# The maximum number of accounts that can be requested in one page from the
|
|
|
|
# API is 80, and the suggestions API does not allow pagination. This number
|
|
|
|
# leaves some room for accounts being filtered during live access
|
|
|
|
SET_SIZE = 100
|
|
|
|
|
|
|
|
def perform
|
|
|
|
# Maintaining a materialized view speeds-up subsequent queries significantly
|
|
|
|
AccountSummary.refresh
|
2021-05-06 05:04:52 +09:00
|
|
|
FollowRecommendation.refresh
|
2021-04-12 19:37:14 +09:00
|
|
|
|
2022-02-08 09:53:49 +09:00
|
|
|
fallback_recommendations = FollowRecommendation.order(rank: :desc).limit(SET_SIZE)
|
2021-04-12 19:37:14 +09:00
|
|
|
|
2022-02-25 08:34:14 +09:00
|
|
|
Trends.available_locales.each do |locale|
|
2023-02-19 07:09:40 +09:00
|
|
|
recommendations = if AccountSummary.safe.filtered.localized(locale).exists? # We can skip the work if no accounts with that language exist
|
2023-03-05 00:38:28 +09:00
|
|
|
FollowRecommendation.localized(locale).order(rank: :desc).limit(SET_SIZE).map { |recommendation| [recommendation.rank, recommendation.account_id] }
|
2023-02-19 07:09:40 +09:00
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2021-04-12 19:37:14 +09:00
|
|
|
|
|
|
|
# Use language-agnostic results if there are not enough language-specific ones
|
2022-02-08 09:53:49 +09:00
|
|
|
missing = SET_SIZE - recommendations.size
|
|
|
|
|
|
|
|
if missing.positive? && fallback_recommendations.size.positive?
|
|
|
|
max_fallback_rank = fallback_recommendations.first.rank || 0
|
|
|
|
|
|
|
|
# Language-specific results should be above language-agnostic ones,
|
|
|
|
# otherwise language-agnostic ones will always overshadow them
|
2023-03-05 00:38:28 +09:00
|
|
|
recommendations.map! { |(rank, account_id)| [rank + max_fallback_rank, account_id] }
|
2021-04-12 19:37:14 +09:00
|
|
|
|
|
|
|
added = 0
|
|
|
|
|
2022-02-08 09:53:49 +09:00
|
|
|
fallback_recommendations.each do |recommendation|
|
2023-03-05 00:38:28 +09:00
|
|
|
next if recommendations.any? { |(_, account_id)| account_id == recommendation.account_id }
|
2021-04-12 19:37:14 +09:00
|
|
|
|
2023-03-05 00:38:28 +09:00
|
|
|
recommendations << [recommendation.rank, recommendation.account_id]
|
2021-04-12 19:37:14 +09:00
|
|
|
added += 1
|
|
|
|
|
|
|
|
break if added >= missing
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-25 08:34:14 +09:00
|
|
|
redis.multi do |multi|
|
|
|
|
multi.del(key(locale))
|
2023-03-05 00:38:28 +09:00
|
|
|
multi.zadd(key(locale), recommendations)
|
2021-04-12 19:37:14 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def key(locale)
|
|
|
|
"follow_recommendations:#{locale}"
|
|
|
|
end
|
|
|
|
end
|