2015-10-15 02:01:03 +03:00
|
|
|
class ConflictCoefficient
|
2016-10-05 13:25:05 +03:00
|
|
|
attr_reader :left, :right
|
2015-10-15 02:01:03 +03:00
|
|
|
|
|
|
|
def self.all
|
2016-10-05 13:25:05 +03:00
|
|
|
Talk.find(:all, from: :halfnarp_friendly).map(&:id).combination(2).map do |talks|
|
|
|
|
ConflictCoefficient.new talks.first, talks.last
|
|
|
|
end
|
|
|
|
end
|
2015-10-15 02:01:03 +03:00
|
|
|
|
2016-10-05 13:28:56 +03:00
|
|
|
def self.all_as_hash
|
|
|
|
conflicts_hash = {}
|
|
|
|
|
|
|
|
all.each do |coefficient|
|
|
|
|
conflicts_hash[coefficient.left] = {} unless conflicts_hash[coefficient.left]
|
|
|
|
conflicts_hash[coefficient.left][coefficient.right] = coefficient.per_cent
|
|
|
|
end
|
|
|
|
|
|
|
|
conflicts_hash
|
|
|
|
end
|
|
|
|
|
2016-10-05 13:25:05 +03:00
|
|
|
def conflicts
|
2016-10-06 23:09:53 +03:00
|
|
|
talk_preferences.joins('INNER JOIN "selected_talks" AS "left"
|
2016-10-05 15:05:25 +03:00
|
|
|
ON "left"."talk_preference_id" = "talk_preferences"."unique_id"
|
2016-10-06 23:09:53 +03:00
|
|
|
INNER JOIN "selected_talks" AS "right"
|
|
|
|
ON "right"."talk_preference_id" = "talk_preferences"."unique_id"').where(left: {talk_id: @left}, right: {talk_id: @right}).count
|
2016-10-05 13:25:05 +03:00
|
|
|
end
|
2015-10-15 02:01:03 +03:00
|
|
|
|
2016-10-05 13:25:05 +03:00
|
|
|
def total_votes
|
|
|
|
talk_preferences.count
|
2015-10-15 02:01:03 +03:00
|
|
|
end
|
|
|
|
|
2016-10-05 13:25:05 +03:00
|
|
|
def initialize(left, right)
|
|
|
|
@left, @right = left, right
|
2015-10-15 02:01:03 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def per_cent
|
2016-10-05 13:25:05 +03:00
|
|
|
Rational(100 * conflicts, total_votes).to_f
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def talk_preferences
|
|
|
|
@talk_preferences ||= TalkPreference.this_years
|
2015-10-15 02:01:03 +03:00
|
|
|
end
|
|
|
|
end
|