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.

124 lines
4.0 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 = 5
  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, at_time = Time.now.utc)
  14. return if account.silenced? || account.bot? || !tag.usable? || !(tag.trendable? || tag.requires_review?)
  15. increment_historical_use!(tag.id, at_time)
  16. increment_unique_use!(tag.id, account.id, at_time)
  17. increment_use!(tag.id, at_time)
  18. tag.update(last_status_at: Time.now.utc) if tag.last_status_at.nil? || tag.last_status_at < 12.hours.ago
  19. end
  20. def update!(at_time = Time.now.utc)
  21. tag_ids = redis.smembers("#{KEY}:used:#{at_time.beginning_of_day.to_i}") + redis.zrange(KEY, 0, -1)
  22. tags = Tag.where(id: tag_ids.uniq)
  23. # First pass to calculate scores and update the set
  24. tags.each do |tag|
  25. expected = redis.pfcount("activity:tags:#{tag.id}:#{(at_time - 1.day).beginning_of_day.to_i}:accounts").to_f
  26. expected = 1.0 if expected.zero?
  27. observed = redis.pfcount("activity:tags:#{tag.id}:#{at_time.beginning_of_day.to_i}:accounts").to_f
  28. max_time = tag.max_score_at
  29. max_score = tag.max_score
  30. max_score = 0 if max_time.nil? || max_time < (at_time - MAX_SCORE_COOLDOWN)
  31. score = begin
  32. if expected > observed || observed < THRESHOLD
  33. 0
  34. else
  35. ((observed - expected)**2) / expected
  36. end
  37. end
  38. if score > max_score
  39. max_score = score
  40. max_time = at_time
  41. # Not interested in triggering any callbacks for this
  42. tag.update_columns(max_score: max_score, max_score_at: max_time)
  43. end
  44. decaying_score = max_score * (0.5**((at_time.to_f - max_time.to_f) / MAX_SCORE_HALFLIFE.to_f))
  45. if decaying_score.zero?
  46. redis.zrem(KEY, tag.id)
  47. else
  48. redis.zadd(KEY, decaying_score, tag.id)
  49. end
  50. end
  51. users_for_review = User.staff.includes(:account).to_a.select(&:allows_trending_tag_emails?)
  52. # Second pass to notify about previously unreviewed trends
  53. tags.each do |tag|
  54. current_rank = redis.zrevrank(KEY, tag.id)
  55. needs_review_notification = tag.requires_review? && !tag.requested_review?
  56. rank_passes_threshold = current_rank.present? && current_rank <= REVIEW_THRESHOLD
  57. next unless !tag.trendable? && rank_passes_threshold && needs_review_notification
  58. tag.touch(:requested_review_at)
  59. users_for_review.each do |user|
  60. AdminMailer.new_trending_tag(user.account, tag).deliver_later!
  61. end
  62. end
  63. # Trim older items
  64. redis.zremrangebyrank(KEY, 0, -(LIMIT + 1))
  65. redis.zremrangebyscore(KEY, '(0.3', '-inf')
  66. end
  67. def get(limit, filtered: true)
  68. tag_ids = redis.zrevrange(KEY, 0, LIMIT - 1).map(&:to_i)
  69. tags = Tag.where(id: tag_ids)
  70. tags = tags.trendable if filtered
  71. tags = tags.index_by(&:id)
  72. tag_ids.map { |tag_id| tags[tag_id] }.compact.take(limit)
  73. end
  74. def trending?(tag)
  75. rank = redis.zrevrank(KEY, tag.id)
  76. rank.present? && rank < LIMIT
  77. end
  78. private
  79. def increment_historical_use!(tag_id, at_time)
  80. key = "activity:tags:#{tag_id}:#{at_time.beginning_of_day.to_i}"
  81. redis.incrby(key, 1)
  82. redis.expire(key, EXPIRE_HISTORY_AFTER)
  83. end
  84. def increment_unique_use!(tag_id, account_id, at_time)
  85. key = "activity:tags:#{tag_id}:#{at_time.beginning_of_day.to_i}:accounts"
  86. redis.pfadd(key, account_id)
  87. redis.expire(key, EXPIRE_HISTORY_AFTER)
  88. end
  89. def increment_use!(tag_id, at_time)
  90. key = "#{KEY}:used:#{at_time.beginning_of_day.to_i}"
  91. redis.sadd(key, tag_id)
  92. redis.expire(key, EXPIRE_HISTORY_AFTER)
  93. end
  94. end
  95. end