FIX: Store user_accuracy_bonus to clarify explanations (#8165)

FIX: Store user_accuracy_bonus to clarify explanations (#8165)

  • FIX: Store user_accuracy_bonus to clarify explanations

  • Fixed specs from rounding change

  • migration cleanup

  • user_accuracy_bonus column not nullable

diff --git a/app/models/reviewable.rb b/app/models/reviewable.rb
index 539ae3c..750f995 100644
--- a/app/models/reviewable.rb
+++ b/app/models/reviewable.rb
@@ -159,6 +159,7 @@ class Reviewable < ActiveRecord::Base
     type_bonus = PostActionType.where(id: reviewable_score_type).pluck(:score_bonus)[0] || 0
     take_action_bonus = take_action ? 5.0 : 0.0
     sub_total = (ReviewableScore.user_flag_score(user) + type_bonus + take_action_bonus)
+    user_accuracy_bonus = ReviewableScore.user_accuracy_bonus(user)
 
     # We can force a reviewable to hit the threshold, for example with queued posts
     if force_review && sub_total < Reviewable.min_score_for_priority
@@ -170,6 +171,7 @@ class Reviewable < ActiveRecord::Base
       status: ReviewableScore.statuses[:pending],
       reviewable_score_type: reviewable_score_type,
       score: sub_total,
+      user_accuracy_bonus: user_accuracy_bonus,
       meta_topic_id: meta_topic_id,
       take_action_bonus: take_action_bonus,
       created_at: created_at || Time.zone.now
@@ -491,6 +493,7 @@ class Reviewable < ActiveRecord::Base
         us.flags_disagreed,
         us.flags_ignored,
         rs.score,
+        rs.user_accuracy_bonus,
         rs.take_action_bonus,
         COALESCE(pat.score_bonus, 0.0) AS type_bonus
       FROM reviewable_scores AS rs
diff --git a/app/models/reviewable_score.rb b/app/models/reviewable_score.rb
index e51af3c..1fe2897 100644
--- a/app/models/reviewable_score.rb
+++ b/app/models/reviewable_score.rb
@@ -84,7 +84,8 @@ class ReviewableScore < ActiveRecord::Base
     axis_distance_multiplier = 1.0 / (top - bottom)
     positivity_multiplier = positive_accuracy ? 1.0 : -1.0
 
-    absolute_distance * axis_distance_multiplier * positivity_multiplier * (Math.log(total, 4) * 5.0)
+    (absolute_distance * axis_distance_multiplier * positivity_multiplier * (Math.log(total, 4) * 5.0))
+      .round(2)
   end
 
   def reviewable_conversation
diff --git a/app/serializers/reviewable_score_explanation_serializer.rb b/app/serializers/reviewable_score_explanation_serializer.rb
index 73dccc0..f008202 100644
--- a/app/serializers/reviewable_score_explanation_serializer.rb
+++ b/app/serializers/reviewable_score_explanation_serializer.rb
@@ -12,9 +12,4 @@ class ReviewableScoreExplanationSerializer < ApplicationSerializer
     :user_accuracy_bonus,
     :score
   )
-
-  def user_accuracy_bonus
-    ReviewableScore.calc_user_accuracy_bonus(object.flags_agreed, object.flags_disagreed)
-  end
-
 end
diff --git a/db/migrate/20191007140446_add_user_accuracy_bonus_to_reviewable_scores.rb b/db/migrate/20191007140446_add_user_accuracy_bonus_to_reviewable_scores.rb
new file mode 100644
index 0000000..e9e2b7a
--- /dev/null
+++ b/db/migrate/20191007140446_add_user_accuracy_bonus_to_reviewable_scores.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+class AddUserAccuracyBonusToReviewableScores < ActiveRecord::Migration[6.0]
+  def up
+    # Add user_accuracy_bonus to reviewable_scores
+    add_column :reviewable_scores, :user_accuracy_bonus, :float, default: 0, null: false
+
+    # Set user_accuracy_bonus = score - user.trust_level - 1
+    execute <<~SQL
+    UPDATE reviewable_scores
+    SET user_accuracy_bonus = score - users.trust_level - 1
+    FROM users
+    WHERE reviewable_scores.user_id = users.id
+    SQL
+  end
+
+  def down
+    # Remove user_accuracy_bonus from reviewable_scores
+    remove_column :reviewable_scores, :user_accuracy_bonus
+  end
+end
diff --git a/spec/models/reviewable_score_spec.rb b/spec/models/reviewable_score_spec.rb
index 03c9616..47fe188 100644
--- a/spec/models/reviewable_score_spec.rb
+++ b/spec/models/reviewable_score_spec.rb
@@ -135,7 +135,7 @@ RSpec.describe ReviewableScore, type: :model do
 
       user_stat.flags_agreed = 9
       user_stat.flags_disagreed = 2
-      expect(ReviewableScore.user_accuracy_bonus(user).floor(2)).to eq(3.40)
+      expect(ReviewableScore.user_accuracy_bonus(user).floor(2)).to eq(3.41)
 
       user_stat.flags_agreed = 25
       user_stat.flags_disagreed = 4
@@ -170,7 +170,7 @@ RSpec.describe ReviewableScore, type: :model do
         user_stat.flags_agreed = 12
         user_stat.flags_disagreed = 2
         user_stat.flags_ignored = 2
-        expect(ReviewableScore.user_flag_score(user).floor(2)).to eq(7.98)
+        expect(ReviewableScore.user_flag_score(user).floor(2)).to eq(7.99)
       end
 
       it 'return 0 if the accuracy_bonus would make the score negative' do

GitHub sha: 057f698e

1 Like