You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.2 KiB

  1. # frozen_string_literal: true
  2. class TrendingTags
  3. KEY = 'trending_tags'
  4. EXPIRE_HISTORY_AFTER = 7.days.seconds
  5. EXPIRE_TRENDS_AFTER = 1.day.seconds
  6. THRESHOLD = 3
  7. LIMIT = 10
  8. REVIEW_THRESHOLD = 3
  9. MAX_SCORE_COOLDOWN = 2.days.freeze
  10. MAX_SCORE_HALFLIFE = 2.hours.freeze
  11. class << self
  12. include Redisable
  13. def record_use!(tag, account, status: nil, at_time: Time.now.utc)
  14. return unless tag.usable? && !account.silenced?
  15. # Even if a tag is not allowed to trend, we still need to
  16. # record the stats since they can be displayed in other places
  17. increment_historical_use!(tag.id, at_time)
  18. increment_unique_use!(tag.id, account.id, at_time)
  19. increment_use!(tag.id, at_time)
  20. # Only update when the tag was last used once every 12 hours
  21. # and only if a status is given (lets use ignore reblogs)
  22. tag.update(last_status_at: at_time) if status.present? && (tag.last_status_at.nil? || (tag.last_status_at < at_time && tag.last_status_at < 12.hours.ago))
  23. end
  24. def update!(at_time = Time.now.utc)
  25. tag_ids = redis.smembers("#{KEY}:used:#{at_time.beginning_of_day.to_i}") + redis.zrange(KEY, 0, -1)
  26. tags = Tag.trendable.where(id: tag_ids.uniq)
  27. # First pass to calculate scores and update the set
  28. tags.each do |tag|
  29. expected = redis.pfcount("activity:tags:#{tag.id}:#{(at_time - 1.day).beginning_of_day.to_i}:accounts").to_f
  30. expected = 1.0 if expected.zero?
  31. observed = redis.pfcount("activity:tags:#{tag.id}:#{at_time.beginning_of_day.to_i}:accounts").to_f
  32. max_time = tag.max_score_at
  33. max_score = tag.max_score
  34. max_score = 0 if max_time.nil? || max_time < (at_time - MAX_SCORE_COOLDOWN)
  35. score = begin
  36. if expected > observed #|| observed < THRESHOLD
  37. 0
  38. else
  39. ((observed - expected)**2) / expected
  40. end
  41. end
  42. if score > max_score
  43. max_score = score
  44. max_time = at_time
  45. # Not interested in triggering any callbacks for this
  46. tag.update_columns(max_score: max_score, max_score_at: max_time)
  47. end
  48. decaying_score = max_score * (0.5**((at_time.to_f - max_time.to_f) / MAX_SCORE_HALFLIFE.to_f))
  49. if decaying_score.zero?
  50. redis.zrem(KEY, tag.id)
  51. else
  52. redis.zadd(KEY, decaying_score, tag.id)
  53. end
  54. end
  55. users_for_review = User.staff.includes(:account).to_a.select(&:allows_trending_tag_emails?)
  56. # Second pass to notify about previously unreviewed trends
  57. tags.each do |tag|
  58. current_rank = redis.zrevrank(KEY, tag.id)
  59. needs_review_notification = tag.requires_review? && !tag.requested_review?
  60. rank_passes_threshold = current_rank.present? && current_rank <= REVIEW_THRESHOLD
  61. next unless !tag.trendable? && rank_passes_threshold && needs_review_notification
  62. tag.touch(:requested_review_at)
  63. users_for_review.each do |user|
  64. AdminMailer.new_trending_tag(user.account, tag).deliver_later!
  65. end
  66. end
  67. # Trim older items
  68. redis.zremrangebyrank(KEY, 0, -(LIMIT + 1))
  69. redis.zremrangebyscore(KEY, '(0.3', '-inf')
  70. end
  71. def get(limit, filtered: true)
  72. tag_ids = redis.zrevrange(KEY, 0, LIMIT - 1).map(&:to_i)
  73. tags = Tag.where(id: tag_ids)
  74. tags = tags.trendable if filtered
  75. tags = tags.index_by(&:id)
  76. tag_ids.map { |tag_id| tags[tag_id] }.compact.take(limit)
  77. end
  78. def trending?(tag)
  79. rank = redis.zrevrank(KEY, tag.id)
  80. rank.present? && rank < LIMIT
  81. end
  82. private
  83. def increment_historical_use!(tag_id, at_time)
  84. key = "activity:tags:#{tag_id}:#{at_time.beginning_of_day.to_i}"
  85. redis.incrby(key, 1)
  86. redis.expire(key, EXPIRE_HISTORY_AFTER)
  87. end
  88. def increment_unique_use!(tag_id, account_id, at_time)
  89. key = "activity:tags:#{tag_id}:#{at_time.beginning_of_day.to_i}:accounts"
  90. redis.pfadd(key, account_id)
  91. redis.expire(key, EXPIRE_HISTORY_AFTER)
  92. end
  93. def increment_use!(tag_id, at_time)
  94. key = "#{KEY}:used:#{at_time.beginning_of_day.to_i}"
  95. redis.sadd(key, tag_id)
  96. redis.expire(key, EXPIRE_HISTORY_AFTER)
  97. end
  98. end
  99. end