Commit 6d2cf16ad6630bf7145c0e7a2c9d6b78f0d66e67

Authored by Luke Baker
1 parent 55041d0f

move bunch of items from test_api to prune_db

Showing 2 changed files with 233 additions and 219 deletions   Show diff stats
lib/tasks/prune_db.rake
... ... @@ -17,6 +17,223 @@ namespace :prune_db do
17 17 end
18 18 end
19 19  
  20 + desc "Generate appearances for any votes that have no current appearance, should only need to be run once"
  21 + task(:generate_appearances_for_existing_votes => :environment) do
  22 + votes = Vote.all
  23 +
  24 + count = 0
  25 + votes.each do |v|
  26 + if v.appearance.nil?
  27 + print "."
  28 + a = Appearance.create(:voter_id => v.voter_id, :site_id => v.site_id, :prompt_id => v.prompt_id, :question_id => v.question_id, :created_at => v.created_at, :updated_at => v.updated_at)
  29 + v.appearance = a
  30 + v.save
  31 +
  32 + count += 1
  33 + end
  34 + end
  35 +
  36 + print count
  37 + end
  38 +
  39 + desc "Don't run unless you know what you are doing"
  40 + task(:generate_lots_of_votes => :environment) do
  41 + if Rails.env.production?
  42 + print "You probably don't want to run this in production as it will falsify a bunch of random votes"
  43 + end
  44 +
  45 +
  46 + current_user = User.first
  47 + 1000.times do |n|
  48 + puts "#{n} votes completed" if n % 100 == 0
  49 + question = Question.find(214) # test question change as needed
  50 + @prompt = question.catchup_choose_prompt(1).first
  51 + @appearance = current_user.record_appearance(current_user.default_visitor, @prompt)
  52 +
  53 + direction = (rand(2) == 0) ? "left" : "right"
  54 + current_user.record_vote(:prompt => @prompt, :direction => direction, :appearance_lookup => @appearance.lookup)
  55 + end
  56 +
  57 + end
  58 +
  59 + desc "Dump votes of a question by left vs right id"
  60 + task(:make_csv => :environment) do
  61 +
  62 + q = Question.find(214)
  63 +
  64 +
  65 + the_prompts = q.prompts_hash_by_choice_ids
  66 +
  67 + #hash_of_choice_ids_from_left_to_right_to_votes
  68 + the_hash = {}
  69 + q.choices.each do |l|
  70 + q.choices.each do |r|
  71 + next if l.id == r.id
  72 +
  73 + if not the_hash.has_key?(l.id)
  74 + the_hash[l.id] = {}
  75 + the_hash[l.id][l.id] = 0
  76 + end
  77 +
  78 + p = the_prompts["#{l.id}, #{r.id}"]
  79 + if p.nil?
  80 + the_hash[l.id][r.id] = 0
  81 + else
  82 + the_hash[l.id][r.id] = p.appearances.size
  83 + end
  84 + end
  85 + end
  86 +
  87 + the_hash.sort.each do |xval, row|
  88 + rowarray = []
  89 + row.sort.each do |yval, cell|
  90 + rowarray << cell
  91 + end
  92 + puts rowarray.join(", ")
  93 + end
  94 + end
  95 +
  96 + desc "Should only need to be run once"
  97 + task(:generate_all_possible_prompts => :environment) do
  98 + Question.find(:all).each do |q|
  99 + choices = q.choices
  100 + if q.prompts.size > choices.size**2 - choices.size
  101 + print "ERROR: #{q.id}\n"
  102 + next
  103 + elsif q.prompts.size == choices.size**2 - choices.size
  104 + print "#{q.id} has enough prompts, skipping...\n"
  105 + next
  106 + else
  107 + print "#{q.id} should add #{(choices.size ** 2 - choices.size) - q.prompts.size}\n"
  108 +
  109 + end
  110 + created_timestring = q.created_at.to_s(:db)
  111 + updated_timestring = Time.now.to_s(:db) #isn't rails awesome?
  112 + promptscount=0
  113 + inserts = []
  114 + the_prompts = Prompt.find(:all, :select => 'id, left_choice_id, right_choice_id', :conditions => {:question_id => q.id})
  115 +
  116 + the_prompts_hash = {}
  117 + the_prompts.each do |p|
  118 + the_prompts_hash["#{p.left_choice_id},#{p.right_choice_id}"] = 1
  119 + end
  120 +
  121 + choices.each do |l|
  122 + choices.each do |r|
  123 + if l.id == r.id
  124 + next
  125 + else
  126 + #p = the_prompts.find{|o| o.left_choice_id == l.id && o.right_choice_id == r.id}
  127 + keystring = "#{l.id},#{r.id}"
  128 + p = the_prompts_hash[keystring]
  129 + if p.nil?
  130 + inserts.push("(NULL, #{q.id}, NULL, #{l.id}, '#{created_timestring}', '#{updated_timestring}', NULL, 0, #{r.id}, NULL, NULL)")
  131 + promptscount+=1
  132 + end
  133 +
  134 + end
  135 +
  136 + end
  137 + end
  138 +
  139 + print "Added #{promptscount} to #{q.id}\n"
  140 + sql = "INSERT INTO `prompts` (`algorithm_id`, `question_id`, `voter_id`, `left_choice_id`, `created_at`, `updated_at`, `tracking`, `votes_count`, `right_choice_id`, `active`, `randomkey`) VALUES #{inserts.join(', ')}"
  141 + unless inserts.empty?
  142 + ActiveRecord::Base.connection.execute(sql)
  143 + end
  144 +
  145 + Question.update_counters(q.id, :prompts_count => promptscount)
  146 +
  147 +
  148 + end
  149 +
  150 +
  151 +
  152 + end
  153 +
  154 +
  155 +
  156 + desc "Generate past density information"
  157 + task(:generate_past_densities => :environment) do
  158 + #this is not elegant, but should only be run once, so quick and dirty wins
  159 +
  160 + start_date = Vote.find(:all, :conditions => 'loser_choice_id IS NOT NULL', :order => :created_at, :limit => 1).first.created_at.to_date
  161 + start_date.upto(Date.today) do |the_date|
  162 + questions = Question.find(:all, :conditions => ['created_at < ?', the_date])
  163 +
  164 + print the_date.to_s
  165 + questions.each do |q|
  166 + puts q.id
  167 + relevant_choices = q.choices.find(:all, :conditions => ['created_at < ?', the_date])
  168 +
  169 + seed_choices = 0
  170 +
  171 + if relevant_choices == 0
  172 + next
  173 + #this question had not been created yet
  174 + end
  175 +
  176 + relevant_choices.each do |c|
  177 + if !c.user_created
  178 + seed_choices+=1
  179 + end
  180 +
  181 + end
  182 +
  183 + nonseed_choices = relevant_choices.size - seed_choices
  184 +
  185 + seed_seed_total = seed_choices **2 - seed_choices
  186 + nonseed_nonseed_total = nonseed_choices **2 - nonseed_choices
  187 + seed_nonseed_total = seed_choices * nonseed_choices
  188 + nonseed_seed_total = seed_choices * nonseed_choices
  189 +
  190 + seed_seed_sum = 0
  191 + seed_nonseed_sum= 0
  192 + nonseed_seed_sum= 0
  193 + nonseed_nonseed_sum= 0
  194 +
  195 + q.appearances.find_each(:conditions => ['prompt_id IS NOT NULL AND created_at < ?', the_date]) do |a|
  196 +
  197 + p = a.prompt
  198 + if p.left_choice.user_created == false && p.right_choice.user_created == false
  199 + seed_seed_sum += 1
  200 + elsif p.left_choice.user_created == false && p.right_choice.user_created == true
  201 + seed_nonseed_sum += 1
  202 + elsif p.left_choice.user_created == true && p.right_choice.user_created == false
  203 + nonseed_seed_sum += 1
  204 + elsif p.left_choice.user_created == true && p.right_choice.user_created == true
  205 + nonseed_nonseed_sum += 1
  206 + end
  207 + end
  208 +
  209 + densities = {}
  210 + densities[:seed_seed] = seed_seed_sum.to_f / seed_seed_total.to_f
  211 + densities[:seed_nonseed] = seed_nonseed_sum.to_f / seed_nonseed_total.to_f
  212 + densities[:nonseed_seed] = nonseed_seed_sum.to_f / nonseed_seed_total.to_f
  213 + densities[:nonseed_nonseed] = nonseed_nonseed_sum.to_f / nonseed_nonseed_total.to_f
  214 +
  215 + densities.each do |type, average|
  216 + d = Density.new
  217 + d.created_at = the_date
  218 + d.question_id = q.id
  219 + d.prompt_type = type.to_s
  220 + d.value = average.nan? ? nil : average
  221 + d.save!
  222 + end
  223 +
  224 + puts "Seed_seed sum: #{seed_seed_sum}, seed_seed total num: #{seed_seed_total}"
  225 + puts "Seed_nonseed sum: #{seed_nonseed_sum}, seed_nonseed total num: #{seed_nonseed_total}"
  226 + puts "Nonseed_seed sum: #{nonseed_seed_sum}, nonseed_seed total num: #{nonseed_seed_total}"
  227 + puts "Nonseed_nonseed sum: #{nonseed_nonseed_sum}, nonseed_nonseed total num: #{nonseed_nonseed_total}"
  228 +
  229 +
  230 + end
  231 +
  232 + end
  233 +
  234 + end
  235 +
  236 +
