2018-05-28 04:45:30 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class TrendingTags
|
2018-07-17 02:21:27 +09:00
|
|
|
KEY = 'trending_tags'
|
2018-05-28 04:45:30 +09:00
|
|
|
EXPIRE_HISTORY_AFTER = 7.days.seconds
|
2018-07-17 02:21:27 +09:00
|
|
|
EXPIRE_TRENDS_AFTER = 1.day.seconds
|
|
|
|
THRESHOLD = 5
|
2019-08-08 00:08:30 +09:00
|
|
|
LIMIT = 10
|
2019-08-09 06:03:28 +09:00
|
|
|
REVIEW_THRESHOLD = 3
|
2019-09-09 02:17:57 +09:00
|
|
|
MAX_SCORE_COOLDOWN = 2.days.freeze
|
|
|
|
MAX_SCORE_HALFLIFE = 2.hours.freeze
|
2018-05-28 04:45:30 +09:00
|
|
|
|
|
|
|
class << self
|
2019-02-03 03:11:38 +09:00
|
|
|
include Redisable
|
|
|
|
|
2018-05-28 04:45:30 +09:00
|
|
|
def record_use!(tag, account, at_time = Time.now.utc)
|
2019-08-06 02:54:29 +09:00
|
|
|
return if account.silenced? || account.bot? || !tag.usable? || !(tag.trendable? || tag.requires_review?)
|
2018-05-28 04:45:30 +09:00
|
|
|
|
|
|
|
increment_historical_use!(tag.id, at_time)
|
|
|
|
increment_unique_use!(tag.id, account.id, at_time)
|
2019-09-03 01:11:13 +09:00
|
|
|
increment_use!(tag.id, at_time)
|
2019-08-18 10:45:51 +09:00
|
|
|
|
|
|
|
tag.update(last_status_at: Time.now.utc) if tag.last_status_at.nil? || tag.last_status_at < 12.hours.ago
|
2019-09-03 01:11:13 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def update!(at_time = Time.now.utc)
|
|
|
|
tag_ids = redis.smembers("#{KEY}:used:#{at_time.beginning_of_day.to_i}") + redis.zrange(KEY, 0, -1)
|
|
|
|
tags = Tag.where(id: tag_ids.uniq)
|
|
|
|
|
|
|
|
# First pass to calculate scores and update the set
|
|
|
|
|
|
|
|
tags.each do |tag|
|
|
|
|
expected = redis.pfcount("activity:tags:#{tag.id}:#{(at_time - 1.day).beginning_of_day.to_i}:accounts").to_f
|
|
|
|
expected = 1.0 if expected.zero?
|
|
|
|
observed = redis.pfcount("activity:tags:#{tag.id}:#{at_time.beginning_of_day.to_i}:accounts").to_f
|
|
|
|
max_time = tag.max_score_at
|
|
|
|
max_score = tag.max_score
|
|
|
|
max_score = 0 if max_time.nil? || max_time < (at_time - MAX_SCORE_COOLDOWN)
|
|
|
|
|
|
|
|
score = begin
|
|
|
|
if expected > observed || observed < THRESHOLD
|
|
|
|
0
|
|
|
|
else
|
|
|
|
((observed - expected)**2) / expected
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if score > max_score
|
|
|
|
max_score = score
|
|
|
|
max_time = at_time
|
|
|
|
|
|
|
|
# Not interested in triggering any callbacks for this
|
|
|
|
tag.update_columns(max_score: max_score, max_score_at: max_time)
|
|
|
|
end
|
|
|
|
|
|
|
|
decaying_score = max_score * (0.5**((at_time.to_f - max_time.to_f) / MAX_SCORE_HALFLIFE.to_f))
|
|
|
|
|
|
|
|
if decaying_score.zero?
|
|
|
|
redis.zrem(KEY, tag.id)
|
|
|
|
else
|
|
|
|
redis.zadd(KEY, decaying_score, tag.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
users_for_review = User.staff.includes(:account).to_a.select(&:allows_trending_tag_emails?)
|
|
|
|
|
|
|
|
# Second pass to notify about previously unreviewed trends
|
|
|
|
|
|
|
|
tags.each do |tag|
|
|
|
|
current_rank = redis.zrevrank(KEY, tag.id)
|
|
|
|
needs_review_notification = tag.requires_review? && !tag.requested_review?
|
|
|
|
rank_passes_threshold = current_rank.present? && current_rank <= REVIEW_THRESHOLD
|
|
|
|
|
|
|
|
next unless !tag.trendable? && rank_passes_threshold && needs_review_notification
|
|
|
|
|
|
|
|
tag.touch(:requested_review_at)
|
|
|
|
|
|
|
|
users_for_review.each do |user|
|
|
|
|
AdminMailer.new_trending_tag(user.account, tag).deliver_later!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Trim older items
|
|
|
|
|
|
|
|
redis.zremrangebyrank(KEY, 0, -(LIMIT + 1))
|
2019-09-09 02:17:57 +09:00
|
|
|
redis.zremrangebyscore(KEY, '(0.3', '-inf')
|
2018-07-17 02:21:27 +09:00
|
|
|
end
|
|
|
|
|
2019-08-06 02:54:29 +09:00
|
|
|
def get(limit, filtered: true)
|
2019-09-03 01:11:13 +09:00
|
|
|
tag_ids = redis.zrevrange(KEY, 0, LIMIT - 1).map(&:to_i)
|
2019-08-06 02:54:29 +09:00
|
|
|
|
|
|
|
tags = Tag.where(id: tag_ids)
|
2019-10-10 09:22:04 +09:00
|
|
|
tags = tags.trendable if filtered
|
2021-01-12 17:27:38 +09:00
|
|
|
tags = tags.index_by(&:id)
|
2019-08-06 02:54:29 +09:00
|
|
|
|
2019-08-08 00:08:30 +09:00
|
|
|
tag_ids.map { |tag_id| tags[tag_id] }.compact.take(limit)
|
2018-05-28 04:45:30 +09:00
|
|
|
end
|
|
|
|
|
2019-08-06 02:54:29 +09:00
|
|
|
def trending?(tag)
|
2019-09-03 01:11:13 +09:00
|
|
|
rank = redis.zrevrank(KEY, tag.id)
|
|
|
|
rank.present? && rank < LIMIT
|
2019-08-06 02:54:29 +09:00
|
|
|
end
|
|
|
|
|
2018-05-28 04:45:30 +09:00
|
|
|
private
|
|
|
|
|
|
|
|
def increment_historical_use!(tag_id, at_time)
|
|
|
|
key = "activity:tags:#{tag_id}:#{at_time.beginning_of_day.to_i}"
|
|
|
|
redis.incrby(key, 1)
|
|
|
|
redis.expire(key, EXPIRE_HISTORY_AFTER)
|
|
|
|
end
|
|
|
|
|
|
|
|
def increment_unique_use!(tag_id, account_id, at_time)
|
|
|
|
key = "activity:tags:#{tag_id}:#{at_time.beginning_of_day.to_i}:accounts"
|
|
|
|
redis.pfadd(key, account_id)
|
|
|
|
redis.expire(key, EXPIRE_HISTORY_AFTER)
|
|
|
|
end
|
|
|
|
|
2019-09-03 01:11:13 +09:00
|
|
|
def increment_use!(tag_id, at_time)
|
|
|
|
key = "#{KEY}:used:#{at_time.beginning_of_day.to_i}"
|
|
|
|
redis.sadd(key, tag_id)
|
|
|
|
redis.expire(key, EXPIRE_HISTORY_AFTER)
|
2018-05-28 04:45:30 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|