Skip to content

Commit

Permalink
Fix dangling language-specific trends (mastodon#17997)
Browse files Browse the repository at this point in the history
- Change score half-life for trending statuses from 2 to 6 hours
- Change score threshold for trimming old items from 1 to 0.3
  • Loading branch information
Gargron authored and kadoshita committed Apr 9, 2022
1 parent 7eed999 commit 623c510
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app/models/trends/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ def record_used_id(id, at_time = Time.now.utc)
end

def trim_older_items
redis.zremrangebyscore("#{key_prefix}:all", '-inf', '(1')
redis.zremrangebyscore("#{key_prefix}:allowed", '-inf', '(1')
redis.zremrangebyscore("#{key_prefix}:all", '-inf', '(0.3')
redis.zremrangebyscore("#{key_prefix}:allowed", '-inf', '(0.3')
end

def score_at_rank(rank)
Expand Down
5 changes: 3 additions & 2 deletions app/models/trends/links.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def add(preview_card, account_id, at_time = Time.now.utc)
def refresh(at_time = Time.now.utc)
preview_cards = PreviewCard.where(id: (recently_used_ids(at_time) + currently_trending_ids(false, -1)).uniq)
calculate_scores(preview_cards, at_time)
trim_older_items
end

def request_review
Expand Down Expand Up @@ -101,14 +100,16 @@ def calculate_scores(preview_cards, at_time)
})
end

trim_older_items

# Clean up localized sets by calculating the intersection with the main
# set. We do this instead of just deleting the localized sets to avoid
# having moments where the API returns empty results

redis.pipelined do
Trends.available_locales.each do |locale|
redis.zinterstore("#{key_prefix}:all:#{locale}", ["#{key_prefix}:all:#{locale}", "#{key_prefix}:all"], aggregate: 'max')
redis.zinterstore("#{key_prefix}:allowed:#{locale}", ["#{key_prefix}:allowed:#{locale}", "#{key_prefix}:all"], aggregate: 'max')
redis.zinterstore("#{key_prefix}:allowed:#{locale}", ["#{key_prefix}:allowed:#{locale}", "#{key_prefix}:allowed"], aggregate: 'max')
end
end
end
Expand Down
7 changes: 4 additions & 3 deletions app/models/trends/statuses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Trends::Statuses < Trends::Base
self.default_options = {
threshold: 5,
review_threshold: 3,
score_halflife: 2.hours.freeze,
score_halflife: 6.hours.freeze,
}

class Query < Trends::Query
Expand Down Expand Up @@ -48,7 +48,6 @@ def query
def refresh(at_time = Time.now.utc)
statuses = Status.where(id: (recently_used_ids(at_time) + currently_trending_ids(false, -1)).uniq).includes(:account, :media_attachments)
calculate_scores(statuses, at_time)
trim_older_items
end

def request_review
Expand Down Expand Up @@ -111,13 +110,15 @@ def calculate_scores(statuses, at_time)
})
end

trim_older_items

# Clean up localized sets by calculating the intersection with the main
# set. We do this instead of just deleting the localized sets to avoid
# having moments where the API returns empty results

Trends.available_locales.each do |locale|
redis.zinterstore("#{key_prefix}:all:#{locale}", ["#{key_prefix}:all:#{locale}", "#{key_prefix}:all"], aggregate: 'max')
redis.zinterstore("#{key_prefix}:allowed:#{locale}", ["#{key_prefix}:allowed:#{locale}", "#{key_prefix}:all"], aggregate: 'max')
redis.zinterstore("#{key_prefix}:allowed:#{locale}", ["#{key_prefix}:allowed:#{locale}", "#{key_prefix}:allowed"], aggregate: 'max')
end
end
end
Expand Down

0 comments on commit 623c510

Please sign in to comment.