Commit fdb1c90ad102760db3ecb64c073d8954aaa217f4

Authored by Dhruv Kapadia
1 parent 3236092e

Tasks to move appearance tracking from votes to appearance model

Showing 1 changed file with 76 additions and 4 deletions   Show diff stats
lib/tasks/prune_db.rake
... ... @@ -68,14 +68,86 @@ namespace :prune_db do
68 68 @appearance = Appearance.find(v.appearance_id)
69 69 @appearance.answerable = v
70 70 @appearance.save
71   -
  71 + if v.id % 1000 == 0
  72 + puts v.id
  73 + end
72 74 end
73 75 Skip.find_each do |s|
74   - @appearance = Appearance.find(s.appearance_id)
75   - @appearance.answerable = s
76   - @appearance.save
  76 + if s.appearance_id
  77 + @appearance = Appearance.find(s.appearance_id)
  78 + @appearance.answerable = s
  79 + @appearance.save
  80 + end
77 81 end
78 82 end
79 83  
  84 + 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 +
  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
  111 +
  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
  118 + end
  119 +
  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;")
  123 +
  124 + puts double_counted_appearances.inspect
  125 +
  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})
  130 +
  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
  142 +
  143 + @appearance = Appearance.find(good_object.appearance_id)
  144 + @appearance.answerable = good_object
  145 + @appearance.save
  146 + end
  147 +
  148 + puts "Total inconsistent appearances: #{count}"
  149 + puts " #{choice_count} have inconsistent choices voted on"
  150 + puts " #{voter_count} have inconsistent voters"
  151 + end
80 152  
81 153 end
... ...