about summary refs log tree commit diff
path: root/app/models/trending_tags.rb
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2018-07-16 19:21:27 +0200
committerGitHub <noreply@github.com>2018-07-16 19:21:27 +0200
commit0d13e30ad2579a9fc55f1b57c80cf9a9a89b1491 (patch)
tree9635d4f3d0c7af3b28e71476d6f61e5f248e8b32 /app/models/trending_tags.rb
parent7e2678f7f66fbdb215b89378e2a1b7d9e3901fc0 (diff)
Display trending hashtags on admin dashboard (#8038)
Diffstat (limited to 'app/models/trending_tags.rb')
-rw-r--r--app/models/trending_tags.rb27
1 files changed, 27 insertions, 0 deletions
diff --git a/app/models/trending_tags.rb b/app/models/trending_tags.rb
index c3641d7fd..b11f35292 100644
--- a/app/models/trending_tags.rb
+++ b/app/models/trending_tags.rb
@@ -1,7 +1,10 @@
 # frozen_string_literal: true
 
 class TrendingTags
+  KEY                  = 'trending_tags'
   EXPIRE_HISTORY_AFTER = 7.days.seconds
+  EXPIRE_TRENDS_AFTER  = 1.day.seconds
+  THRESHOLD            = 5
 
   class << self
     def record_use!(tag, account, at_time = Time.now.utc)
@@ -9,6 +12,14 @@ class TrendingTags
 
       increment_historical_use!(tag.id, at_time)
       increment_unique_use!(tag.id, account.id, at_time)
+      increment_vote!(tag.id, at_time)
+    end
+
+    def get(limit)
+      key     = "#{KEY}:#{Time.now.utc.beginning_of_day.to_i}"
+      tag_ids = redis.zrevrange(key, 0, limit).map(&:to_i)
+      tags    = Tag.where(id: tag_ids).to_a.map { |tag| [tag.id, tag] }.to_h
+      tag_ids.map { |tag_id| tags[tag_id] }.compact
     end
 
     private
@@ -25,6 +36,22 @@ class TrendingTags
       redis.expire(key, EXPIRE_HISTORY_AFTER)
     end
 
+    def increment_vote!(tag_id, at_time)
+      key      = "#{KEY}:#{at_time.beginning_of_day.to_i}"
+      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
+
+      if expected > observed || observed < THRESHOLD
+        redis.zrem(key, tag_id.to_s)
+      else
+        score = ((observed - expected)**2) / expected
+        redis.zadd(key, score, tag_id.to_s)
+      end
+
+      redis.expire(key, EXPIRE_TRENDS_AFTER)
+    end
+
     def disallowed_hashtags
       return @disallowed_hashtags if defined?(@disallowed_hashtags)