From fdb1c90ad102760db3ecb64c073d8954aaa217f4 Mon Sep 17 00:00:00 2001 From: Dhruv Kapadia Date: Mon, 19 Jul 2010 06:57:14 -0400 Subject: [PATCH] Tasks to move appearance tracking from votes to appearance model --- lib/tasks/prune_db.rake | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 76 insertions(+), 4 deletions(-) diff --git a/lib/tasks/prune_db.rake b/lib/tasks/prune_db.rake index 01f8e18..0d5ddba 100644 --- a/lib/tasks/prune_db.rake +++ b/lib/tasks/prune_db.rake @@ -68,14 +68,86 @@ namespace :prune_db do @appearance = Appearance.find(v.appearance_id) @appearance.answerable = v @appearance.save - + if v.id % 1000 == 0 + puts v.id + end end Skip.find_each do |s| - @appearance = Appearance.find(s.appearance_id) - @appearance.answerable = s - @appearance.save + if s.appearance_id + @appearance = Appearance.find(s.appearance_id) + @appearance.answerable = s + @appearance.save + end end end + task(:remove_double_counted_votes_with_same_appearance => :environment) do + problem_appearances = Vote.count(:group => :appearance_id, :having => "count(*) > 1") + count = 0 + choice_count = 0 + voter_count = 0 + problem_appearances.each do |id, num| + votes = Vote.find(:all, :conditions => {:appearance_id => id}, :order => 'id ASC') + choices = votes.map{|v| v.choice_id} + voters = votes.map{|v| v.voter_id} + questions = votes.map{|v| v.question_id} + + if choices.uniq.size > 1 || voters.uniq.size > 1 + count+=1 + puts "Appearance #{id}, on Question #{questions.uniq.first} has more than one inconsistent vote" + if choices.uniq.size > 1 + puts " There are #{choices.uniq.size} different choices!" + choice_count +=1 + end + if voters.uniq.size > 1 + puts " There are #{voters.uniq.size} different voters!" + voter_count +=1 + end + + if votes.size == 2 + puts " There was #{votes.second.created_at.to_f - votes.first.created_at.to_f} seconds between votes" + end + end + + votes = votes - [votes.first] # keep the first valid vote + votes.each do |v| + v.valid_record = false + v.validity_information = "Double counted vote" + v.save + end + end + + # one vote and one skip: + # + double_counted_appearances = ActiveRecord::Base.connection.select_all("select votes.appearance_id from skips inner join votes using (appearance_id) where votes.valid_record=1 AND skips.valid_record=1;") + + puts double_counted_appearances.inspect + + double_counted_appearances.each do |result| + v = result["appearance_id"] + vote = Vote.find(:first, :conditions => {:appearance_id => v}) + skip = Skip.find(:first, :conditions => {:appearance_id => v}) + + if vote.created_at < skip.created_at + object = skip + good_object = vote + else + object = vote + good_object = skip + end + + object.valid_record = false + object.validity_information = "Double counted vote" + object.save + + @appearance = Appearance.find(good_object.appearance_id) + @appearance.answerable = good_object + @appearance.save + end + + puts "Total inconsistent appearances: #{count}" + puts " #{choice_count} have inconsistent choices voted on" + puts " #{voter_count} have inconsistent voters" + end end -- libgit2 0.21.2