Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Towards a usable discovery algo #64

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/models/concerns/status/threading_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def descendants(limit, account = nil, depth = nil)
find_statuses_from_tree_path(descendant_ids(limit, depth), account, promote: true)
end

def descendants_count
descendant_ids('ALL', nil).length
end

def self_replies(limit)
account.statuses.distributable_visibility.where(in_reply_to_id: id).reorder(id: :asc).limit(limit)
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/trends/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def filter_for_allowed_items(items)
private

def used_key(at_time)
"#{key_prefix}:used:#{at_time.beginning_of_day.to_i}"
"#{key_prefix}:used:#{at_time.days_ago(1).to_i}"
end

def rename_set(pipeline, from_key, to_key, set_items)
Expand Down
29 changes: 22 additions & 7 deletions app/models/trends/statuses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ class Trends::Statuses < Trends::Base
self.default_options = {
threshold: 5,
review_threshold: 3,
score_halflife: 1.hour.freeze,
score_halflife: 12.hours.freeze,
decay_threshold: 0.3,
local_weight: 1.5,
favorites_value: 1,
reblogs_value: 1.5,
replies_value: 2,
interactions_exponent: 1.5,
}

class Query < Trends::Query
Expand Down Expand Up @@ -59,7 +64,7 @@ def query
Query.new(key_prefix, klass)
end

def refresh(at_time = Time.now.utc)
def refresh(at_time = Time.now.utc, since_time = Time.now.days_ago(3).utc)
# First, recalculate scores for statuses that were trending previously. We split the queries
# to avoid having to load all of the IDs into Ruby just to send them back into Postgres
Status.where(id: StatusTrend.select(:status_id)).includes(:status_stat, :account).reorder(nil).find_in_batches(batch_size: BATCH_SIZE) do |statuses|
Expand All @@ -68,7 +73,7 @@ def refresh(at_time = Time.now.utc)

# Then, calculate scores for statuses that were used today. There are potentially some
# duplicate items here that we might process one more time, but that should be fine
Status.where(id: recently_used_ids(at_time)).includes(:status_stat, :account).reorder(nil).find_in_batches(batch_size: BATCH_SIZE) do |statuses|
Status.where(id: recently_used_ids(since_time)).includes(:status_stat, :account).reorder(nil).find_in_batches(batch_size: BATCH_SIZE) do |statuses|
calculate_scores(statuses, at_time)
end

Expand Down Expand Up @@ -106,20 +111,30 @@ def klass
private

def eligible?(status)
status.public_visibility? && status.account.discoverable? && !status.account.silenced? && !status.account.sensitized? && (status.spoiler_text.blank? || Setting.trending_status_cw) && !status.sensitive? && !status.reply? && valid_locale?(status.language)
status.public_visibility? && status.account.discoverable? && !status.account.silenced? && !status.account.sensitized? && (status.spoiler_text.blank? || Setting.trending_status_cw) && !status.sensitive? && valid_locale?(status.language)
end

def calculate_scores(statuses, at_time)
items = statuses.map do |status|
expected = 1.0
observed = (status.reblogs_count + status.favourites_count).to_f
expected = 1.0
observed = if eligible?(status)
(
(status.reblogs_count * options[:reblogs_value]) +
(status.favourites_count * options[:favorites_value]) +
(status.descendants_count * options[:replies_value])
).to_f
else
0
end

score = if expected > observed || observed < options[:threshold]
0
else
((observed - expected)**2) / expected
((observed - expected)**options[:interactions_exponent]) / expected
end

score *= options[:local_weight] if status.local?

decaying_score = if score.zero? || !eligible?(status)
0
else
Expand Down
Loading