From 057f698e37d2ede776cc787ea55ca69e89ad05f7 Mon Sep 17 00:00:00 2001 From: Mark VanLandingham Date: Tue, 8 Oct 2019 08:49:07 -0500 Subject: [PATCH] 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 --- app/models/reviewable.rb | 3 +++ app/models/reviewable_score.rb | 3 ++- ...reviewable_score_explanation_serializer.rb | 5 ----- ...ser_accuracy_bonus_to_reviewable_scores.rb | 21 +++++++++++++++++++ spec/models/reviewable_score_spec.rb | 4 ++-- 5 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 db/migrate/20191007140446_add_user_accuracy_bonus_to_reviewable_scores.rb diff --git a/app/models/reviewable.rb b/app/models/reviewable.rb index 539ae3c2f9d..750f99544a3 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 e51af3c0a2d..1fe28972977 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 73dccc01bf7..f0082022883 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 00000000000..e9e2b7a6f0b --- /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 03c96163323..47fe188f617 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