From b36bc6f78e2877a3b90eb27a32d2d711a4755268 Mon Sep 17 00:00:00 2001 From: Luke Baker Date: Thu, 23 Feb 2012 16:40:25 -0500 Subject: [PATCH] add median_responses_per_session API call --- CHANGELOG.md | 1 + app/controllers/questions_controller.rb | 7 +++++++ app/models/question.rb | 11 +++++++++++ config/routes.rb | 1 + spec/integration/questions_spec.rb | 13 +++++++++++++ spec/models/question_spec.rb | 29 +++++++++++++++++++++++++++++ 6 files changed, 62 insertions(+), 0 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa2ea9b..0059fe8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ + * Added median_responses_per_session call to API * Added upload_to_participation_ratio call to API * Added vote_rate call to API diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index f6105b6..27a549b 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -91,6 +91,13 @@ class QuestionsController < InheritedResources::Base end end + def median_responses_per_session + @question = current_user.questions.find(params[:id]) + respond_to do |format| + format.xml{ render :xml => {:median => @question.median_responses_per_session}.to_xml and return} + end + end + def object_info_by_visitor_id object_type = params[:object_type] diff --git a/app/models/question.rb b/app/models/question.rb index 1a81b80..09c8dac 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -672,6 +672,17 @@ class Question < ActiveRecord::Base last_appearance end + # a response is either a vote or a skip, get the median per session + def median_responses_per_session + median(Question.connection.select_values(" + SELECT COUNT(*) total FROM ( + (SELECT voter_id vid FROM votes WHERE question_id = #{id}) + UNION ALL + (SELECT skipper_id vid FROM skips WHERE question_id = #{id}) + ) b GROUP BY b.vid ORDER BY total + "), true) || 0 + end + def upload_to_participation_ratio swp = sessions_with_participation return 0.to_f if swp == 0 diff --git a/config/routes.rb b/config/routes.rb index 84a9a74..6bed27b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,6 +8,7 @@ ActionController::Routing::Routes.draw do |map| :object_info_by_visitor_id => :get, :median_votes_per_session => :get, :vote_rate => :get, + :median_responses_per_session => :get, :upload_to_participation_ratio => :get, :export => :post} , :collection => {:all_num_votes_by_visitor_id => :get, diff --git a/spec/integration/questions_spec.rb b/spec/integration/questions_spec.rb index a02cb1a..be778f3 100644 --- a/spec/integration/questions_spec.rb +++ b/spec/integration/questions_spec.rb @@ -252,6 +252,19 @@ describe "Questions" do describe "GET 'object_info_totals_by_date'" do end + describe "GET 'median_responses_per_session'" do + before(:all) { truncate_all } + it "should return the median responses per session" do + q = Factory.create(:aoi_question, :site => @api_user) + Factory.create(:vote_new_user, :question => q) + v = Factory.create(:vote_new_user, :question => q) + Factory.create(:vote, :question => q, :voter => v.voter) + get_auth median_responses_per_session_question_path(q, :format => 'xml') + response.should be_success + response.body.should have_tag("median", :text => "1.5") + end + end + describe "GET 'upload_to_participation_ratio'" do before(:all) { truncate_all } it "should return the proper upload:participation ratio" do diff --git a/spec/models/question_spec.rb b/spec/models/question_spec.rb index 283f73d..4b1cf22 100644 --- a/spec/models/question_spec.rb +++ b/spec/models/question_spec.rb @@ -255,6 +255,35 @@ describe Question do @q = Factory.create(:aoi_question) end + it "should properly calculate median_responses_per_session with no responses" do + @q.median_responses_per_session.should == 0 + end + + it "should properly calculate median_responses_per_session with 2 sessions" do + # one session with 1 vote, one with 2 votes + Factory.create(:vote_new_user, :question => @q) + v = Factory.create(:vote_new_user, :question => @q) + Factory.create(:vote, :question => @q, :voter => v.voter) + @q.median_responses_per_session.should == 1.5 + end + + it "should properly calculate median_responses_per_session with 3 sessions" do + # one session with 3 skips, 2 votes + v = Factory.create(:vote_new_user, :question => @q) + 3.times { Factory.create(:skip, :question => @q, :skipper => v.voter) } + Factory.create(:vote, :question => @q, :voter => v.voter) + + # second session with 3 skips + v = Factory.create(:skip_new_user, :question => @q) + 2.times { Factory.create(:skip, :question => @q, :skipper => v.skipper) } + + # third session with 4 votes, 5 skips + v = Factory.create(:vote_new_user, :question => @q) + 3.times { Factory.create(:skip, :question => @q, :skipper => v.voter) } + 4.times { Factory.create(:vote, :question => @q, :voter => v.voter) } + @q.median_responses_per_session.should == 5 + end + it "should give proper stats required for idea:participation ratio" do @q.sessions_with_uploaded_ideas.should == 0 @q.sessions_with_participation.should == 0 -- libgit2 0.21.2