about summary refs log tree commit diff
diff options
context:
space:
mode:
authorClaire <claire.github-309c@sitedethib.com>2021-11-26 00:26:06 +0100
committerClaire <claire.github-309c@sitedethib.com>2021-11-26 00:50:13 +0100
commit5f10e64330635bfd609ba5acdd78fa505c12f5b1 (patch)
treee904eeb81cc2a1cd99dc11d8fbe0ed0e9d766d59
parent0ccaedc92bff79fa2922c700dd7dc42378b99090 (diff)
Fix trends admin page crashing
-rw-r--r--app/models/trends/base.rb13
-rw-r--r--app/models/trends/links.rb25
-rw-r--r--app/models/trends/tags.rb25
-rw-r--r--spec/models/trends/tags_spec.rb9
4 files changed, 37 insertions, 35 deletions
diff --git a/app/models/trends/base.rb b/app/models/trends/base.rb
index b767dcb1a..788f128a0 100644
--- a/app/models/trends/base.rb
+++ b/app/models/trends/base.rb
@@ -3,19 +3,6 @@
 class Trends::Base
   include Redisable
 
-  class_attribute :default_options
-
-  attr_reader :options
-
-  # @param [Hash] options
-  # @option options [Integer] :threshold Minimum amount of uses by unique accounts to begin calculating the score
-  # @option options [Integer] :review_threshold Minimum rank (lower = better) before requesting a review
-  # @option options [ActiveSupport::Duration] :max_score_cooldown For this amount of time, the peak score (if bigger than current score) is decayed-from
-  # @option options [ActiveSupport::Duration] :max_score_halflife How quickly a peak score decays
-  def initialize(options = {})
-    @options = self.class.default_options.merge(options)
-  end
-
   def register(_status)
     raise NotImplementedError
   end
diff --git a/app/models/trends/links.rb b/app/models/trends/links.rb
index a0d65138b..a7cd9e29d 100644
--- a/app/models/trends/links.rb
+++ b/app/models/trends/links.rb
@@ -3,12 +3,17 @@
 class Trends::Links < Trends::Base
   PREFIX = 'trending_links'
 
-  self.default_options = {
-    threshold: 15,
-    review_threshold: 10,
-    max_score_cooldown: 2.days.freeze,
-    max_score_halflife: 8.hours.freeze,
-  }
+  # Minimum amount of uses by unique accounts to begin calculating the score
+  THRESHOLD = 15
+
+  # Minimum rank (lower = better) before requesting a review
+  REVIEW_THRESHOLD = 10
+
+  # For this amount of time, the peak score (if bigger than current score) is decayed-from
+  MAX_SCORE_COOLDOWN = 2.days.freeze
+
+  # How quickly a peak score decays
+  MAX_SCORE_HALFLIFE = 8.hours.freeze
 
   def register(status, at_time = Time.now.utc)
     original_status = status.reblog? ? status.reblog : status
@@ -76,10 +81,10 @@ class Trends::Links < Trends::Base
       observed  = preview_card.history.get(at_time).accounts.to_f
       max_time  = preview_card.max_score_at
       max_score = preview_card.max_score
-      max_score = 0 if max_time.nil? || max_time < (at_time - options[:max_score_cooldown])
+      max_score = 0 if max_time.nil? || max_time < (at_time - MAX_SCORE_COOLDOWN)
 
       score = begin
-        if expected > observed || observed < options[:threshold]
+        if expected > observed || observed < THRESHOLD
           0
         else
           ((observed - expected)**2) / expected
@@ -94,7 +99,7 @@ class Trends::Links < Trends::Base
         preview_card.update_columns(max_score: max_score, max_score_at: max_time)
       end
 
-      decaying_score = max_score * (0.5**((at_time.to_f - max_time.to_f) / options[:max_score_halflife].to_f))
+      decaying_score = max_score * (0.5**((at_time.to_f - max_time.to_f) / MAX_SCORE_HALFLIFE.to_f))
 
       if decaying_score.zero?
         redis.zrem("#{PREFIX}:all", preview_card.id)
@@ -112,6 +117,6 @@ class Trends::Links < Trends::Base
   end
 
   def would_be_trending?(id)
