Commit d08205453b60b45cc0adeb1ee6aa578a1fa5d5b5

Authored by Dhruv Kapadia
1 parent 1a4f43bd

Rake tasks to deal with doubly counted votes

Showing 2 changed files with 44 additions and 74 deletions   Show diff stats
lib/tasks/prune_db.rake
@@ -64,90 +64,60 @@ namespace :prune_db do @@ -64,90 +64,60 @@ namespace :prune_db do
64 end 64 end
65 65
66 task(:move_vote_and_skip_ids_to_appearance => :environment) do 66 task(:move_vote_and_skip_ids_to_appearance => :environment) do
67 - Vote.find_each do |v|  
68 - @appearance = Appearance.find(v.appearance_id)  
69 - @appearance.answerable = v  
70 - @appearance.save  
71 - if v.id % 1000 == 0  
72 - puts v.id  
73 - end  
74 - end 67 + #Vote.find_each do |v|
  68 + # @appearance = Appearance.find(v.appearance_id)
  69 +# @appearance.answerable = v
  70 +# @appearance.save
  71 +# if v.id % 1000 == 0
  72 +# puts v.id
  73 +# end
  74 +# end
75 Skip.find_each do |s| 75 Skip.find_each do |s|
76 if s.appearance_id 76 if s.appearance_id
77 @appearance = Appearance.find(s.appearance_id) 77 @appearance = Appearance.find(s.appearance_id)
78 - @appearance.answerable = s  
79 - @appearance.save 78 +
  79 + if @appearance.answerable
  80 + puts "Appearance #{@appearance.id} has more than one skip!"
  81 + else
  82 + @appearance.answerable = s
  83 + @appearance.save
  84 + end
80 end 85 end
81 end 86 end
82 end 87 end
83 88
84 task(:remove_double_counted_votes_with_same_appearance => :environment) do 89 task(:remove_double_counted_votes_with_same_appearance => :environment) do
85 - problem_appearances = Vote.count(:group => :appearance_id, :having => "count(*) > 1")  
86 - count = 0  
87 - choice_count = 0  
88 - voter_count = 0  
89 - problem_appearances.each do |id, num|  
90 - votes = Vote.find(:all, :conditions => {:appearance_id => id}, :order => 'id ASC')  
91 - choices = votes.map{|v| v.choice_id}  
92 - voters = votes.map{|v| v.voter_id}  
93 - questions = votes.map{|v| v.question_id}  
94 -  
95 - if choices.uniq.size > 1 || voters.uniq.size > 1  
96 - count+=1  
97 - puts "Appearance #{id}, on Question #{questions.uniq.first} has more than one inconsistent vote"  
98 - if choices.uniq.size > 1  
99 - puts " There are #{choices.uniq.size} different choices!"  
100 - choice_count +=1  
101 - end  
102 - if voters.uniq.size > 1  
103 - puts " There are #{voters.uniq.size} different voters!"  
104 - voter_count +=1  
105 - end  
106 90
107 - if votes.size == 2  
108 - puts " There was #{votes.second.created_at.to_f - votes.first.created_at.to_f} seconds between votes"  
109 - end  
110 - end 91 + votes_with_no_appearance = []
  92 + Vote.find_each(:include => :appearance) do |v|
  93 + puts v.id if v.id % 1000 == 0
111 94
112 - votes = votes - [votes.first] # keep the first valid vote  
113 - votes.each do |v|  
114 - v.valid_record = false  
115 - v.validity_information = "Double counted vote"  
116 - v.save  
117 - end 95 + votes_with_no_appearance << v if v.appearance.nil?
118 end 96 end
  97 +
  98 + skips_with_no_appearance = []
  99 + Skip.find_each(:include => :appearance) do |s|
  100 + puts s.id if s.id % 1000 == 0
119 101
120 - # one vote and one skip:  
121 - #  
122 - 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;") 102 + skips_with_no_appearance << s if s.appearance.nil?
  103 + end
123 104
124 - puts double_counted_appearances.inspect  
125 105
126 - double_counted_appearances.each do |result|  
127 - v = result["appearance_id"]  
128 - vote = Vote.find(:first, :conditions => {:appearance_id => v})  
129 - skip = Skip.find(:first, :conditions => {:appearance_id => v}) 106 + puts "#{votes_with_no_appearance.size} Votes"
  107 + puts "#{skips_with_no_appearance.size} Skips"
130 108
131 - if vote.created_at < skip.created_at  
132 - object = skip  
133 - good_object = vote  
134 - else  
135 - object = vote  
136 - good_object = skip  
137 - end  
138 -  
139 - object.valid_record = false  
140 - object.validity_information = "Double counted vote"  
141 - object.save 109 + votes_with_no_appearance.each do |v|
  110 + v.valid_record = false
  111 + v.validity_information = "No associated appearance object"
  112 + v.save!
  113 + end
142 114
143 - @appearance = Appearance.find(good_object.appearance_id)  
144 - @appearance.answerable = good_object  
145 - @appearance.save 115 + skips_with_no_appearance.each do |s|
  116 + s.valid_record = false
  117 + s.validity_information = "No associated appearance object"
  118 + s.save!
146 end 119 end
147 120
148 - puts "Total inconsistent appearances: #{count}"  
149 - puts " #{choice_count} have inconsistent choices voted on"  
150 - puts " #{voter_count} have inconsistent voters"  
151 end 121 end
152 122
153 #call this by doing rake prune_db:populate_seed_ideas['blahblah',questionnum], where blahblah is the filename 123 #call this by doing rake prune_db:populate_seed_ideas['blahblah',questionnum], where blahblah is the filename
lib/tasks/test_api.rake
@@ -545,16 +545,16 @@ namespace :test_api do @@ -545,16 +545,16 @@ namespace :test_api do
545 def ensure_all_votes_and_skips_have_unique_appearance 545 def ensure_all_votes_and_skips_have_unique_appearance
546 error_message = "" 546 error_message = ""
547 success_message = "All vote and skip objects have an associated appearance object" 547 success_message = "All vote and skip objects have an associated appearance object"
548 - votes_without_appearances= Vote.count(:conditions => {:appearance_id => nil})  
549 - if (votes_without_appearances > 0)  
550 - error_message += "Error! There are #{votes_without_appearances} votes without associated appearance objects."  
551 - end  
552 548
553 - skips_without_appearances= Skip.count(:conditions => {:appearance_id => nil})  
554 - if (skips_without_appearances > 0)  
555 - error_message += "Error! There are #{skips_without_appearances} skips without associated appearance objects." 549 + total_answered_appearances = Appearance.count(:conditions => 'answerable_id IS NOT NULL')
  550 + total_votes = Vote.count
  551 + total_skips = Skip.count
  552 +
  553 + if (total_answered_appearances != total_votes+ total_skips)
  554 + difference = (total_votes+ total_skips) - total_answered_appearances
  555 + error_message += "Error! There are #{difference} votes or skips without associated appearance objects."
556 end 556 end
557 - 557 +
558 return error_message.blank? ? [success_message, false] : [error_message, true] 558 return error_message.blank? ? [success_message, false] : [error_message, true]
559 end 559 end
560 560