|
|
@ -5,6 +5,7 @@ class TrendingTags |
|
|
|
EXPIRE_HISTORY_AFTER = 7.days.seconds |
|
|
|
EXPIRE_TRENDS_AFTER = 1.day.seconds |
|
|
|
THRESHOLD = 5 |
|
|
|
LIMIT = 10 |
|
|
|
|
|
|
|
class << self |
|
|
|
include Redisable |
|
|
@ -18,18 +19,18 @@ class TrendingTags |
|
|
|
end |
|
|
|
|
|
|
|
def get(limit, filtered: true) |
|
|
|
tag_ids = redis.zrevrange("#{KEY}:#{Time.now.utc.beginning_of_day.to_i}", 0, limit - 1).map(&:to_i) |
|
|
|
tag_ids = redis.zrevrange("#{KEY}:#{Time.now.utc.beginning_of_day.to_i}", 0, LIMIT - 1).map(&:to_i) |
|
|
|
|
|
|
|
tags = Tag.where(id: tag_ids) |
|
|
|
tags = tags.where(trendable: true) if filtered |
|
|
|
tags = tags.each_with_object({}) { |tag, h| h[tag.id] = tag } |
|
|
|
|
|
|
|
tag_ids.map { |tag_id| tags[tag_id] }.compact |
|
|
|
tag_ids.map { |tag_id| tags[tag_id] }.compact.take(limit) |
|
|
|
end |
|
|
|
|
|
|
|
def trending?(tag) |
|
|
|
rank = redis.zrevrank("#{KEY}:#{Time.now.utc.beginning_of_day.to_i}", tag.id) |
|
|
|
rank.present? && rank <= 10n> |
|
|
|
rank.present? && rank <= LIMIT |
|
|
|
end |
|
|
|
|
|
|
|
private |
|
|
@ -59,7 +60,7 @@ class TrendingTags |
|
|
|
old_rank = redis.zrevrank(key, tag.id) |
|
|
|
|
|
|
|
redis.zadd(key, score, tag.id) |
|
|
|
request_review!(tag) if (old_rank.nil? || old_rank > 10n>) && redis.zrevrank(key, tag.id) <= 10n> && !tag.trendable? && tag.requires_review? && !tag.requested_review? |
|
|
|
request_review!(tag) if (old_rank.nil? || old_rank > LIMIT) && redis.zrevrank(key, tag.id) <= LIMIT && !tag.trendable? && tag.requires_review? && !tag.requested_review? |
|
|
|
end |
|
|
|
|
|
|
|
redis.expire(key, EXPIRE_TRENDS_AFTER) |
|
|
|