From d5b52abf2f6433d375fe36214496be011cd3294c Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Thu, 19 Sep 2019 13:42:50 -0400 Subject: [PATCH] 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. --- app/jobs/scheduled/reviewable_priorities.rb | 5 +++ spec/jobs/reviewable_priorities_spec.rb | 35 ++++++++++++++------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/app/jobs/scheduled/reviewable_priorities.rb b/app/jobs/scheduled/reviewable_priorities.rb index 49f4aa2cd84..e4a57bb4729 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 2f53a6b9479..076fba42631 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) - end - - it "will return 0 if no reviewables exist" 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(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 "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