-    score(id) > score_at_rank(options[:review_threshold] - 1)
+    score(id) > score_at_rank(REVIEW_THRESHOLD - 1)
   end
 end
diff --git a/app/models/trends/tags.rb b/app/models/trends/tags.rb
index 13e0ab56b..027e3dbac 100644
--- a/app/models/trends/tags.rb
+++ b/app/models/trends/tags.rb
@@ -3,12 +3,17 @@
 class Trends::Tags < Trends::Base
   PREFIX = 'trending_tags'
 
-  self.default_options = {
-    threshold: 15,
-    review_threshold: 10,
-    max_score_cooldown: 2.days.freeze,
-    max_score_halflife: 4.hours.freeze,
-  }
+  # Minimum amount of uses by unique accounts to begin calculating the score
+  THRESHOLD = 15
+
+  # Minimum rank (lower = better) before requesting a review
+  REVIEW_THRESHOLD = 10
+
+  # For this amount of time, the peak score (if bigger than current score) is decayed-from
+  MAX_SCORE_COOLDOWN = 2.days.freeze
+
+  # How quickly a peak score decays
+  MAX_SCORE_HALFLIFE = 4.hours.freeze
 
   def register(status, at_time = Time.now.utc)
     original_status = status.reblog? ? status.reblog : status
@@ -70,10 +75,10 @@ class Trends::Tags < Trends::Base
       observed  = tag.history.get(at_time).accounts.to_f
       max_time  = tag.max_score_at
       max_score = tag.max_score
-      max_score = 0 if max_time.nil? || max_time < (at_time - options[:max_score_cooldown])
+      max_score = 0 if max_time.nil? || max_time < (at_time - MAX_SCORE_COOLDOWN)
 
       score = begin
-        if expected > observed || observed < options[:threshold]
+        if expected > observed || observed < THRESHOLD
           0
         else
           ((observed - expected)**2) / expected
@@ -88,7 +93,7 @@ class Trends::Tags < Trends::Base
         tag.update_columns(max_score: max_score, max_score_at: max_time)
       end
 
-      decaying_score = max_score * (0.5**((at_time.to_f - max_time.to_f) / options[:max_score_halflife].to_f))
+      decaying_score = max_score * (0.5**((at_time.to_f - max_time.to_f) / MAX_SCORE_HALFLIFE.to_f))
 
       if decaying_score.zero?
         redis.zrem("#{PREFIX}:all", tag.id)
@@ -106,6 +111,6 @@ class Trends::Tags < Trends::Base
   end
 
   def would_be_trending?(id)
-    score(id) > score_at_rank(options[:review_threshold] - 1)
+    score(id) > score_at_rank(REVIEW_THRESHOLD - 1)
   end
 end
diff --git a/spec/models/trends/tags_spec.rb b/spec/models/trends/tags_spec.rb
index 4f98c6aa4..23f8a7ba7 100644
--- a/spec/models/trends/tags_spec.rb
+++ b/spec/models/trends/tags_spec.rb
@@ -1,10 +1,15 @@
 require 'rails_helper'
 
 RSpec.describe Trends::Tags do
-  subject { described_class.new(threshold: 5, review_threshold: 10) }
+  subject { described_class.new }
 
   let!(:at_time) { DateTime.new(2021, 11, 14, 10, 15, 0) }
 
+  before do
+    stub_const 'Trends::Tags::THRESHOLD', 5
+    stub_const 'Trends::Tags::REVIEW_THRESHOLD', 10
+  end
+
   describe '#add' do
     let(:tag) { Fabricate(:tag) }
 
@@ -59,7 +64,7 @@ RSpec.describe Trends::Tags do
       subject.refresh(yesterday + 12.hours)
       original_score = subject.score(tag3.id)
       expect(original_score).to eq 144.0
-      subject.refresh(yesterday + 12.hours + subject.options[:max_score_halflife])
+      subject.refresh(yesterday + 12.hours + described_class::MAX_SCORE_HALFLIFE)
       decayed_score = subject.score(tag3.id)
       expect(decayed_score).to be <= original_score / 2
     end