Skip to content

Instantly share code, notes, and snippets.

@ArtOfCode-
Last active February 13, 2018 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArtOfCode-/993b93d0c5f975002807092678fcffed to your computer and use it in GitHub Desktop.
Save ArtOfCode-/993b93d0c5f975002807092678fcffed to your computer and use it in GitHub Desktop.
# How to validate a weighting preference
user_weights = current_user.user_site_settings.map { |uss| [uss.site, uss.group_weight] }.to_h
flags_per_site = current_user.user_site_settings.map { |uss| [uss.site, FlagLog.auto.successful.where(site: uss.site).count] }
fps_per_day = flags_per_site.map do |fps|
site_flags = FlagLog.auto.successful.where(site: fps[0])
[fps[0], fps[1].to_f / (site_flags.first.created_at.to_i - site_flags.last.created_at.to_i) / 86400)]
end
fpspd_per_user = if current_user.has_role?(:core)
user_count = User.with_role(:core).where(flags_enabled: true).count
fps_per_day.map do |fsd|
# 2/3 of the flags per day on each site go to core users
[fsd[0], (fsd[1] / (2/3.0)) / user_count]
end
else
user_count = User.without_role(:core).where(flags_enabled: true).count
fps_per_day.map do |fsd|
# 1/3 to everyone else
[fsd[0], (fsd[1] / (1/3.0)) / user_count]
end
end
weighted_fsdu = fpspd_per_user.map { |fsdu| [fsdu[0], user_weights[fsdu[0]] * fsdu[1]] }
unewighted_flags = fpspd_per_user.map { |fsdu| fsdu[1] }.reduce(:+)
weighted_flags = weighted_fsdu.map { |fsdu| fsdu[1] }.reduce(:+)
diff = weighted_flags - unweighted_flags
if diff.abs <= some_tolerance
# valid and fair to everyone
else
# invalid, user would get too many (or too few) flags
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment