You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.2 KiB

  1. # frozen_string_literal: true
  2. class Scheduler::FollowRecommendationsScheduler
  3. include Sidekiq::Worker
  4. include Redisable
  5. sidekiq_options retry: 0
  6. # The maximum number of accounts that can be requested in one page from the
  7. # API is 80, and the suggestions API does not allow pagination. This number
  8. # leaves some room for accounts being filtered during live access
  9. SET_SIZE = 100
  10. def perform
  11. # Maintaining a materialized view speeds-up subsequent queries significantly
  12. AccountSummary.refresh
  13. FollowRecommendation.refresh
  14. fallback_recommendations = FollowRecommendation.order(rank: :desc).limit(SET_SIZE)
  15. Trends.available_locales.each do |locale|
  16. recommendations = if AccountSummary.safe.filtered.localized(locale).exists? # We can skip the work if no accounts with that language exist
  17. FollowRecommendation.localized(locale).order(rank: :desc).limit(SET_SIZE).map { |recommendation| [recommendation.account_id, recommendation.rank] }
  18. else
  19. []
  20. end
  21. # Use language-agnostic results if there are not enough language-specific ones
  22. missing = SET_SIZE - recommendations.size
  23. if missing.positive? && fallback_recommendations.size.positive?
  24. max_fallback_rank = fallback_recommendations.first.rank || 0
  25. # Language-specific results should be above language-agnostic ones,
  26. # otherwise language-agnostic ones will always overshadow them
  27. recommendations.map! { |(account_id, rank)| [account_id, rank + max_fallback_rank] }
  28. added = 0
  29. fallback_recommendations.each do |recommendation|
  30. next if recommendations.any? { |(account_id, _)| account_id == recommendation.account_id }
  31. recommendations << [recommendation.account_id, recommendation.rank]
  32. added += 1
  33. break if added >= missing
  34. end
  35. end
  36. redis.multi do |multi|
  37. multi.del(key(locale))
  38. recommendations.each do |(account_id, rank)|
  39. multi.zadd(key(locale), rank, account_id)
  40. end
  41. end
  42. end
  43. end
  44. private
  45. def key(locale)
  46. "follow_recommendations:#{locale}"
  47. end
  48. end