|
|
@ -3,12 +3,17 @@
|
|
|
|
class Trends::Links < Trends::Base
|
|
|
|
class Trends::Links < Trends::Base
|
|
|
|
PREFIX = 'trending_links'
|
|
|
|
PREFIX = 'trending_links'
|
|
|
|
|
|
|
|
|
|
|
|
self.default_options = {
|
|
|
|
# Minimum amount of uses by unique accounts to begin calculating the score
|
|
|
|
threshold: 15,
|
|
|
|
THRESHOLD = 15
|
|
|
|
review_threshold: 10,
|
|
|
|
|
|
|
|
max_score_cooldown: 2.days.freeze,
|
|
|
|
# Minimum rank (lower = better) before requesting a review
|
|
|
|
max_score_halflife: 8.hours.freeze,
|
|
|
|
REVIEW_THRESHOLD = 10
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# For this amount of time, the peak score (if bigger than current score) is decayed-from
|
|
|
|
|
|
|
|
MAX_SCORE_COOLDOWN = 2.days.freeze
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# How quickly a peak score decays
|
|
|
|
|
|
|
|
MAX_SCORE_HALFLIFE = 8.hours.freeze
|
|
|
|
|
|
|
|
|
|
|
|
def register(status, at_time = Time.now.utc)
|
|
|
|
def register(status, at_time = Time.now.utc)
|
|
|
|
original_status = status.reblog? ? status.reblog : status
|
|
|
|
original_status = status.reblog? ? status.reblog : status
|
|
|
@ -76,10 +81,10 @@ class Trends::Links < Trends::Base
|
|
|
|
observed = preview_card.history.get(at_time).accounts.to_f
|
|
|
|
observed = preview_card.history.get(at_time).accounts.to_f
|
|
|
|
max_time = preview_card.max_score_at
|
|
|
|
max_time = preview_card.max_score_at
|
|
|
|
max_score = preview_card.max_score
|
|
|
|
max_score = preview_card.max_score
|
|
|
|
max_score = 0 if max_time.nil? || max_time < (at_time - options[:max_score_cooldown])
|
|
|
|
max_score = 0 if max_time.nil? || max_time < (at_time - MAX_SCORE_COOLDOWN)
|
|
|
|
|
|
|
|
|
|
|
|
score = begin
|
|
|
|
score = begin
|
|
|
|
if expected > observed || observed < options[:threshold]
|
|
|
|
if expected > observed || observed < THRESHOLD
|
|
|
|
0
|
|
|
|
0
|
|
|
|
else
|
|
|
|
else
|
|
|
|
((observed - expected)**2) / expected
|
|
|
|
((observed - expected)**2) / expected
|
|
|
@ -94,7 +99,7 @@ class Trends::Links < Trends::Base
|
|
|
|
preview_card.update_columns(max_score: max_score, max_score_at: max_time)
|
|
|
|
preview_card.update_columns(max_score: max_score, max_score_at: max_time)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
decaying_score = max_score * (0.5**((at_time.to_f - max_time.to_f) / options[:max_score_halflife].to_f))
|
|
|
|
decaying_score = max_score * (0.5**((at_time.to_f - max_time.to_f) / MAX_SCORE_HALFLIFE.to_f))
|
|
|
|
|
|
|
|
|
|
|
|
if decaying_score.zero?
|
|
|
|
if decaying_score.zero?
|
|
|
|
redis.zrem("#{PREFIX}:all", preview_card.id)
|
|
|
|
redis.zrem("#{PREFIX}:all", preview_card.id)
|
|
|
@ -112,6 +117,6 @@ class Trends::Links < Trends::Base
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def would_be_trending?(id)
|
|
|
|
def would_be_trending?(id)
|
|
|
|
score(id) > score_at_rank(options[:review_threshold] - 1)
|
|
|
|
score(id) > score_at_rank(REVIEW_THRESHOLD - 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|