20 237 desc "Invalidates votes with bad response times"
21 238 task :invalidate_votes_with_bad_response_times => :environment do
22 239 badvotes = []
... ...
lib/tasks/test_api.rake
... ... @@ -68,223 +68,6 @@ namespace :test_api do
68 68 return error_message.blank? ? [success_message, false] : [error_message, true]
69 69 end
70 70  
71   - desc "Don't run unless you know what you are doing"
72   - task(:generate_lots_of_votes => :environment) do
73   - if Rails.env.production?
74   - print "You probably don't want to run this in production as it will falsify a bunch of random votes"
75   - end
76   -
77   -
78   - current_user = User.first
79   - 1000.times do |n|
80   - puts "#{n} votes completed" if n % 100 == 0
81   - question = Question.find(214) # test question change as needed
82   - @prompt = question.catchup_choose_prompt(1).first
83   - @appearance = current_user.record_appearance(current_user.default_visitor, @prompt)
84   -
85   - direction = (rand(2) == 0) ? "left" : "right"
86   - current_user.record_vote(:prompt => @prompt, :direction => direction, :appearance_lookup => @appearance.lookup)
87   - end
88   -
89   - end
90   -
91   - desc "Generate appearances for any votes that have no current appearance, should only need to be run once"
92   - task(:generate_appearances_for_existing_votes => :environment) do
93   - votes = Vote.all
94   -
95   - count = 0
96   - votes.each do |v|
97   - if v.appearance.nil?
98   - print "."
99   - a = Appearance.create(:voter_id => v.voter_id, :site_id => v.site_id, :prompt_id => v.prompt_id, :question_id => v.question_id, :created_at => v.created_at, :updated_at => v.updated_at)
100   - v.appearance = a
101   - v.save
102   -
103   - count += 1
104   - end
105   - end
106   -
107   - print count
108   - end
109   -
110   -
111   - desc "Generate past density information"
112   - task(:generate_past_densities => :environment) do
113   - #this is not elegant, but should only be run once, so quick and dirty wins
114   -
115   - start_date = Vote.find(:all, :conditions => 'loser_choice_id IS NOT NULL', :order => :created_at, :limit => 1).first.created_at.to_date
116   - start_date.upto(Date.today) do |the_date|
117   - questions = Question.find(:all, :conditions => ['created_at < ?', the_date])
118   -
119   - print the_date.to_s
120   - questions.each do |q|
121   - puts q.id
122   - relevant_choices = q.choices.find(:all, :conditions => ['created_at < ?', the_date])
123   -
124   - seed_choices = 0
125   -
126   - if relevant_choices == 0
127   - next
128   - #this question had not been created yet
129   - end
130   -
131   - relevant_choices.each do |c|
132   - if !c.user_created
133   - seed_choices+=1
134   - end
135   -
136   - end
137   -
138   - nonseed_choices = relevant_choices.size - seed_choices
139   -
140   - seed_seed_total = seed_choices **2 - seed_choices
141   - nonseed_nonseed_total = nonseed_choices **2 - nonseed_choices
142   - seed_nonseed_total = seed_choices * nonseed_choices
143   - nonseed_seed_total = seed_choices * nonseed_choices
144   -
145   - seed_seed_sum = 0
146   - seed_nonseed_sum= 0
147   - nonseed_seed_sum= 0
148   - nonseed_nonseed_sum= 0
149   -
150   - q.appearances.find_each(:conditions => ['prompt_id IS NOT NULL AND created_at < ?', the_date]) do |a|
151   -
152   - p = a.prompt
153   - if p.left_choice.user_created == false && p.right_choice.user_created == false
154   - seed_seed_sum += 1
155   - elsif p.left_choice.user_created == false && p.right_choice.user_created == true
156   - seed_nonseed_sum += 1
157   - elsif p.left_choice.user_created == true && p.right_choice.user_created == false
158   - nonseed_seed_sum += 1
159   - elsif p.left_choice.user_created == true && p.right_choice.user_created == true
160   - nonseed_nonseed_sum += 1
161   - end
162   - end
163   -
164   - densities = {}
165   - densities[:seed_seed] = seed_seed_sum.to_f / seed_seed_total.to_f
166   - densities[:seed_nonseed] = seed_nonseed_sum.to_f / seed_nonseed_total.to_f
167   - densities[:nonseed_seed] = nonseed_seed_sum.to_f / nonseed_seed_total.to_f
168   - densities[:nonseed_nonseed] = nonseed_nonseed_sum.to_f / nonseed_nonseed_total.to_f
169   -
170   - densities.each do |type, average|
171   - d = Density.new
172   - d.created_at = the_date
173   - d.question_id = q.id
174   - d.prompt_type = type.to_s
175   - d.value = average.nan? ? nil : average
176   - d.save!
177   - end
178   -
179   - puts "Seed_seed sum: #{seed_seed_sum}, seed_seed total num: #{seed_seed_total}"
180   - puts "Seed_nonseed sum: #{seed_nonseed_sum}, seed_nonseed total num: #{seed_nonseed_total}"
181   - puts "Nonseed_seed sum: #{nonseed_seed_sum}, nonseed_seed total num: #{nonseed_seed_total}"
182   - puts "Nonseed_nonseed sum: #{nonseed_nonseed_sum}, nonseed_nonseed total num: #{nonseed_nonseed_total}"
183   -
184   -
185   - end
186   -
187   - end
188   -
189   - end
190   -
191   -
192   - desc "Should only need to be run once"
193   - task(:generate_all_possible_prompts => :environment) do
194   - Question.find(:all).each do |q|
195   - choices = q.choices
196   - if q.prompts.size > choices.size**2 - choices.size
197   - print "ERROR: #{q.id}\n"
198   - next
199   - elsif q.prompts.size == choices.size**2 - choices.size
200   - print "#{q.id} has enough prompts, skipping...\n"
201   - next
202   - else
203   - print "#{q.id} should add #{(choices.size ** 2 - choices.size) - q.prompts.size}\n"
204   -
205   - end
206   - created_timestring = q.created_at.to_s(:db)
207   - updated_timestring = Time.now.to_s(:db) #isn't rails awesome?
208   - promptscount=0
209   - inserts = []
210   - the_prompts = Prompt.find(:all, :select => 'id, left_choice_id, right_choice_id', :conditions => {:question_id => q.id})
211   -
212   - the_prompts_hash = {}
213   - the_prompts.each do |p|
214   - the_prompts_hash["#{p.left_choice_id},#{p.right_choice_id}"] = 1
215   - end
216   -
217   - choices.each do |l|
218   - choices.each do |r|
219   - if l.id == r.id
220   - next
221   - else
222   - #p = the_prompts.find{|o| o.left_choice_id == l.id && o.right_choice_id == r.id}
223   - keystring = "#{l.id},#{r.id}"
224   - p = the_prompts_hash[keystring]
225   - if p.nil?
226   - inserts.push("(NULL, #{q.id}, NULL, #{l.id}, '#{created_timestring}', '#{updated_timestring}', NULL, 0, #{r.id}, NULL, NULL)")
227   - promptscount+=1
228   - end
229   -
230   - end
231   -
232   - end
233   - end
234   -
235   - print "Added #{promptscount} to #{q.id}\n"
236   - sql = "INSERT INTO `prompts` (`algorithm_id`, `question_id`, `voter_id`, `left_choice_id`, `created_at`, `updated_at`, `tracking`, `votes_count`, `right_choice_id`, `active`, `randomkey`) VALUES #{inserts.join(', ')}"
237   - unless inserts.empty?
238   - ActiveRecord::Base.connection.execute(sql)
239   - end
240   -
241   - Question.update_counters(q.id, :prompts_count => promptscount)
242   -
243   -
244   - end
245   -
246   -
247   -
248   - end
249   -
250   -
251   - desc "Dump votes of a question by left vs right id"
252   - task(:make_csv => :environment) do
253   -
254   - q = Question.find(214)
255   -
256   -
257   - the_prompts = q.prompts_hash_by_choice_ids
258   -
259   - #hash_of_choice_ids_from_left_to_right_to_votes
260   - the_hash = {}
261   - q.choices.each do |l|
262   - q.choices.each do |r|
263   - next if l.id == r.id
264   -
265   - if not the_hash.has_key?(l.id)
266   - the_hash[l.id] = {}
267   - the_hash[l.id][l.id] = 0
268   - end
269   -
270   - p = the_prompts["#{l.id}, #{r.id}"]
271   - if p.nil?
272   - the_hash[l.id][r.id] = 0
273   - else
274   - the_hash[l.id][r.id] = p.appearances.size
275   - end
276   - end
277   - end
278   -
279   - the_hash.sort.each do |xval, row|
280   - rowarray = []
281   - row.sort.each do |yval, cell|
282   - rowarray << cell
283   - end
284   - puts rowarray.join(", ")
285   - end
286   - end
287   -
288 71  
289 72 desc "Generate density information for each question - should be run nightly"
290 73 task(:generate_density_information => :environment) do
... ... @@ -663,8 +446,11 @@ namespace :test_api do
663 446  
664 447 desc "Ensure that a question has: answered_appearances == votes + skips"
665 448 task :answered_appearances_equals_votes_and_skips, [:question_id] => :environment do |t, args|
666   - question = Question.find(args[:question_id])
667   - puts answered_appearances_equals_votes_and_skips(question).inspect
  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
  453 + end
668 454 end
669 455  
670 456 def answered_appearances_equals_votes_and_skips(question)
... ... @@ -726,3 +512,14 @@ namespace :test_api do
726 512 end
727 513 end
728 514  
  515 +def cleanup_args(args)
  516 + args.with_defaults(:question_id => :all, :choice_id => :all)
  517 + a = args.to_hash
  518 + if a[:question_id] != :all
  519 + a[:question_id] = a[:question_id].split(".")
  520 + end
  521 + if a[:choice_id] != :all
  522 + a[:choice_id] = a[:choice_id].split(".")
  523 + end
  524 + a
  525 +end
... ...