Skip to content

Commit

Permalink
Merge branch 'record-prompt-algo'
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebaker committed Oct 7, 2014
2 parents ff7edf0 + 079f21a commit 775e4bd
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 7 deletions.
10 changes: 5 additions & 5 deletions Gemfile.lock
Expand Up @@ -12,12 +12,11 @@ GEM
activeresource (2.3.18)
activesupport (= 2.3.18)
activesupport (2.3.18)
airbrake (3.1.12)
activesupport
airbrake (4.1.0)
builder
json
multi_json
ambethia-smtp-tls (1.1.2)
builder (3.0.0)
builder (3.2.2)
cucumber (1.1.0)
builder (>= 2.1.2)
diff-lcs (>= 1.1.2)
Expand Down Expand Up @@ -45,12 +44,13 @@ GEM
responders (~> 0.4.3)
jferris-mocha (0.9.5.0.1241126838)
rake
json (1.7.7)
json (1.8.1)
json_pure (1.4.6)
jtrupiano-timecop (0.2.1)
libxml-ruby (2.2.2)
mime-types (1.16)
mock_redis (0.4.1)
multi_json (1.10.1)
mysql2 (0.2.18)
newrelic_rpm (3.5.5.38)
nokogiri (1.5.5)
Expand Down
6 changes: 6 additions & 0 deletions app/models/prompt.rb
Expand Up @@ -26,6 +26,12 @@ class Prompt < ActiveRecord::Base
attr_protected :votes_count, :left_choice_id, :right_choice_id
attr_readonly :question_id

# Algorithm used to select this prompt.
#
# This is not saved to the prompt table and only lives as long as this prompt
# instance. We use this to save the algorithm to the appearances table.
attr_accessor :algorithm

def self.voted_on_by(u)
select {|z| z.voted_on_by_user?(u)}
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/question.rb
Expand Up @@ -102,6 +102,7 @@ def simple_random_choose_prompt(rank = 2)
choice_id_array = distinct_array_of_choice_ids(:rank => rank, :only_active => true)
prompt = prompts.find_or_initialize_by_left_choice_id_and_right_choice_id(choice_id_array[0], choice_id_array[1])
prompt.save
prompt.algorithm = {:name => 'simple-random'}
prompt
end

Expand Down Expand Up @@ -526,6 +527,7 @@ def pop_prompt_queue
prompt = prompt_id.nil? ? nil : Prompt.find(prompt_id.to_i)
end until (prompt.nil? || prompt.active?)
$redis.expire(self.pq_key, @@expire_prompt_cache_in_seconds)
prompt.algorithm = {:name => 'catchup'} if prompt
prompt
end

Expand Down
3 changes: 2 additions & 1 deletion app/models/user.rb
Expand Up @@ -41,7 +41,8 @@ def record_vote(options)
end

def record_appearance(visitor, prompt)
a = Appearance.create(:voter => visitor, :prompt => prompt, :question_id => prompt.question_id, :site_id => self.id, :lookup => Digest::MD5.hexdigest(rand(10000000000).to_s + visitor.id.to_s + prompt.id.to_s) )
algorithm_name = prompt.algorithm[:name] || prompt.algorithm['name'] unless prompt.algorithm.nil?
Appearance.create(:voter => visitor, :prompt => prompt, :question_id => prompt.question_id, :site_id => self.id, :lookup => Digest::MD5.hexdigest(rand(10000000000).to_s + visitor.id.to_s + prompt.id.to_s), :algorithm_metadata => prompt.algorithm.to_json, :algorithm_name => algorithm_name )
end


Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20140609193423_add_algorithm_to_appearance.rb
@@ -0,0 +1,11 @@
class AddAlgorithmToAppearance < ActiveRecord::Migration
def self.up
add_column :appearances, :algorithm_name, :string
add_column :appearances, :algorithm_metadata, :text
end

def self.down
remove_column :appearances, :algorithm_name
remove_column :appearances, :algorithm_metadata
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Expand Up @@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20140327170621) do
ActiveRecord::Schema.define(:version => 20140609193423) do

create_table "appearances", :force => true do |t|
t.integer "voter_id"
Expand All @@ -23,6 +23,8 @@
t.string "answerable_type"
t.boolean "valid_record", :default => true
t.string "validity_information"
t.string "algorithm_name"
t.text "algorithm_metadata"
end

add_index "appearances", ["answerable_id", "answerable_type"], :name => "index_appearances_on_answerable_id_and_answerable_type"
Expand Down
17 changes: 17 additions & 0 deletions spec/models/question_spec.rb
Expand Up @@ -75,6 +75,11 @@
prompt.active?.should == true
end

it "should set the algorithm attribute on prompt after choice" do
prompt = @question.choose_prompt()
prompt.algorithm.should == {:name => 'simple-random'}
end

it "should raise runtime exception if there is no possible prompt to choose" do
@question.choices.active.each{|c| c.deactivate!}
@question.reload
Expand Down Expand Up @@ -428,6 +433,18 @@
end


it "should set the algorithm attribute on prompt after choice" do
@catchup_q.add_prompt_to_queue
prompt = @catchup_q.choose_prompt(:algorithm => 'catchup')
prompt.algorithm.should == {:name => 'catchup'}
end

it "should set the algorithm attribute to simple-random on prompt after choice if prompt queue is empty" do
@catchup_q.clear_prompt_queue
prompt = @catchup_q.choose_prompt(:algorithm => 'catchup')
prompt.algorithm.should == {:name => 'simple-random'}
end

it "should create a delayed job after requesting a prompt" do
proc { @catchup_q.choose_prompt}.should change(Delayed::Job, :count).by(1)
end
Expand Down
Binary file removed vendor/cache/airbrake-3.1.12.gem
Binary file not shown.
Binary file added vendor/cache/airbrake-4.1.0.gem
Binary file not shown.
Binary file removed vendor/cache/builder-3.0.0.gem
Binary file not shown.
Binary file added vendor/cache/builder-3.2.2.gem
Binary file not shown.
Binary file removed vendor/cache/json-1.7.7.gem
Binary file not shown.
Binary file added vendor/cache/json-1.8.1.gem
Binary file not shown.
Binary file added vendor/cache/multi_json-1.10.1.gem
Binary file not shown.

0 comments on commit 775e4bd

Please sign in to comment.