Commit d31e8f8fe4ba99e3e8e2ad15be1397d2f435dd37
1 parent
af179ec4
Exists in
master
and in
1 other branch
add vote_rate call
Showing
7 changed files
with
106 additions
and
1 deletions
Show diff stats
CHANGELOG.md
app/controllers/questions_controller.rb
| @@ -206,6 +206,15 @@ class QuestionsController < InheritedResources::Base | @@ -206,6 +206,15 @@ class QuestionsController < InheritedResources::Base | ||
| 206 | end | 206 | end |
| 207 | end | 207 | end |
| 208 | 208 | ||
| 209 | + def vote_rate | ||
| 210 | + @question = current_user.questions.find(params[:id]) | ||
| 211 | + response = {:voterate => @question.vote_rate} | ||
| 212 | + logger.info(@question.inspect) | ||
| 213 | + respond_to do |format| | ||
| 214 | + format.xml { render :xml => response.to_xml and return} | ||
| 215 | + end | ||
| 216 | + end | ||
| 217 | + | ||
| 209 | def object_info_totals_by_date | 218 | def object_info_totals_by_date |
| 210 | object_type = params[:object_type] | 219 | object_type = params[:object_type] |
| 211 | 220 |
app/models/question.rb
| @@ -672,6 +672,19 @@ class Question < ActiveRecord::Base | @@ -672,6 +672,19 @@ class Question < ActiveRecord::Base | ||
| 672 | last_appearance | 672 | last_appearance |
| 673 | end | 673 | end |
| 674 | 674 | ||
| 675 | + def vote_rate | ||
| 676 | + return 0.to_f if total_uniq_sessions == 0 | ||
| 677 | + sessions_with_vote.to_f / total_uniq_sessions.to_f | ||
| 678 | + end | ||
| 679 | + | ||
| 680 | + def total_uniq_sessions | ||
| 681 | + appearances.count(:select => "DISTINCT(voter_id)") | ||
| 682 | + end | ||
| 675 | 683 | ||
| 684 | + def sessions_with_vote | ||
| 685 | + Question.connection.select_one(" | ||
| 686 | + SELECT COUNT(DISTINCT(appearances.voter_id)) from appearances LEFT JOIN votes ON (votes.voter_id = appearances.voter_id) WHERE votes.id IS NOT NULL AND appearances.question_id = #{self.id} | ||
| 687 | + ").values.first | ||
| 688 | + end | ||
| 676 | 689 | ||
| 677 | end | 690 | end |
config/routes.rb
| @@ -7,6 +7,7 @@ ActionController::Routing::Routes.draw do |map| | @@ -7,6 +7,7 @@ ActionController::Routing::Routes.draw do |map| | ||
| 7 | :member => {:object_info_totals_by_date => :get, | 7 | :member => {:object_info_totals_by_date => :get, |
| 8 | :object_info_by_visitor_id => :get, | 8 | :object_info_by_visitor_id => :get, |
| 9 | :median_votes_per_session => :get, | 9 | :median_votes_per_session => :get, |
| 10 | + :vote_rate => :get, | ||
| 10 | :export => :post} , | 11 | :export => :post} , |
| 11 | :collection => {:all_num_votes_by_visitor_id => :get, | 12 | :collection => {:all_num_votes_by_visitor_id => :get, |
| 12 | :all_object_info_totals_by_date => :get, | 13 | :all_object_info_totals_by_date => :get, |
spec/factories.rb
| @@ -67,21 +67,31 @@ Factory.define(:vote) do |f| | @@ -67,21 +67,31 @@ Factory.define(:vote) do |f| | ||
| 67 | f.choice {|v| v.prompt.left_choice} | 67 | f.choice {|v| v.prompt.left_choice} |
| 68 | f.loser_choice {|v| v.prompt.right_choice} | 68 | f.loser_choice {|v| v.prompt.right_choice} |
| 69 | f.voter {|v| v.question.creator} | 69 | f.voter {|v| v.question.creator} |
| 70 | + f.appearance { |v| Factory.build(:appearance, :voter => v.voter, :question => v.question) } | ||
| 71 | +end | ||
| 72 | + | ||
| 73 | +Factory.define(:vote_new_user, :parent => :vote) do |f| | ||
| 74 | + f.voter {|v| Factory.build(:visitor, :site => v.question.site)} | ||
| 70 | end | 75 | end |
| 71 | 76 | ||
| 72 | Factory.define(:skip) do |f| | 77 | Factory.define(:skip) do |f| |
| 73 | f.association :question, :factory => :aoi_question | 78 | f.association :question, :factory => :aoi_question |
| 79 | + f.appearance { |v| Factory.build(:appearance, :question => v.question) } | ||
| 74 | f.prompt {|s| s.question.prompts.first} | 80 | f.prompt {|s| s.question.prompts.first} |
| 75 | f.skipper {|s| s.question.creator} | 81 | f.skipper {|s| s.question.creator} |
| 76 | end | 82 | end |
| 77 | 83 | ||
| 78 | Factory.define(:appearance) do |f| | 84 | Factory.define(:appearance) do |f| |
| 79 | f.association :question, :factory => :aoi_question | 85 | f.association :question, :factory => :aoi_question |
| 80 | - f.prompt {|a| a.question.prompts.rand} | 86 | + f.prompt {|a| a.question.prompts.sample} |
| 81 | f.voter {|a| a.question.creator} | 87 | f.voter {|a| a.question.creator} |
| 82 | f.answerable { nil } | 88 | f.answerable { nil } |
| 83 | end | 89 | end |
| 84 | 90 | ||
| 91 | +Factory.define(:appearance_new_user, :parent => :appearance) do |f| | ||
| 92 | + f.voter {|a| Factory.build(:visitor, :site => a.question.site)} | ||
| 93 | +end | ||
| 94 | + | ||
| 85 | Factory.sequence :email do |n| | 95 | Factory.sequence :email do |n| |
| 86 | "user#{n}@example.com" | 96 | "user#{n}@example.com" |
| 87 | end | 97 | end |
spec/integration/questions_spec.rb
| @@ -2,6 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') | @@ -2,6 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') | ||
| 2 | 2 | ||
| 3 | describe "Questions" do | 3 | describe "Questions" do |
| 4 | include IntegrationSupport | 4 | include IntegrationSupport |
| 5 | + include DBSupport | ||
| 5 | before do | 6 | before do |
| 6 | @user = self.default_user = Factory(:email_confirmed_user) | 7 | @user = self.default_user = Factory(:email_confirmed_user) |
| 7 | @choices = {} | 8 | @choices = {} |
| @@ -251,4 +252,28 @@ describe "Questions" do | @@ -251,4 +252,28 @@ describe "Questions" do | ||
| 251 | describe "GET 'object_info_totals_by_date'" do | 252 | describe "GET 'object_info_totals_by_date'" do |
| 252 | end | 253 | end |
| 253 | 254 | ||
| 255 | + describe "GET 'vote_rate'" do | ||
| 256 | + before(:all) { truncate_all } | ||
| 257 | + it "should return the proper vote rate one vote" do | ||
| 258 | + Factory.create(:vote, :question => @questions.first) | ||
| 259 | + get_auth vote_rate_question_path(@questions.first, :format => 'xml') | ||
| 260 | + response.should be_success | ||
| 261 | + response.body.should have_tag("voterate", :text => "1.0") | ||
| 262 | + end | ||
| 263 | + it "should return the proper vote rate if 1 vote and 3 non-vote" do | ||
| 264 | + Factory.create(:vote, :question => @questions.first) | ||
| 265 | + 3.times do | ||
| 266 | + Factory.create(:appearance_new_user, :question => @questions.first) | ||
| 267 | + end | ||
| 268 | + get_auth vote_rate_question_path(@questions.first, :format => 'xml') | ||
| 269 | + response.should be_success | ||
| 270 | + response.body.should have_tag("voterate", :text => "0.25") | ||
| 271 | + end | ||
| 272 | + it "should return the proper vote rate if no votes" do | ||
| 273 | + get_auth vote_rate_question_path(@questions.first, :format => 'xml') | ||
| 274 | + response.should be_success | ||
| 275 | + response.body.should have_tag("voterate", :text => "0.0") | ||
| 276 | + end | ||
| 277 | + end | ||
| 278 | + | ||
| 254 | end | 279 | end |
spec/models/question_spec.rb
| @@ -248,6 +248,51 @@ describe Question do | @@ -248,6 +248,51 @@ describe Question do | ||
| 248 | endTime = Time.now | 248 | endTime = Time.now |
| 249 | (endTime - start).should < 20 | 249 | (endTime - start).should < 20 |
| 250 | end | 250 | end |
| 251 | + | ||
| 252 | + context "vote rate" do | ||
| 253 | + before(:all) do | ||
| 254 | + truncate_all | ||
| 255 | + @q = Factory.create(:aoi_question) | ||
| 256 | + end | ||
| 257 | + | ||
| 258 | + it "should give proper stats required for vote rate" do | ||
| 259 | + @q.total_uniq_sessions.should == 0 | ||
| 260 | + @q.sessions_with_vote.should == 0 | ||
| 261 | + @q.vote_rate.should == 0.0 | ||
| 262 | + | ||
| 263 | + # add new session + appearance, but no vote | ||
| 264 | + Factory.create(:appearance_new_user, :question => @q) | ||
| 265 | + @q.total_uniq_sessions.should == 1 | ||
| 266 | + @q.sessions_with_vote.should == 0 | ||
| 267 | + @q.vote_rate.should == 0.0 | ||
| 268 | + | ||
| 269 | + # add new vote + session | ||
| 270 | + Factory.create(:vote, :question => @q) | ||
| 271 | + Factory.create(:vote, :question => @q) | ||
| 272 | + Factory.create(:vote, :question => @q) | ||
| 273 | + @q.total_uniq_sessions.should == 2 | ||
| 274 | + @q.sessions_with_vote.should == 1 | ||
| 275 | + @q.vote_rate.should == 0.5 | ||
| 276 | + | ||
| 277 | + # add new session + appearance, but no vote | ||
| 278 | + Factory.create(:appearance_new_user, :question => @q) | ||
| 279 | + @q.total_uniq_sessions.should == 3 | ||
| 280 | + @q.sessions_with_vote.should == 1 | ||
| 281 | + @q.vote_rate.should == (1.to_f / 3.to_f) | ||
| 282 | + | ||
| 283 | + # add new session + appearance, but no vote | ||
| 284 | + Factory.create(:appearance_new_user, :question => @q) | ||
| 285 | + @q.total_uniq_sessions.should == 4 | ||
| 286 | + @q.sessions_with_vote.should == 1 | ||
| 287 | + @q.vote_rate.should == 0.25 | ||
| 288 | + | ||
| 289 | + # add new vote + session | ||
| 290 | + v = Factory.create(:vote_new_user, :question => @q) | ||
| 291 | + @q.total_uniq_sessions.should == 5 | ||
| 292 | + @q.sessions_with_vote.should == 2 | ||
| 293 | + @q.vote_rate.should == 0.4 | ||
| 294 | + end | ||
| 295 | + end | ||
| 251 | context "catchup algorithm" do | 296 | context "catchup algorithm" do |
| 252 | before(:all) do | 297 | before(:all) do |
| 253 | @catchup_q = Factory.create(:aoi_question) | 298 | @catchup_q = Factory.create(:aoi_question) |