From 4a76e0c9832b84629bf2cdb17faba56f4718169e Mon Sep 17 00:00:00 2001 From: Scott Comer Date: Wed, 21 May 2014 14:05:45 -0500 Subject: [PATCH] get_work has a larger radius and excludes the current connections address --- db/manifest | 1 + db/up/update_get_work_for_larger_radius.sql | 16 +++++++ resetdb.sh | 2 +- ruby/lib/jam_ruby/models/get_work.rb | 8 ++-- ruby/lib/jam_ruby/models/score.rb | 2 + ruby/spec/jam_ruby/models/get_work_spec.rb | 4 +- web/app/controllers/api_scoring_controller.rb | 4 +- .../api_scoring_controller_spec.rb | 47 ++++++++++--------- 8 files changed, 54 insertions(+), 30 deletions(-) create mode 100644 db/up/update_get_work_for_larger_radius.sql diff --git a/db/manifest b/db/manifest index 0eed9fba8..899773187 100755 --- a/db/manifest +++ b/db/manifest @@ -155,3 +155,4 @@ session_ratings.sql scheduled_sessions.sql add_last_jam_user_fields.sql remove_lat_lng_user_fields.sql +update_get_work_for_larger_radius.sql diff --git a/db/up/update_get_work_for_larger_radius.sql b/db/up/update_get_work_for_larger_radius.sql new file mode 100644 index 000000000..f2edb5123 --- /dev/null +++ b/db/up/update_get_work_for_larger_radius.sql @@ -0,0 +1,16 @@ +DROP FUNCTION get_work (mylocidispid BIGINT); +CREATE FUNCTION get_work (mylocidispid BIGINT, myaddr BIGINT) RETURNS TABLE (client_id VARCHAR(64)) ROWS 5 VOLATILE AS $$ +BEGIN + CREATE TEMPORARY TABLE foo (locidispid BIGINT, locid INT); + INSERT INTO foo SELECT DISTINCT locidispid, locidispid/1000000 FROM connections WHERE client_type = 'client'; + DELETE FROM foo WHERE locidispid IN (SELECT DISTINCT blocidispid FROM current_scores WHERE alocidispid = mylocidispid AND (current_timestamp - score_dt) < INTERVAL '24 hours'); + DELETE FROM foo WHERE locid NOT IN (SELECT locid FROM geoiplocations WHERE geog && st_buffer((SELECT geog FROM geoiplocations WHERE locid = mylocidispid/1000000), 4023360)); + CREATE TEMPORARY TABLE bar (client_id VARCHAR(64), locidispid BIGINT, r DOUBLE PRECISION); + INSERT INTO bar SELECT l.client_id, l.locidispid, random() FROM connections l, foo f WHERE l.locidispid = f.locidispid AND l.client_type = 'client' AND addr != myaddr; + DROP TABLE foo; + DELETE FROM bar b WHERE r != (SELECT MAX(r) FROM bar b0 WHERE b0.locidispid = b.locidispid); + RETURN QUERY SELECT b.client_id FROM bar b ORDER BY r LIMIT 5; + DROP TABLE bar; + RETURN; +END; +$$ LANGUAGE plpgsql; diff --git a/resetdb.sh b/resetdb.sh index 5a06c1ff0..10b0bee88 100755 --- a/resetdb.sh +++ b/resetdb.sh @@ -4,4 +4,4 @@ dropdb jam_db_build dropdb jam_ruby_test dropdb jam_web_test dropdb jam_websocket_test -createdb -Upostgres jam +sudo su postgres -c "createdb jam" diff --git a/ruby/lib/jam_ruby/models/get_work.rb b/ruby/lib/jam_ruby/models/get_work.rb index f82ca00f4..2d52615c8 100644 --- a/ruby/lib/jam_ruby/models/get_work.rb +++ b/ruby/lib/jam_ruby/models/get_work.rb @@ -3,15 +3,15 @@ module JamRuby self.table_name = "connections" - def self.get_work(mylocidispid) - list = self.get_work_list(mylocidispid) + def self.get_work(mylocidispid, myaddr) + list = self.get_work_list(mylocidispid, myaddr) return nil if list.nil? return nil if list.length == 0 return list[0] end - def self.get_work_list(mylocidispid) - r = GetWork.select(:client_id).find_by_sql("select get_work(#{mylocidispid}) as client_id") + def self.get_work_list(mylocidispid, myaddr) + r = GetWork.select(:client_id).find_by_sql("select get_work(#{mylocidispid}, #{myaddr}) as client_id") #puts("r = #{r}") a = r.map {|i| i.client_id} #puts("a = #{a}") diff --git a/ruby/lib/jam_ruby/models/score.rb b/ruby/lib/jam_ruby/models/score.rb index 9fce6a089..962acd7c1 100644 --- a/ruby/lib/jam_ruby/models/score.rb +++ b/ruby/lib/jam_ruby/models/score.rb @@ -11,6 +11,8 @@ module JamRuby def self.createx(alocidispid, anodeid, aaddr, blocidispid, bnodeid, baddr, score, score_dt=nil) score_dt = Time.new.utc if score_dt.nil? + score = score.ceil + raise "score must be positive" if score <= 0 Score.create(alocidispid: alocidispid, anodeid: anodeid, aaddr: aaddr, blocidispid: blocidispid, bnodeid: bnodeid, baddr: baddr, score: score, scorer: 0, score_dt: score_dt) Score.create(alocidispid: blocidispid, anodeid: bnodeid, aaddr: baddr, blocidispid: alocidispid, bnodeid: anodeid, baddr: aaddr, score: score, scorer: 1, score_dt: score_dt) if alocidispid != blocidispid end diff --git a/ruby/spec/jam_ruby/models/get_work_spec.rb b/ruby/spec/jam_ruby/models/get_work_spec.rb index 9f8349b33..4e12d2ab0 100644 --- a/ruby/spec/jam_ruby/models/get_work_spec.rb +++ b/ruby/spec/jam_ruby/models/get_work_spec.rb @@ -7,13 +7,13 @@ describe GetWork do end it "get_work_1" do - x = GetWork.get_work(1) + x = GetWork.get_work(1, 0) #puts x.inspect x.should be_nil end it "get_work_list_1" do - x = GetWork.get_work_list(1) + x = GetWork.get_work_list(1, 0) #puts x.inspect x.should eql([]) end diff --git a/web/app/controllers/api_scoring_controller.rb b/web/app/controllers/api_scoring_controller.rb index 4178fac6b..fa9f042b1 100644 --- a/web/app/controllers/api_scoring_controller.rb +++ b/web/app/controllers/api_scoring_controller.rb @@ -12,7 +12,7 @@ class ApiScoringController < ApiController # if !current_user.id.eql?(conn.user.id) then render :json => {message: 'session not owned by user'}, :status => 403; return end #puts "ApiScoringController#work(#{clientid}) => locidispid #{c.locidispid}" - result_client_id = JamRuby::GetWork.get_work(conn.locidispid) + result_client_id = JamRuby::GetWork.get_work(conn.locidispid, conn.addr) #result_client_id = clientid+'peer' render :json => {:clientid => result_client_id}, :status => 200 @@ -26,7 +26,7 @@ class ApiScoringController < ApiController if conn.nil? then render :json => {message: 'session not found'}, :status => 404; return end # if !current_user.id.eql?(conn.user.id) then render :json => {message: 'session not owned by user'}, :status => 403; return end - result_client_ids = JamRuby::GetWork.get_work_list(conn.locidispid) + result_client_ids = JamRuby::GetWork.get_work_list(conn.locidispid, conn.addr) #result_client_ids = [clientid+'peer1', clientid+'peer2'] render :json => {:clientids => result_client_ids}, :status => 200 diff --git a/web/spec/controllers/api_scoring_controller_spec.rb b/web/spec/controllers/api_scoring_controller_spec.rb index 11df2af83..eae99ebd9 100644 --- a/web/spec/controllers/api_scoring_controller_spec.rb +++ b/web/spec/controllers/api_scoring_controller_spec.rb @@ -9,15 +9,15 @@ describe ApiScoringController do MARY_IP_ADDRESS = '75.92.54.210' # 1264334546, 4B.5C.36.D2 MARY_ADDR = 1264334546 - MARY_LOCIDISPID = 17192008423 + MARY_LOCIDISPID = 1807000004 MIKE_IP_ADDRESS = '173.172.108.1' # 2913758209, AD.AC.6C.01 MIKE_ADDR = 2913758209 - MIKE_LOCIDISPID = 17192043640 + MIKE_LOCIDISPID = 1539000002 JOHN_IP_ADDRESS = '255.255.1.2' # 4294902018, FF.FF.01.02 JOHN_ADDR = 4294902018 - JOHN_LOCIDISPID = 0 + JOHN_LOCIDISPID = 98765432 before do @mary = FactoryGirl.create(:user, first_name: 'mary') @@ -83,8 +83,9 @@ describe ApiScoringController do response.should be_success json = JSON.parse(response.body, :symbolize_names => true) json.length.should == 1 - json[:clientid].should_not be_nil - [@mary_client_id, @mike_client_id].should include(json[:clientid]) + clientid = json[:clientid] + clientid.should_not be_nil + [@mike_client_id].should include(clientid) end it 'try work with mike login and mike' do @@ -93,8 +94,9 @@ describe ApiScoringController do response.should be_success json = JSON.parse(response.body, :symbolize_names => true) json.length.should == 1 - json[:clientid].should_not be_nil - [@mary_client_id, @mike_client_id].should include(json[:clientid]) + clientid = json[:clientid] + clientid.should_not be_nil + [@mary_client_id].should include(clientid) end it 'try work with mary login and mike' do @@ -152,13 +154,11 @@ describe ApiScoringController do response.should be_success json = JSON.parse(response.body, :symbolize_names => true) json.length.should == 1 - json[:clientids].should_not be_nil - json[:clientids].should_receive :length - json[:clientids].length == 2 - [@mary_client_id, @mike_client_id].should include(json[:clientids][0]) - [@mary_client_id, @mike_client_id].should include(json[:clientids][1]) - json[:clientids][0].should_not eql(json[:clientids][1]) - + clientids = json[:clientids] + clientids.should_not be_nil + clientids.should respond_to :length + clientids.length.should == 1 + [@mike_client_id].should include(clientids[0]) end it 'try worklist with mike login and mike' do @@ -167,12 +167,11 @@ describe ApiScoringController do response.should be_success json = JSON.parse(response.body, :symbolize_names => true) json.length.should == 1 - json[:clientids].should_not be_nil - json[:clientids].should_receive :length - json[:clientids].length == 2 - [@mary_client_id, @mike_client_id].should include(json[:clientids][0]) - [@mary_client_id, @mike_client_id].should include(json[:clientids][1]) - json[:clientids][0].should_not eql(json[:clientids][1]) + clientids = json[:clientids] + clientids.should_not be_nil + clientids.should respond_to :length + clientids.length.should == 1 + [@mary_client_id].should include(clientids[0]) end it 'try worklist with mary login and mike' do @@ -359,12 +358,15 @@ describe ApiScoringController do json[:message].should eql('b\'s location or isp not found') end - it 'record with mary login, mary, mary_addr, mike, mike_addr, score' do + it 'record with mary login, mary, mary_addr, mike, mike_addr, score (integer)' do controller.current_user = @mary post :record, {:format => 'json', :aclientid => @mary_client_id, :aAddr => MARY_IP_ADDRESS, :bclientid => @mike_client_id, :bAddr => MIKE_IP_ADDRESS, :score => 20} response.should be_success json = JSON.parse(response.body, :symbolize_names => true) json.length.should == 0 + score = Score.findx(MARY_LOCIDISPID, MIKE_LOCIDISPID) + score.should_not be_nil + score.should eq(20) end it 'record with mary login, mary, mary_addr, mike, mike_addr, score (floating pt)' do @@ -373,6 +375,9 @@ describe ApiScoringController do response.should be_success json = JSON.parse(response.body, :symbolize_names => true) json.length.should == 0 + score = Score.findx(MARY_LOCIDISPID, MIKE_LOCIDISPID) + score.should_not be_nil + score.should eq(22) end end