Commit 47c8d62ee7348358406c81b67ddf07f5ffcbd60b

Authored by Luke Baker
1 parent 237ad20f

re-arrange rake tasks

Showing 2 changed files with 77 additions and 76 deletions   Show diff stats
lib/tasks/prune_db.rake
@@ -140,4 +140,80 @@ namespace :prune_db do @@ -140,4 +140,80 @@ namespace :prune_db do
140 140
141 end 141 end
142 142
  143 + desc "Searches questions for orphaned votes (votes with no appearance) and marks them as invalid"
  144 + task :invalidate_orphaned_votes => :environment do
  145 + question_ids = ENV["question_ids"].split(/[\s,]+/)
  146 + question_ids.each do |question_id|
  147 + question = Question.find(question_id)
  148 +
  149 + orphaned_votes = Vote.find(:all,
  150 + :select => "votes.id",
  151 + :joins => "LEFT JOIN appearances ON (votes.id = appearances.answerable_id AND answerable_type <> 'Skip')",
  152 + :conditions => ["answerable_id IS NULL AND votes.valid_record = 1 AND votes.question_id = ?", question.id])
  153 + puts "Question ##{question.id} has #{orphaned_votes.count} orphaned votes"
  154 + orphaned_votes.each do |orphaned_vote_id|
  155 + orphaned_vote = Vote.find(orphaned_vote_id.id)
  156 +
  157 + # attempt to find sibling vote
  158 + # sibling vote is one that is valid has the same voter and prompt,
  159 + # is associated with an appearance, and created within 10 seconds
  160 + sibling_vote = nil
  161 + votes = Vote.find(:all, :conditions => {:voter_id => orphaned_vote.voter_id, :prompt_id => orphaned_vote.prompt_id})
  162 + votes.each do |vote|
  163 + next if vote.id == orphaned_vote.id
  164 + next if vote.created_at > orphaned_vote.created_at + 5.seconds
  165 + next if vote.created_at < orphaned_vote.created_at - 5.seconds
  166 + next if vote.appearance == nil
  167 + sibling_vote = vote
  168 + break
  169 + end
  170 + info = "Appearance XXXX already answered"
  171 + if sibling_vote
  172 + info = "Appearance #{sibling_vote.appearance.id} already answered"
  173 + end
  174 + orphaned_vote.update_attributes!(:valid_record => false, :validity_information => info)
  175 + end
  176 + end
  177 + end
  178 +
  179 + desc "Updates cached values for losses and wins for choices."
  180 + task :update_cached_losses_wins => :environment do
  181 + Question.all.each do |question|
  182 + question.choices.each do |choice|
  183 + choice.reload
  184 + true_losses = question.votes.count(:conditions => {:loser_choice_id => choice.id})
  185 + true_wins = choice.votes.count
  186 + Choice.update_counters choice.id,
  187 + :losses => (true_losses - choice.losses),
  188 + :wins => (true_wins - choice.wins)
  189 + choice.reload
  190 + choice.score = choice.compute_score
  191 + choice.save(false)
  192 + end
  193 + end
  194 + end
  195 +
  196 + desc "Update cached values for prompts on left and right for choices."
  197 + task :update_cached_prompts_on_left_right => :environment do
  198 + Question.all.each do |question|
  199 + question.choices.each do |choice|
  200 + choice.reload
  201 + choice.prompts_on_the_left
  202 + choice.prompts_on_the_right
  203 + Choice.update_counters choice.id,
  204 + :prompts_on_the_left_count => choice.prompts_on_the_left.count,
  205 + :prompts_on_the_right_count => choice.prompts_on_the_right.count
  206 + end
  207 + end
  208 + end
  209 +
  210 + desc "Recomputes scores for all choices."
  211 + task :recompute_scores => :environment do
  212 + Choice.find_each do |choice|
  213 + choice.reload
  214 + choice.score = choice.compute_score
  215 + choice.save(false)
  216 + end
  217 + end
  218 +
