FIX: Require a min amount of reviewables before calculating thresholds

FIX: Require a min amount of reviewables before calculating thresholds

On forums with very few flags you don’t want to calculate averages because they won’t be very useful. Stick with the defaults until we hit 15 reviewables at least.

diff --git a/app/jobs/scheduled/reviewable_priorities.rb b/app/jobs/scheduled/reviewable_priorities.rb
index 49f4aa2..e4a57bb 100644
--- a/app/jobs/scheduled/reviewable_priorities.rb
+++ b/app/jobs/scheduled/reviewable_priorities.rb
@@ -3,7 +3,12 @@
 class Jobs::ReviewablePriorities < Jobs::Scheduled
   every 1.day
 
+  def self.min_reviewables
+    15
+  end
+
   def execute(args)
+    return unless Reviewable.where('score > 0').count >= self.class.min_reviewables
 
     # We calculate the percentiles here for medium and high. Low is always 0 (all)
     res = DB.query_single(<<~SQL)
diff --git a/spec/jobs/reviewable_priorities_spec.rb b/spec/jobs/reviewable_priorities_spec.rb
index 2f53a6b..076fba4 100644
--- a/spec/jobs/reviewable_priorities_spec.rb
+++ b/spec/jobs/reviewable_priorities_spec.rb
@@ -4,21 +4,32 @@ require 'rails_helper'
 
 describe Jobs::ReviewablePriorities do
 
-  it "will set priorities based on the maximum score" do
-    (1..6).each { |i| Fabricate(:reviewable, score: i) }
+  it "needs returns 0s with no existing reviewables" do
     Jobs::ReviewablePriorities.new.execute({})
-
     expect(Reviewable.min_score_for_priority(:low)).to eq(0.0)
-    expect(Reviewable.min_score_for_priority(:medium)).to eq(3.0)
-    expect(Reviewable.min_score_for_priority('medium')).to eq(3.0)
-    expect(Reviewable.min_score_for_priority(:high)).to eq(6.0)
+    expect(Reviewable.min_score_for_priority(:medium)).to eq(0.0)
+    expect(Reviewable.min_score_for_priority(:high)).to eq(0.0)
+    expect(Reviewable.score_required_to_hide_post).to eq(8.33)
   end
 
-  it "will return 0 if no reviewables exist" do
+  it "needs a minimum amount of reviewables before it calculates anything" do
+    (1..5).each { |i| Fabricate(:reviewable, score: i) }
     Jobs::ReviewablePriorities.new.execute({})
-
     expect(Reviewable.min_score_for_priority(:low)).to eq(0.0)
     expect(Reviewable.min_score_for_priority(:medium)).to eq(0.0)
     expect(Reviewable.min_score_for_priority(:high)).to eq(0.0)
+    expect(Reviewable.score_required_to_hide_post).to eq(8.33)
+  end
+
+  it "will set priorities based on the maximum score" do
+    (1..Jobs::ReviewablePriorities.min_reviewables).each { |i| Fabricate(:reviewable, score: i) }
+    Jobs::ReviewablePriorities.new.execute({})
+
+    expect(Reviewable.min_score_for_priority(:low)).to eq(0.0)
+    expect(Reviewable.min_score_for_priority(:medium)).to eq(8.0)
+    expect(Reviewable.min_score_for_priority('medium')).to eq(8.0)
+    expect(Reviewable.min_score_for_priority(:high)).to eq(13.0)
+    expect(Reviewable.score_required_to_hide_post).to eq(8.66)
   end
+
 end

GitHub sha: d5b52abf

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there: