The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

73 lines
2.5 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. class << self
  8. include Redisable
  9. def record_use!(tag, account, at_time = Time.now.utc)
  10. return if account.silenced? || account.bot? || !tag.usable? || !(tag.trendable? || tag.requires_review?)
  11. increment_historical_use!(tag.id, at_time)
  12. increment_unique_use!(tag.id, account.id, at_time)
  13. increment_vote!(tag, at_time)
  14. end
  15. def get(limit, filtered: true)
  16. tag_ids = redis.zrevrange("#{KEY}:#{Time.now.utc.beginning_of_day.to_i}", 0, limit - 1).map(&:to_i)
  17. tags = Tag.where(id: tag_ids)
  18. tags = tags.where(trendable: true) if filtered
  19. tags = tags.each_with_object({}) { |tag, h| h[tag.id] = tag }
  20. tag_ids.map { |tag_id| tags[tag_id] }.compact
  21. end
  22. def trending?(tag)
  23. rank = redis.zrevrank("#{KEY}:#{Time.now.utc.beginning_of_day.to_i}", tag.id)
  24. rank.present? && rank <= 10
  25. end
  26. private
  27. def increment_historical_use!(tag_id, at_time)
  28. key = "activity:tags:#{tag_id}:#{at_time.beginning_of_day.to_i}"
  29. redis.incrby(key, 1)
  30. redis.expire(key, EXPIRE_HISTORY_AFTER)
  31. end
  32. def increment_unique_use!(tag_id, account_id, at_time)
  33. key = "activity:tags:#{tag_id}:#{at_time.beginning_of_day.to_i}:accounts"
  34. redis.pfadd(key, account_id)
  35. redis.expire(key, EXPIRE_HISTORY_AFTER)
  36. end
  37. def increment_vote!(tag, at_time)
  38. key = "#{KEY}:#{at_time.beginning_of_day.to_i}"
  39. expected = redis.pfcount("activity:tags:#{tag.id}:#{(at_time - 1.day).beginning_of_day.to_i}:accounts").to_f
  40. expected = 1.0 if expected.zero?
  41. observed = redis.pfcount("activity:tags:#{tag.id}:#{at_time.beginning_of_day.to_i}:accounts").to_f
  42. if expected > observed || observed < THRESHOLD
  43. redis.zrem(key, tag.id)
  44. else
  45. score = ((observed - expected)**2) / expected
  46. old_rank = redis.zrevrank(key, tag.id)
  47. redis.zadd(key, score, tag.id)
  48. request_review!(tag) if (old_rank.nil? || old_rank > 10) && redis.zrevrank(key, tag.id) <= 10 && !tag.trendable? && tag.requires_review? && !tag.requested_review?
  49. end
  50. redis.expire(key, EXPIRE_TRENDS_AFTER)
  51. end
  52. def request_review!(tag)
  53. User.staff.includes(:account).find_each { |u| AdminMailer.new_trending_tag(u.account, tag).deliver_later! if u.allows_trending_tag_emails? }
  54. end
  55. end
  56. end