diff --git a/app/controllers/prompts_controller.rb b/app/controllers/prompts_controller.rb index 16277b4..09f9cd1 100644 --- a/app/controllers/prompts_controller.rb +++ b/app/controllers/prompts_controller.rb @@ -144,8 +144,11 @@ class PromptsController < InheritedResources::Base logger.info("Question #{@question.id} is using catchup algorithm!") @next_prompt = @question.pop_prompt_queue if @next_prompt.nil? + @question.record_prompt_cache_miss logger.info("Catchup prompt cache miss! Nothing in prompt_queue") @next_prompt = @question.catchup_choose_prompt + else + @question.record_prompt_cache_hit end @question.send_later :add_prompt_to_queue else diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index ad53d7f..689f5f7 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -64,6 +64,9 @@ class QuestionsController < InheritedResources::Base if @p.nil? logger.info("Catchup prompt cache miss! Nothing in prompt_queue") @p = @question.catchup_choose_prompt + @question.record_prompt_cache_miss + else + @question.record_prompt_cache_hit end @question.send_later :add_prompt_to_queue diff --git a/app/models/question.rb b/app/models/question.rb index f45ce02..208f8e5 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -240,7 +240,6 @@ class Question < ActiveRecord::Base def density # slow code, only to be run by cron job once at night - the_prompts = prompts.find(:all, :include => ['left_choice', 'right_choice']) seed_seed_sum = 0 seed_seed_total = 0 @@ -254,7 +253,8 @@ class Question < ActiveRecord::Base nonseed_nonseed_sum= 0 nonseed_nonseed_total= 0 - the_prompts.each do |p| + #the_prompts = prompts.find(:all, :include => ['left_choice', 'right_choice']) + prompts.find_each(:include => ['left_choice', 'right_choice']) do |p| if p.left_choice.user_created == false && p.right_choice.user_created == false seed_seed_sum += p.appearances.size seed_seed_total +=1 @@ -317,7 +317,22 @@ class Question < ActiveRecord::Base prompt = prompt_id.nil? ? nil : Prompt.find(prompt_id.to_i) end + def record_prompt_cache_miss + $redis.incr(self.pq_key + "_" + Time.now.to_date.to_s + "_"+ "misses") + $redis.expire(self.pq_key, 24*60*60 * 3) #Expire in three days + end + + def record_prompt_cache_hit + $redis.incr(self.pq_key + "_" + Time.now.to_date.to_s + "_"+ "hits") + $redis.expire(self.pq_key, 24*60*60 * 3) #Expire in three days + end + def get_prompt_cache_misses(date) + $redis.get(self.pq_key + "_" + date.to_s + "_"+ "misses") + end + def get_prompt_cache_hits(date) + $redis.get(self.pq_key + "_" + date.to_s + "_"+ "hits") + end diff --git a/lib/tasks/test_api.rake b/lib/tasks/test_api.rake index 02e2644..38377e2 100644 --- a/lib/tasks/test_api.rake +++ b/lib/tasks/test_api.rake @@ -352,6 +352,25 @@ namespace :test_api do end + #catchup specific + if question.uses_catchup? + misses = question.get_prompt_cache_misses(Date.yesterday).to_i + hits = question.get_prompt_cache_hits(Date.yesterday).to_i + + + yesterday_votes = question.appearances.count(:conditions => ['date(created_at) = ?', Date.yesterday]) + + if misses + hits != yesterday_votes + error_msg += "Error! Question #{question.id} isn't tracking prompt cache hits and misses accurately! Expected #{yesterday_votes}, Actual: #{misses+hits}\n" + end + + miss_rate = misses.to_f / yesterday_votes.to_f + if miss_rate > 0.1 + error_msg += "Error! Question #{question.id} has less than 90% of appearances taken from a pre-generated cache! Expected <#{0.1}, Actual: #{miss_rate}\n" + end + end + + if error_bool error_msg += "Question #{question.id}: 2*wins = #{2*total_wins}, total votes = #{total_votes}, vote_count = #{question.votes_count}\n" end @@ -416,6 +435,7 @@ namespace :test_api do " The prompt count matches the expected number of prompts ( num_choices ^2 - num choices) for each question\n" + " All Vote objects have an associated appearance object\n" + " All Vote objects have an client response time < calculated server roundtrip time\n" + " More than 90% of prompts on catchup algorithm questions were served from cache\n" print success_msg @@ -423,6 +443,9 @@ namespace :test_api do else CronMailer.deliver_info_message("#{CRON_EMAIL},#{ERRORS_EMAIL}", "Error! Failure of API Vote Consistency " , error_msg) + puts "There were errors: " + puts error_msg + unless bad_choices.blank? puts "Here's a list of choice ids that you may want to modify: #{bad_choices.uniq.inspect}" -- libgit2 0.21.2