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
This commit is contained in:
Mark VanLandingham 2019-10-08 08:49:07 -05:00 committed by Robin Ward
parent 4c9ed7bd85
commit 057f698e37
5 changed files with 28 additions and 8 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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