Skip to content

Commit

Permalink
Send a configurable number of flags based on accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
Undo1 committed May 22, 2018
1 parent 83df256 commit 5345682
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
10 changes: 7 additions & 3 deletions app/models/flag_condition.rb
Expand Up @@ -14,9 +14,6 @@ class FlagCondition < ApplicationRecord
def accuracy_and_post_count
return unless flags_enabled
post_feedback_results = posts.pluck(:is_tp)
true_positive_count = post_feedback_results.count(true)

accuracy = true_positive_count.to_f * 100 / post_feedback_results.count.to_f

if accuracy < FlagSetting['min_accuracy'].to_f
errors.add(:accuracy, "must be over #{number_to_percentage(FlagSetting['min_accuracy'].to_f, precision: 2)}")
Expand All @@ -26,6 +23,13 @@ def accuracy_and_post_count
errors.add(:post_count, "must be over #{FlagSetting['min_post_count']}")
end

def accuracy
post_feedback_results = posts.pluck(:is_tp)
true_positive_count = post_feedback_results.count(true)

true_positive_count.to_f * 100 / post_feedback_results.count.to_f
end

def self.revalidate_all
FlagCondition.where(flags_enabled: true).find_each do |fc|
unless fc.validate
Expand Down
26 changes: 25 additions & 1 deletion app/models/post.rb
Expand Up @@ -106,7 +106,31 @@ def autoflag
end

Rails.logger.warn "[autoflagging] #{id}: lottery begin"
max_flags = [post.site.max_flags_per_post, (FlagSetting['max_flags'] || '3').to_i].min

# Defined by the scaled_max_flags FlagSetting
# scaled_max_flags = 0,0,0,99.9,99.99,101 would always allow 3 flags,
# 4 flags on 99.9% accuracy, and 5 flags on 99.99% accuracy.
# 101% means six flags is never allowed
scaled_maxes = FlagSetting['scaled_max_flags']&.split(',')
Rails.logger.warn "[autoflagging] #{id}: scaled maxes: #{scaled_maxes}"
if !scaled_maxes.nil? && scaled_maxes.count == 6
# Check historical accuracy
fake_flag_condition = FlagCondition.new(
sites: Site.all,
max_poster_rep: post.user_reputation,
min_reason_count: 1,
min_weight: post.reasons.sum(&:weight)
)

accuracy = fake_flag_condition.accuracy # Decimal number, like 0.998

# If the accuracy is higher than all 6 thresholds (indicating 6 flags), index will be null
scaled_max = scaled_maxes.index { |n| n.to_f > accuracy * 100 } || FlagSetting['max_flags'].to_i
else
scaled_max = FlagSetting['max_flags'].to_i
end

max_flags = [post.site.max_flags_per_post, (FlagSetting['max_flags'] || '3').to_i, scaled_max].min

# Send the first flag with Smokey's account; shows up nicely in the flag queue / timeline
# At this stage, we know that at least one user has a matching flag_condition (thus 99.x% accuracy)
Expand Down

0 comments on commit 5345682

Please sign in to comment.