From c12546915f639f23fb848e745ff6749df6aeb2f9 Mon Sep 17 00:00:00 2001 From: Pius Uzamere Date: Thu, 3 Dec 2009 21:05:02 -0500 Subject: [PATCH] DRAMATIC improvements in performance through counter caches and more intelligent picking --- app/controllers/clicks_controller.rb | 10 +++++++--- app/controllers/questions_controller.rb | 3 ++- app/models/choice.rb | 8 ++------ app/models/prompt.rb | 12 ++++-------- app/models/question.rb | 25 ++++++------------------- app/models/vote.rb | 2 +- db/migrate/20091204012430_add_counter_caches_to_choices.rb | 20 ++++++++++++++++++++ db/migrate/20091204013843_add_votes_count_to_prompts.rb | 15 +++++++++++++++ db/migrate/20091204015325_add_counter_caches_to_questions.rb | 16 ++++++++++++++++ 9 files changed, 73 insertions(+), 38 deletions(-) create mode 100644 db/migrate/20091204012430_add_counter_caches_to_choices.rb create mode 100644 db/migrate/20091204013843_add_votes_count_to_prompts.rb create mode 100644 db/migrate/20091204015325_add_counter_caches_to_questions.rb diff --git a/app/controllers/clicks_controller.rb b/app/controllers/clicks_controller.rb index 4f3562a..d4c3907 100644 --- a/app/controllers/clicks_controller.rb +++ b/app/controllers/clicks_controller.rb @@ -40,9 +40,13 @@ class ClicksController < ApplicationController # POST /clicks # POST /clicks.xml def create - authenticate - p = params[:click].except(:sid).merge(:visitor_id => current_user.visitors.find_or_create_by_identifier(params[:click][:sid]).id) - @click = Click.new(p) + #authenticate + if signed_in? + p = params[:click].except(:sid).merge(:visitor_id => current_user.visitors.find_or_create_by_identifier(params[:click][:sid]).id) + @click = Click.new(p) + else + render :nothing => true and return + end respond_to do |format| if @click.save diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index 5cbace4..138df86 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -7,7 +7,8 @@ class QuestionsController < InheritedResources::Base show! do |format| session['prompts_ids'] ||= [] format.xml { - render :xml => @question.to_xml(:methods => [:item_count, :left_choice_text, :right_choice_text, :picked_prompt_id, :votes_count, :creator_id]) + #render :xml => @question.to_xml(:methods => [:item_count, :left_choice_text, :right_choice_text, :picked_prompt_id, :votes_count, :creator_id]) + render :xml => @question.to_xml(:methods => [:item_count, :left_choice_text, :right_choice_text, :picked_prompt_id]) } end end diff --git a/app/models/choice.rb b/app/models/choice.rb index bbe4f18..2f79cd6 100644 --- a/app/models/choice.rb +++ b/app/models/choice.rb @@ -17,18 +17,14 @@ class Choice < ActiveRecord::Base end def wins_plus_losses - (prompts_on_the_left.collect(&:votes_count).sum + prompts_on_the_right.collect(&:votes_count).sum) + #(prompts_on_the_left.collect(&:votes_count).sum + prompts_on_the_right.collect(&:votes_count).sum) + Prompt.sum('votes_count', :conditions => "left_choice_id = #{id} OR right_choice_id = #{id}") end def wins votes_count end - def votes_count - votes(true).size - end - - after_create :generate_prompts def before_create unless item diff --git a/app/models/prompt.rb b/app/models/prompt.rb index 2f4c97a..f31056e 100644 --- a/app/models/prompt.rb +++ b/app/models/prompt.rb @@ -2,12 +2,12 @@ class Prompt < ActiveRecord::Base #has_many :choices, :order => 'position DESC' has_many :skips - has_many :votes, :as => :voteable + has_many :votes, :as => :voteable - belongs_to :question - belongs_to :left_choice, :class_name => "Choice", :foreign_key => "left_choice_id" - belongs_to :right_choice, :class_name => "Choice", :foreign_key => "right_choice_id" + belongs_to :question, :counter_cache => true + belongs_to :left_choice, :class_name => "Choice", :foreign_key => "left_choice_id", :counter_cache => true + belongs_to :right_choice, :class_name => "Choice", :foreign_key => "right_choice_id", :counter_cache => true named_scope :with_left_choice, lambda { |*args| {:conditions => ["left_choice_id = ?", (args.first.id)]} } named_scope :with_right_choice, lambda { |*args| {:conditions => ["right_choice_id = ?", (args.first.id)]} } @@ -26,10 +26,6 @@ class Prompt < ActiveRecord::Base validates_presence_of :left_choice, :on => :create, :message => "can't be blank" validates_presence_of :right_choice, :on => :create, :message => "can't be blank" - def votes_count - votes(true).size - end - def choices [left_choice, right_choice] end diff --git a/app/models/question.rb b/app/models/question.rb index 997b68c..c6c81ff 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -18,31 +18,18 @@ class Question < ActiveRecord::Base attr_accessor :ideas def item_count - choice_count + choices_count end - def prompt_count - Prompt.count(:all, :conditions => {:question_id => id}) - end - - def choice_count - Choice.count(:all, :conditions => {:question_id => id}) - end - - def votes_count - Vote.count(:all, :conditions => {:voteable_id => id, :voteable_type => 'Question'}) - end - def picked_prompt - prompts[rand(prompts.count-1)]#Prompt.find(picked_prompt_id) + #prompts[rand(prompts.count-1)]#Prompt.find(picked_prompt_id) + Prompt.find(rand(prompts_count-1)) end - + def picked_prompt_id - lambda {@picked ||= prompts(true)[rand(prompts.count-1)].id}.call - - lambda { prompts(true)[rand(prompts.count-1)].id }.call + rand(prompts_count-1) end - + def left_choice_text(prompt = nil) prompt ||= prompts.first#prompts.pick picked_prompt.left_choice.item.data diff --git a/app/models/vote.rb b/app/models/vote.rb index 049c65d..b9ec8d5 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -1,4 +1,4 @@ class Vote < ActiveRecord::Base - belongs_to :voteable, :polymorphic => true + belongs_to :voteable, :polymorphic => true, :counter_cache => true belongs_to :voter, :class_name => "Visitor", :foreign_key => "voter_id" end diff --git a/db/migrate/20091204012430_add_counter_caches_to_choices.rb b/db/migrate/20091204012430_add_counter_caches_to_choices.rb new file mode 100644 index 0000000..f994ece --- /dev/null +++ b/db/migrate/20091204012430_add_counter_caches_to_choices.rb @@ -0,0 +1,20 @@ +class AddCounterCachesToChoices < ActiveRecord::Migration + def self.up + add_column :choices, :prompts_on_the_left_count, :integer, :default => 0 + add_column :choices, :prompts_on_the_right_count, :integer, :default => 0 + add_column :choices, :votes_count, :integer, :default => 0 + + Choice.reset_column_information + Choice.find(:all).each do |c| + Choice.update_counters c.id, :prompts_on_the_left_count => c.prompts_on_the_left.length + Choice.update_counters c.id, :prompts_on_the_right_count => c.prompts_on_the_right.length + Choice.update_counters c.id, :votes_count => c.votes.length + end + end + + def self.down + remove_column :choices, :votes_count + remove_column :choices, :prompts_on_the_right_count + remove_column :choices, :prompts_on_the_left_count + end +end diff --git a/db/migrate/20091204013843_add_votes_count_to_prompts.rb b/db/migrate/20091204013843_add_votes_count_to_prompts.rb new file mode 100644 index 0000000..6c5808e --- /dev/null +++ b/db/migrate/20091204013843_add_votes_count_to_prompts.rb @@ -0,0 +1,15 @@ +class AddVotesCountToPrompts < ActiveRecord::Migration + def self.up + add_column :prompts, :votes_count, :integer, :default => 0 + + Prompt.reset_column_information + Prompt.find(:all).each do |p| + Prompt.update_counters p.id, :votes_count => p.votes.length + end + + end + + def self.down + remove_column :prompts, :votes_count + end +end diff --git a/db/migrate/20091204015325_add_counter_caches_to_questions.rb b/db/migrate/20091204015325_add_counter_caches_to_questions.rb new file mode 100644 index 0000000..079a6d6 --- /dev/null +++ b/db/migrate/20091204015325_add_counter_caches_to_questions.rb @@ -0,0 +1,16 @@ +class AddCounterCachesToQuestions < ActiveRecord::Migration + def self.up + add_column :questions, :votes_count, :integer, :default => 0 + + Question.reset_column_information + Question.find(:all).each do |q| + Question.update_counters q.id, :choices_count => q.choices.length + Question.update_counters q.id, :prompts_count => q.prompts.length + Question.update_counters q.id, :votes_count => q.votes.length + end + end + + def self.down + remove_column :questions, :votes_count + end +end -- libgit2 0.21.2