Commit b2d2b3432fa45bc20302a9f8f3925377d6338612
1 parent
6d2cf16a
Exists in
master
and in
1 other branch
add namespaces for question / choices
remove generate_densities from api test and move to prune_db
Showing
2 changed files
with
65 additions
and
59 deletions
Show diff stats
lib/tasks/prune_db.rake
... | ... | @@ -17,6 +17,38 @@ namespace :prune_db do |
17 | 17 | end |
18 | 18 | end |
19 | 19 | |
20 | + desc "Generate density information for each question - should be run nightly" | |
21 | + task(:generate_density_information => :environment) do | |
22 | + | |
23 | + # calculating densities is expensive, so only do it for questions with new data | |
24 | + question_ids = Vote.count(:conditions => ['date(created_at) = ?', Date.yesterday], :group => 'question_id').keys() | |
25 | + | |
26 | + Question.find(:all, :conditions => {:id => question_ids}).each do |q| | |
27 | + q.save_densities! | |
28 | + end | |
29 | + | |
30 | + # we can just copy the previous night's data for remaining questions | |
31 | + | |
32 | + Question.find(:all, :conditions => ['id NOT IN (?)', question_ids]).each do |q| | |
33 | + densities = q.densities.find(:all, :conditions => ['date(created_at) = ?', Date.yesterday]) | |
34 | + | |
35 | + | |
36 | + densities.each do |d| | |
37 | + new_d = d.clone | |
38 | + new_d.created_at = new_d.updated_at = Time.now | |
39 | + new_d.save! | |
40 | + end | |
41 | + | |
42 | + if densities.blank? | |
43 | + #fallback in case there wasn't a successful run yesterday | |
44 | + q.save_densities! | |
45 | + | |
46 | + end | |
47 | + | |
48 | + end | |
49 | + end | |
50 | + | |
51 | + | |
20 | 52 | desc "Generate appearances for any votes that have no current appearance, should only need to be run once" |
21 | 53 | task(:generate_appearances_for_existing_votes => :environment) do |
22 | 54 | votes = Vote.all | ... | ... |
lib/tasks/test_api.rake
1 | 1 | namespace :test_api do |
2 | 2 | |
3 | 3 | desc "Run all API tests" |
4 | - task :all => [:question_vote_consistency,:generate_density_information] | |
4 | + task :all => [:question_vote_consistency] | |
5 | 5 | |
6 | 6 | desc "Ensure all appearance and votes have matching prompt_ids" |
7 | 7 | task :verify_appearance_vote_prompt_ids => :environment do |
... | ... | @@ -37,66 +37,36 @@ namespace :test_api do |
37 | 37 | return error_message.blank? ? [success_message, false] : [error_message, true] |
38 | 38 | end |
39 | 39 | |
40 | - desc "Ensure that cached prompt counts are valid for a choice" | |
41 | - task :verify_cached_prompt_counts, [:choice_id] => :environment do |t, args| | |
42 | - choice = Choice.find(args[:choice_id]) | |
43 | - puts verify_cached_prompt_counts(choice).inspect | |
44 | - end | |
45 | - | |
46 | - def verify_cached_prompt_counts(choice) | |
47 | - success_message = "Choice has accurate prompt cache count" | |
48 | - if choice.prompts_on_the_left.count != choice.prompts_on_the_left_count || choice.prompts_on_the_right.count != choice.prompts_on_the_right_count | |
49 | - error_message = "Choice #{choice.id} in Question ##{choice.question_id} has inaccurate prompt count cache" | |
40 | + namespace :choice do | |
41 | + desc "Ensure that cached prompt counts are valid for a choice" | |
42 | + task :verify_cached_prompt_counts, [:choice_id] => :environment do |t, args| | |
43 | + choice = Choice.find(args[:choice_id]) | |
44 | + puts verify_cached_prompt_counts(choice).inspect | |
50 | 45 | end |
51 | - return error_message.blank? ? [success_message, false] : [error_message, true] | |
52 | - end | |
53 | - | |
54 | - desc "Ensure that an idea: appearances on left + appearances on right >= (wins + losses + skips)" | |
55 | - task :verify_choice_appearances_and_votes, [:choice_id] => :environment do |t, args| | |
56 | - choice = Choice.find(args[:choice_id]) | |
57 | - puts verify_choice_appearances_and_votes(choice).inspect | |
58 | - end | |
59 | 46 | |
60 | - def verify_choice_appearances_and_votes(choice) | |
61 | - success_message = "Choice has more appearances than votes and skips" | |
62 | - all_appearances = choice.appearances_on_the_left.count + choice.appearances_on_the_right.count | |
63 | - skips = choice.skips_on_the_left.count + choice.skips_on_the_right.count | |
64 | - | |
65 | - if all_appearances < choice.wins + choice.losses + skips | |
66 | - error_message = "Choice #{choice.id} in Question ##{choice.question_id} has fewer appearances than wins + losses + skips" | |
47 | + def verify_cached_prompt_counts(choice) | |
48 | + success_message = "Choice has accurate prompt cache count" | |
49 | + if choice.prompts_on_the_left.count != choice.prompts_on_the_left_count || choice.prompts_on_the_right.count != choice.prompts_on_the_right_count | |
50 | + error_message = "Choice #{choice.id} in Question ##{choice.question_id} has inaccurate prompt count cache" | |
51 | + end | |
52 | + return error_message.blank? ? [success_message, false] : [error_message, true] | |
67 | 53 | end |
68 | - return error_message.blank? ? [success_message, false] : [error_message, true] | |
69 | - end | |
70 | - | |
71 | - | |
72 | - desc "Generate density information for each question - should be run nightly" | |
73 | - task(:generate_density_information => :environment) do | |
74 | 54 | |
75 | - # calculating densities is expensive, so only do it for questions with new data | |
76 | - question_ids = Vote.count(:conditions => ['date(created_at) = ?', Date.yesterday], :group => 'question_id').keys() | |
77 | - | |
78 | - Question.find(:all, :conditions => {:id => question_ids}).each do |q| | |
79 | - q.save_densities! | |
55 | + desc "Ensure that an idea: appearances on left + appearances on right >= (wins + losses + skips)" | |
56 | + task :verify_choice_appearances_and_votes, [:choice_id] => :environment do |t, args| | |
57 | + choice = Choice.find(args[:choice_id]) | |
58 | + puts verify_choice_appearances_and_votes(choice).inspect | |
80 | 59 | end |
81 | 60 | |
82 | - # we can just copy the previous night's data for remaining questions | |
83 | - | |
84 | - Question.find(:all, :conditions => ['id NOT IN (?)', question_ids]).each do |q| | |
85 | - densities = q.densities.find(:all, :conditions => ['date(created_at) = ?', Date.yesterday]) | |
86 | - | |
87 | - | |
88 | - densities.each do |d| | |
89 | - new_d = d.clone | |
90 | - new_d.created_at = new_d.updated_at = Time.now | |
91 | - new_d.save! | |
92 | - end | |
93 | - | |
94 | - if densities.blank? | |
95 | - #fallback in case there wasn't a successful run yesterday | |
96 | - q.save_densities! | |
61 | + def verify_choice_appearances_and_votes(choice) | |
62 | + success_message = "Choice has more appearances than votes and skips" | |
63 | + all_appearances = choice.appearances_on_the_left.count + choice.appearances_on_the_right.count | |
64 | + skips = choice.skips_on_the_left.count + choice.skips_on_the_right.count | |
97 | 65 | |
66 | + if all_appearances < choice.wins + choice.losses + skips | |
67 | + error_message = "Choice #{choice.id} in Question ##{choice.question_id} has fewer appearances than wins + losses + skips" | |
98 | 68 | end |
99 | - | |
69 | + return error_message.blank? ? [success_message, false] : [error_message, true] | |
100 | 70 | end |
101 | 71 | end |
102 | 72 | |
... | ... | @@ -444,13 +414,17 @@ namespace :test_api do |
444 | 414 | return error_message.blank? ? [success_message, false] : [error_message, true] |
445 | 415 | end |
446 | 416 | |
447 | - desc "Ensure that a question has: answered_appearances == votes + skips" | |
448 | - task :answered_appearances_equals_votes_and_skips, [:question_id] => :environment do |t, args| | |
449 | - a = cleanup_args(args) | |
450 | - questions = Question.find(a[:question_id]) | |
451 | - questions.each do |question| | |
452 | - puts answered_appearances_equals_votes_and_skips(question).inspect | |
417 | + namespace :question do | |
418 | + | |
419 | + desc "Ensure that a question has: answered_appearances == votes + skips" | |
420 | + task :answered_appearances_equals_votes_and_skips, [:question_id] => :environment do |t, args| | |
421 | + a = cleanup_args(args) | |
422 | + questions = Question.find(a[:question_id]) | |
423 | + questions.each do |question| | |
424 | + puts answered_appearances_equals_votes_and_skips(question).inspect | |
425 | + end | |
453 | 426 | end |
427 | + | |
454 | 428 | end |
455 | 429 | |
456 | 430 | def answered_appearances_equals_votes_and_skips(question) | ... | ... |