about summary refs log tree commit diff
path: root/app/workers/scheduler/follow_recommendations_scheduler.rb
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2021-04-12 12:37:14 +0200
committerGitHub <noreply@github.com>2021-04-12 12:37:14 +0200
commitf7117646afddb2676e9275d8efe90c3a20c59021 (patch)
treeefb9ba8f7b22d27b0a1ea595cfa30030f5d03b09 /app/workers/scheduler/follow_recommendations_scheduler.rb
parentad61265268f13d9b2a04e2e176724d8a7376f85a (diff)
Add cold-start follow recommendations (#15945)
Diffstat (limited to 'app/workers/scheduler/follow_recommendations_scheduler.rb')
-rw-r--r--app/workers/scheduler/follow_recommendations_scheduler.rb61
1 files changed, 61 insertions, 0 deletions
diff --git a/app/workers/scheduler/follow_recommendations_scheduler.rb b/app/workers/scheduler/follow_recommendations_scheduler.rb
new file mode 100644
index 000000000..0a0286496
--- /dev/null
+++ b/app/workers/scheduler/follow_recommendations_scheduler.rb
@@ -0,0 +1,61 @@
+# 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
+
+    fallback_recommendations = FollowRecommendation.safe.filtered.limit(SET_SIZE).index_by(&:account_id)
+
+    I18n.available_locales.each do |locale|
+      recommendations = begin
+        if AccountSummary.safe.filtered.localized(locale).exists? # We can skip the work if no accounts with that language exist
+          FollowRecommendation.safe.filtered.localized(locale).limit(SET_SIZE).index_by(&:account_id)
+        else
+          {}
+        end
+      end
+
+      # Use language-agnostic results if there are not enough language-specific ones
+      missing = SET_SIZE - recommendations.keys.size
+
+      if missing.positive?
+        added = 0
+
+        # Avoid duplicate results
+        fallback_recommendations.each_value do |recommendation|
+          next if recommendations.key?(recommendation.account_id)
+
+          recommendations[recommendation.account_id] = recommendation
+          added += 1
+
+          break if added >= missing
+        end
+      end
+
+      redis.pipelined do
+        redis.del(key(locale))
+
+        recommendations.each_value do |recommendation|
+          redis.zadd(key(locale), recommendation.rank, recommendation.account_id)
+        end
+      end
+    end
+  end
+
+  private
+
+  def key(locale)
+    "follow_recommendations:#{locale}"
+  end
+end