143 end 219 end
lib/tasks/test_api.rake
1 namespace :test_api do 1 namespace :test_api do
2 2
3 - desc "Searches questions for orphaned votes (votes with no appearance) and marks them as invalid"  
4 - task :invalidate_orphaned_votes => :environment do  
5 - question_ids = ENV["question_ids"].split(/[\s,]+/)  
6 - question_ids.each do |question_id|  
7 - question = Question.find(question_id)  
8 -  
9 - orphaned_votes = Vote.find(:all,  
10 - :select => "votes.id",  
11 - :joins => "LEFT JOIN appearances ON (votes.id = appearances.answerable_id AND answerable_type <> 'Skip')",  
12 - :conditions => ["answerable_id IS NULL AND votes.valid_record = 1 AND votes.question_id = ?", question.id])  
13 - puts "Question ##{question.id} has #{orphaned_votes.count} orphaned votes"  
14 - orphaned_votes.each do |orphaned_vote_id|  
15 - orphaned_vote = Vote.find(orphaned_vote_id.id)  
16 -  
17 - # attempt to find sibling vote  
18 - # sibling vote is one that is valid has the same voter and prompt,  
19 - # is associated with an appearance, and created within 10 seconds  
20 - sibling_vote = nil  
21 - votes = Vote.find(:all, :conditions => {:voter_id => orphaned_vote.voter_id, :prompt_id => orphaned_vote.prompt_id})  
22 - votes.each do |vote|  
23 - next if vote.id == orphaned_vote.id  
24 - next if vote.created_at > orphaned_vote.created_at + 5.seconds  
25 - next if vote.created_at < orphaned_vote.created_at - 5.seconds  
26 - next if vote.appearance == nil  
27 - sibling_vote = vote  
28 - break  
29 - end  
30 - info = "Appearance XXXX already answered"  
31 - if sibling_vote  
32 - info = "Appearance #{sibling_vote.appearance.id} already answered"  
33 - end  
34 - orphaned_vote.update_attributes!(:valid_record => false, :validity_information => info)  
35 - end  
36 - end  
37 - end  
38 -  
39 - desc "Updates cached values for losses and wins for choices."  
40 - task :update_cached_losses_wins => :environment do  
41 - Question.all.each do |question|  
42 - question.choices.each do |choice|  
43 - choice.reload  
44 - true_losses = question.votes.count(:conditions => {:loser_choice_id => choice.id})  
45 - true_wins = choice.votes.count  
46 - Choice.update_counters choice.id,  
47 - :losses => (true_losses - choice.losses),  
48 - :wins => (true_wins - choice.wins)  
49 - choice.reload  
50 - choice.score = choice.compute_score  
51 - choice.save(false)  
52 - end  
53 - end  
54 - end  
55 -  
56 - desc "Update cached values for prompts on left and right for choices."  
57 - task :update_cahced_prompts_on_left_right => :environment do  
58 - Question.all.each do |question|  
59 - question.choices.each do |choice|  
60 - choice.reload  
61 - choice.prompts_on_the_left  
62 - choice.prompts_on_the_right  
63 - Choice.update_counters choice.id,  
64 - :prompts_on_the_left_count => choice.prompts_on_the_left.count,  
65 - :prompts_on_the_right_count => choice.prompts_on_the_right.count  
66 - end  
67 - end  
68 - end  
69 -  
70 - desc "Recomputes scores for all choices."  
71 - task :recompute_scores => :environment do  
72 - Choice.find_each do |choice|  
73 - choice.reload  
74 - choice.score = choice.compute_score  
75 - choice.save(false)  
76 - end  
77 - end  
78 - 3 + desc "Run all API tests"
79 task :all => [:question_vote_consistency,:generate_density_information] 4 task :all => [:question_vote_consistency,:generate_density_information]
80 5
81 desc "Ensure that all choices have 0 <= score <= 100" 6 desc "Ensure that all choices have 0 <= score <= 100"