Created
July 7, 2014 16:34
-
-
Save jarib/3f73b6635d42b510e0ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'csv' | |
class DisagreementVoteFinder | |
include Rails.application.routes.url_helpers | |
def initialize(session, a = ['frp', 'h'], b = ['v', 'krf']) | |
@session = session | |
@a = Party.where(slug: a).to_a | |
@b = Party.where(slug: b).to_a | |
end | |
def write(path) | |
STDERR.puts "finding disagreement votes for #{@a.map(&:name).to_sentence} vs. #{@b.map(&:name).to_sentence}" | |
disagreements = @session.votes.with_results.select { |v| disagree?(v) } | |
disagreements = disagreements.sort_by { |e| e.time } | |
votes_csv = CSV.generate do |csv| | |
csv << %w[url time parties_for parties_against subject] | |
disagreements.each do |vote| | |
csv << [ | |
vote_url(vote, host: 'www.holderdeord.no'), | |
vote.time.strftime("%F"), | |
parties_for(vote).map(&:external_id).join(';'), | |
parties_against(vote).map(&:external_id).join(';'), | |
vote.subject | |
] | |
end | |
end | |
pissues = disagreements.flat_map { |e| e.parliament_issues }.uniq | |
parliament_issues_csv = CSV.generate do |csv| | |
csv << %w[summary committee url stortinget_url] | |
pissues.each do |pissue| | |
csv << [ | |
pissue.summary, | |
pissue.committee_name, | |
parliament_issue_url(pissue, host: 'www.holderdeord.no'), | |
pissue.url | |
] | |
end | |
end | |
stats_csv = CSV.generate do |csv| | |
total_votes = @session.votes.size | |
total_issues = @session.votes.flat_map(&:parliament_issues).uniq.size | |
disagreement_votes = disagreements.size | |
disagreement_issues = pissues.size | |
csv << ['total_votes', total_votes] | |
csv << ['total_issues', total_issues] | |
csv << ['disagreement_votes', disagreement_votes] | |
csv << ['disagreement_issues', disagreement_issues] | |
csv << ['percent_disagreement_votes', disagreement_votes * 100 / total_votes.to_f] | |
csv << ['percent_disagreement_issues', disagreement_issues * 100 / total_issues.to_f] | |
end | |
File.open("#{path}.votes.csv", 'w') { |io| io << votes_csv } | |
File.open("#{path}.stats.csv", 'w') { |io| io << stats_csv } | |
File.open("#{path}.parliament-issues.csv", 'w') { |io| io << parliament_issues_csv } | |
end | |
def disagree?(vote) | |
stats = vote.stats | |
gov_keys = @a.map { |p| stats.key_for(p) } | |
return false if gov_keys.uniq.size != 1 # if the government isn't internally agreeing, we're not interested | |
gov_position = gov_keys.first | |
coop_positions = @b.map { |p| stats.key_for(p) } | |
coop_positions.all? { |p| p != gov_position } | |
end | |
def parties_for(vote) | |
vote.stats.parties.select { |e| vote.stats.party_for?(e) } | |
end | |
def parties_against(vote) | |
vote.stats.parties.select { |e| vote.stats.party_against?(e) } | |
end | |
end | |
combos = [ | |
[['h', 'frp'], ['v', 'krf']], | |
[['h', 'frp', 'v'], ['krf']], | |
[['h', 'frp', 'krf'], ['v']], | |
] | |
[ParliamentSession.current, ParliamentPeriod.previous].each do |session| | |
combos.each do |a, b| | |
DisagreementVoteFinder.new(session, a, b).write("#{session.name}.#{b.join('-')}") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment