Commit 4832eb568aa692d4f66b59494b04f07ec5851e62

Authored by Dhruv Kapadia
1 parent 74081666

Improvements to density generation code

db/migrate/20100517230848_add_question_index_to_appearances_table.rb 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +class AddQuestionIndexToAppearancesTable < ActiveRecord::Migration
  2 + def self.up
  3 + add_index :appearances, :question_id
  4 + end
  5 +
  6 + def self.down
  7 + remove_index :appearances, :question_id
  8 + end
  9 +end
... ...
lib/tasks/test_api.rake
... ... @@ -46,15 +46,20 @@ namespace :test_api do
46 46  
47 47 start_date = Vote.find(:all, :conditions => 'loser_choice_id IS NOT NULL', :order => :created_at, :limit => 1).first.created_at.to_date
48 48 start_date.upto(Date.today) do |the_date|
49   - questions = Question.find(:all)
  49 + questions = Question.find(:all, :conditions => ['created_at < ?', the_date])
50 50  
51 51 print the_date.to_s
52 52 questions.each do |q|
53   - relevant_votes = q.votes.find(:all, :conditions => ['loser_choice_id IS NOT NULL AND created_at < ?', the_date])
  53 + puts q.id
54 54 relevant_choices = q.choices.find(:all, :conditions => ['created_at < ?', the_date])
55 55  
56 56 seed_choices = 0
57 57  
  58 + if relevant_choices == 0
  59 + next
  60 + #this question had not been created yet
  61 + end
  62 +
58 63 relevant_choices.each do |c|
59 64 if !c.user_created
60 65 seed_choices+=1
... ... @@ -74,9 +79,9 @@ namespace :test_api do
74 79 nonseed_seed_sum= 0
75 80 nonseed_nonseed_sum= 0
76 81  
77   - relevant_votes.each do |v|
  82 + q.appearances.find_each(:conditions => ['prompt_id IS NOT NULL AND created_at < ?', the_date]) do |a|
78 83  
79   - p = v.prompt
  84 + p = a.prompt
80 85 if p.left_choice.user_created == false && p.right_choice.user_created == false
81 86 seed_seed_sum += 1
82 87 elsif p.left_choice.user_created == false && p.right_choice.user_created == true
... ... @@ -100,7 +105,7 @@ namespace :test_api do
100 105 d.question_id = q.id
101 106 d.prompt_type = type.to_s
102 107 d.value = average.nan? ? nil : average
103   - d.save!
  108 + d.save!
104 109 end
105 110  
106 111 puts "Seed_seed sum: #{seed_seed_sum}, seed_seed total num: #{seed_seed_total}"
... ...