From 47c8d62ee7348358406c81b67ddf07f5ffcbd60b Mon Sep 17 00:00:00 2001 From: Luke Baker Date: Wed, 16 Nov 2011 15:34:55 -0500 Subject: [PATCH] re-arrange rake tasks --- lib/tasks/prune_db.rake | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/tasks/test_api.rake | 77 +---------------------------------------------------------------------------- 2 files changed, 77 insertions(+), 76 deletions(-) diff --git a/lib/tasks/prune_db.rake b/lib/tasks/prune_db.rake index a1fac8a..779024a 100644 --- a/lib/tasks/prune_db.rake +++ b/lib/tasks/prune_db.rake @@ -140,4 +140,80 @@ namespace :prune_db do end + desc "Searches questions for orphaned votes (votes with no appearance) and marks them as invalid" + task :invalidate_orphaned_votes => :environment do + question_ids = ENV["question_ids"].split(/[\s,]+/) + question_ids.each do |question_id| + question = Question.find(question_id) + + orphaned_votes = Vote.find(:all, + :select => "votes.id", + :joins => "LEFT JOIN appearances ON (votes.id = appearances.answerable_id AND answerable_type <> 'Skip')", + :conditions => ["answerable_id IS NULL AND votes.valid_record = 1 AND votes.question_id = ?", question.id]) + puts "Question ##{question.id} has #{orphaned_votes.count} orphaned votes" + orphaned_votes.each do |orphaned_vote_id| + orphaned_vote = Vote.find(orphaned_vote_id.id) + + # attempt to find sibling vote + # sibling vote is one that is valid has the same voter and prompt, + # is associated with an appearance, and created within 10 seconds + sibling_vote = nil + votes = Vote.find(:all, :conditions => {:voter_id => orphaned_vote.voter_id, :prompt_id => orphaned_vote.prompt_id}) + votes.each do |vote| + next if vote.id == orphaned_vote.id + next if vote.created_at > orphaned_vote.created_at + 5.seconds + next if vote.created_at < orphaned_vote.created_at - 5.seconds + next if vote.appearance == nil + sibling_vote = vote + break + end + info = "Appearance XXXX already answered" + if sibling_vote + info = "Appearance #{sibling_vote.appearance.id} already answered" + end + orphaned_vote.update_attributes!(:valid_record => false, :validity_information => info) + end + end + end + + desc "Updates cached values for losses and wins for choices." + task :update_cached_losses_wins => :environment do + Question.all.each do |question| + question.choices.each do |choice| + choice.reload + true_losses = question.votes.count(:conditions => {:loser_choice_id => choice.id}) + true_wins = choice.votes.count + Choice.update_counters choice.id, + :losses => (true_losses - choice.losses), + :wins => (true_wins - choice.wins) + choice.reload + choice.score = choice.compute_score + choice.save(false) + end + end + end + + desc "Update cached values for prompts on left and right for choices." + task :update_cached_prompts_on_left_right => :environment do + Question.all.each do |question| + question.choices.each do |choice| + choice.reload + choice.prompts_on_the_left + choice.prompts_on_the_right + Choice.update_counters choice.id, + :prompts_on_the_left_count => choice.prompts_on_the_left.count, + :prompts_on_the_right_count => choice.prompts_on_the_right.count + end + end + end + + desc "Recomputes scores for all choices." + task :recompute_scores => :environment do + Choice.find_each do |choice| + choice.reload + choice.score = choice.compute_score + choice.save(false) + end + end + end diff --git a/lib/tasks/test_api.rake b/lib/tasks/test_api.rake index c7bd868..5f646c1 100644 --- a/lib/tasks/test_api.rake +++ b/lib/tasks/test_api.rake @@ -1,81 +1,6 @@ namespace :test_api do - desc "Searches questions for orphaned votes (votes with no appearance) and marks them as invalid" - task :invalidate_orphaned_votes => :environment do - question_ids = ENV["question_ids"].split(/[\s,]+/) - question_ids.each do |question_id| - question = Question.find(question_id) - - orphaned_votes = Vote.find(:all, - :select => "votes.id", - :joins => "LEFT JOIN appearances ON (votes.id = appearances.answerable_id AND answerable_type <> 'Skip')", - :conditions => ["answerable_id IS NULL AND votes.valid_record = 1 AND votes.question_id = ?", question.id]) - puts "Question ##{question.id} has #{orphaned_votes.count} orphaned votes" - orphaned_votes.each do |orphaned_vote_id| - orphaned_vote = Vote.find(orphaned_vote_id.id) - - # attempt to find sibling vote - # sibling vote is one that is valid has the same voter and prompt, - # is associated with an appearance, and created within 10 seconds - sibling_vote = nil - votes = Vote.find(:all, :conditions => {:voter_id => orphaned_vote.voter_id, :prompt_id => orphaned_vote.prompt_id}) - votes.each do |vote| - next if vote.id == orphaned_vote.id - next if vote.created_at > orphaned_vote.created_at + 5.seconds - next if vote.created_at < orphaned_vote.created_at - 5.seconds - next if vote.appearance == nil - sibling_vote = vote - break - end - info = "Appearance XXXX already answered" - if sibling_vote - info = "Appearance #{sibling_vote.appearance.id} already answered" - end - orphaned_vote.update_attributes!(:valid_record => false, :validity_information => info) - end - end - end - - desc "Updates cached values for losses and wins for choices." - task :update_cached_losses_wins => :environment do - Question.all.each do |question| - question.choices.each do |choice| - choice.reload - true_losses = question.votes.count(:conditions => {:loser_choice_id => choice.id}) - true_wins = choice.votes.count - Choice.update_counters choice.id, - :losses => (true_losses - choice.losses), - :wins => (true_wins - choice.wins) - choice.reload - choice.score = choice.compute_score - choice.save(false) - end - end - end - - desc "Update cached values for prompts on left and right for choices." - task :update_cahced_prompts_on_left_right => :environment do - Question.all.each do |question| - question.choices.each do |choice| - choice.reload - choice.prompts_on_the_left - choice.prompts_on_the_right - Choice.update_counters choice.id, - :prompts_on_the_left_count => choice.prompts_on_the_left.count, - :prompts_on_the_right_count => choice.prompts_on_the_right.count - end - end - end - - desc "Recomputes scores for all choices." - task :recompute_scores => :environment do - Choice.find_each do |choice| - choice.reload - choice.score = choice.compute_score - choice.save(false) - end - end - + desc "Run all API tests" task :all => [:question_vote_consistency,:generate_density_information] desc "Ensure that all choices have 0 <= score <= 100" -- libgit2 0.21.2