diff --git a/db/manifest b/db/manifest index 58d338b79..65fbf8688 100755 --- a/db/manifest +++ b/db/manifest @@ -194,4 +194,5 @@ migrate_old_sessions.sql max_mind_releases.sql score_histories.sql update_sms_index.sql -connection_allow_null_locidispid.sql \ No newline at end of file +connection_allow_null_locidispid.sql +track_user_in_scores.sql \ No newline at end of file diff --git a/db/up/track_user_in_scores.sql b/db/up/track_user_in_scores.sql new file mode 100644 index 000000000..7be747535 --- /dev/null +++ b/db/up/track_user_in_scores.sql @@ -0,0 +1,9 @@ +ALTER TABLE scores ADD COLUMN auserid VARCHAR(64); +ALTER TABLE scores ADD COLUMN buserid VARCHAR(64); +ALTER TABLE scores ADD COLUMN alatencytestid VARCHAR(64); +ALTER TABLE scores ADD COLUMN blatencytestid VARCHAR(64); + +ALTER TABLE score_histories ADD COLUMN from_user_id VARCHAR(64); +ALTER TABLE score_histories ADD COLUMN to_user_id VARCHAR(64); +ALTER TABLE score_histories ADD COLUMN from_latency_tester_id VARCHAR(64); +ALTER TABLE score_histories ADD COLUMN to_latency_tester_id VARCHAR(64); \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/score.rb b/ruby/lib/jam_ruby/models/score.rb index dbca3cad6..7fbf595a2 100644 --- a/ruby/lib/jam_ruby/models/score.rb +++ b/ruby/lib/jam_ruby/models/score.rb @@ -7,16 +7,16 @@ module JamRuby self.table_name = 'scores' - attr_accessible :alocidispid, :anodeid, :aaddr, :blocidispid, :bnodeid, :baddr, :score, :score_dt, :scorer, :scoring_data + attr_accessible :alocidispid, :anodeid, :aaddr, :auserid, :alatencytestid, :blocidispid, :bnodeid, :baddr, :buserid, :blatencytestid, :score, :score_dt, :scorer, :scoring_data default_scope order('score_dt desc') - def self.createx(alocidispid, anodeid, aaddr, blocidispid, bnodeid, baddr, score, score_dt = Time.now, score_data = nil) + def self.createx(alocidispid, anodeid, aaddr, blocidispid, bnodeid, baddr, score, score_dt = Time.now, score_data = nil, user_info = {}) score_dt = Time.new.utc if score_dt.nil? score = score.ceil raise "score must be positive" if score <= 0 - ascore = Score.create(alocidispid: alocidispid, anodeid: anodeid, aaddr: aaddr, blocidispid: blocidispid, bnodeid: bnodeid, baddr: baddr, score: score, scorer: 0, score_dt: score_dt, scoring_data: score_data) - bscore = 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 + ascore = Score.create(alocidispid: alocidispid, anodeid: anodeid, aaddr: aaddr, auserid: user_info[:auserid], alatencytestid: user_info[:alatencytestid], blocidispid: blocidispid, bnodeid: bnodeid, baddr: baddr, buserid: user_info[:buserid], blatencytestid: user_info[:blatencytestid], score: score, scorer: 0, score_dt: score_dt, scoring_data: score_data) + bscore = Score.create(alocidispid: blocidispid, anodeid: bnodeid, aaddr: baddr, auserid: user_info[:buserid], blatencytestid: user_info[:blatencytestid], blocidispid: alocidispid, bnodeid: anodeid, baddr: aaddr, buserid: user_info[:auserid], blatencytestid: user_info[:alatencytestid], score: score, scorer: 1, score_dt: score_dt) if alocidispid != blocidispid return [ascore, bscore] end @@ -32,7 +32,20 @@ module JamRuby end def self.score_conns(c1, c2, score) - self.createx(c1.locidispid, c1.client_id, c1.addr, c2.locidispid, c2.client_id, c2.addr, score) + user_info = {} + if c1.user_id + user_info[:auserid] = c1.user_id + else + user_info[:alatencytestid] = c1.latency_tester.id + end + + if c2.user_id + user_info[:buserid] = c2.user_id + else + user_info[:blatencytestid] = c2.latency_tester.id + end + + self.createx(c1.locidispid, c1.client_id, c1.addr, c2.locidispid, c2.client_id, c2.addr, score, nil, user_info) end # locid is a geoiplocation or geoipblock locid. diff --git a/ruby/lib/jam_ruby/models/score_history.rb b/ruby/lib/jam_ruby/models/score_history.rb index 5b2786710..f69563893 100644 --- a/ruby/lib/jam_ruby/models/score_history.rb +++ b/ruby/lib/jam_ruby/models/score_history.rb @@ -12,11 +12,13 @@ module JamRuby result = connection.execute( "INSERT INTO score_histories - (from_client_id, from_addr, from_isp, from_country, from_region, from_city, from_postal, from_latitude, from_longitude, - to_client_id, to_addr, to_isp, to_country, to_region, to_city, to_postal, to_latitude, to_longitude, + (from_client_id, from_user_id, from_latency_tester_id, from_addr, from_isp, from_country, from_region, from_city, from_postal, from_latitude, from_longitude, + to_client_id, to_user_id, to_latency_tester_id, to_addr, to_isp, to_country, to_region, to_city, to_postal, to_latitude, to_longitude, score, score_dt, scoring_data) SELECT s.anodeid AS from_client_id, + s.auserid AS from_user_id, + s.alatencytestid AS from_latency_tester_id, s.aaddr AS from_addr, x.company AS from_isp, a.countrycode AS from_country, @@ -26,6 +28,8 @@ module JamRuby a.latitude AS from_latitude, a.longitude AS from_longitude, s.bnodeid AS to_client_id, + s.buserid AS to_user_id, + s.blatencytestid AS to_latency_tester_id, s.baddr AS to_addr, y.company AS to_isp, b.countrycode AS to_country, diff --git a/ruby/lib/jam_ruby/models/user.rb b/ruby/lib/jam_ruby/models/user.rb index 2c046df78..5f241d71b 100644 --- a/ruby/lib/jam_ruby/models/user.rb +++ b/ruby/lib/jam_ruby/models/user.rb @@ -1006,7 +1006,6 @@ module JamRuby self.last_jam_updated_reason = reason self.last_jam_updated_at = Time.now unless self.save - puts "OK?" @@log.warn("unable to update user #{self} with last_jam_reason #{reason}. errors: #{self.errors.inspect}") end end diff --git a/ruby/spec/jam_ruby/models/score_history_spec.rb b/ruby/spec/jam_ruby/models/score_history_spec.rb index aa07ac196..67fcdae11 100644 --- a/ruby/spec/jam_ruby/models/score_history_spec.rb +++ b/ruby/spec/jam_ruby/models/score_history_spec.rb @@ -21,7 +21,10 @@ describe ScoreHistory do Country.count.should == 1 JamCompany.count.should == 4 - score1, score2 = create_score(austin, dallas) + user1 = FactoryGirl.create(:user) + user2 = FactoryGirl.create(:user) + + score1, score2 = create_score(austin, dallas, { auserid: user1.id, buserid: user2.id } ) Score.count.should == 2 Score.connection.execute("UPDATE scores SET created_at = TIMESTAMP '#{1.hour.ago}'").check @@ -32,6 +35,8 @@ describe ScoreHistory do score_history = ScoreHistory.first score_history.from_client_id.should == score1.anodeid + score_history.from_user_id.should == score1.auserid + score_history.from_latency_tester_id.should == score1.alatencytestid score_history.from_addr.should == score1.aaddr score_history.from_isp.should == austin[:jamisp].jam_company.company score_history.from_country.should == austin[:geoiplocation].countrycode @@ -40,6 +45,17 @@ describe ScoreHistory do score_history.from_postal == austin[:geoiplocation].postalcode score_history.from_latitude == austin[:geoiplocation].latitude score_history.from_longitude == austin[:geoiplocation].longitude + score_history.to_client_id.should == score1.bnodeid + score_history.to_user_id.should == score1.buserid + score_history.to_latency_tester_id.should == score1.blatencytestid + score_history.to_addr.should == score1.baddr + score_history.to_isp.should == dallas[:jamisp].jam_company.company + score_history.to_country.should == dallas[:geoiplocation].countrycode + score_history.to_region.should == dallas[:geoiplocation].region + score_history.to_city.should == dallas[:geoiplocation].city + score_history.to_postal == dallas[:geoiplocation].postalcode + score_history.to_latitude == dallas[:geoiplocation].latitude + score_history.to_longitude == dallas[:geoiplocation].longitude end it "ignores recent scores" do diff --git a/ruby/spec/jam_ruby/models/score_spec.rb b/ruby/spec/jam_ruby/models/score_spec.rb index 65e806d6c..c360cf881 100644 --- a/ruby/spec/jam_ruby/models/score_spec.rb +++ b/ruby/spec/jam_ruby/models/score_spec.rb @@ -2,8 +2,15 @@ require 'spec_helper' describe Score do + let(:user1) { FactoryGirl.create(:user) } + let(:user2) { FactoryGirl.create(:user) } + let(:score_with_user) { s1, s2 = Score.createx(1234, 'anodeid', 0x01020304, 2345, 'bnodeid', 0x02030405, 20, nil, 'foo', { auserid: user1.id, buserid: user2.id }); s1 } + + let(:latency_tester1) { FactoryGirl.create(:latency_tester) } + let(:latency_tester2) { FactoryGirl.create(:latency_tester) } + let(:score_with_latency_tester) { s1, s2 = Score.createx(1234, 'anodeid', 0x01020304, 2345, 'bnodeid', 0x02030405, 20, nil, 'foo', { alatencytestid: latency_tester1.id, blatencytestid: latency_tester2.id}); s1 } + before do - Score.delete_all Score.createx(1234, 'anodeid', 0x01020304, 2345, 'bnodeid', 0x02030405, 20, nil, 'foo') Score.createx(1234, 'anodeid', 0x01020304, 3456, 'cnodeid', 0x03040506, 30, nil) Score.createx(1234, 'anodeid', 0x01020304, 3456, 'cnodeid', 0x03040506, 40, Time.new.utc-3600) @@ -123,4 +130,23 @@ describe Score do Score.findx(7, 6).should == 23 end + describe "createx" do + it "creates with user info" do + score_with_user.touch + score_with_user.auserid.should == user1.id + score_with_user.buserid.should == user2.id + score_with_user.alatencytestid.should be_nil + score_with_user.blatencytestid.should be_nil + end + + it "creates with latency-tester info" do + score_with_latency_tester.touch + score_with_latency_tester.auserid.should be_nil + score_with_latency_tester.buserid.should be_nil + score_with_latency_tester.alatencytestid.should == latency_tester1.id + score_with_latency_tester.blatencytestid.should == latency_tester2.id + end + end + + end diff --git a/ruby/spec/support/maxmind.rb b/ruby/spec/support/maxmind.rb index 4ac3166e2..d48a550e5 100644 --- a/ruby/spec/support/maxmind.rb +++ b/ruby/spec/support/maxmind.rb @@ -177,11 +177,11 @@ end # attempts to make the creation of a score more straightforward. # a_geoip and b_geoip are hashes with keys jamisp and geoiplocation (like those created by austin_geoip and dallas_geoip) -def create_score(a_geoip, b_geoip, a_addr = a_geoip[:jamisp].beginip, b_addr = b_geoip[:jamisp].beginip, - a_client_id = 'a_client_id', b_client_id = 'b_client_id', score = 10, score_dt = Time.now, score_data = nil) +def create_score(a_geoip, b_geoip, user_info = {}, a_addr = a_geoip[:jamisp].beginip, b_addr = b_geoip[:jamisp].beginip, + a_client_id = 'a_client_id', b_client_id = 'b_client_id', score = 10, score_dt = Time.now, score_data = nil) Score.createx(Score.create_locidispid(a_geoip[:geoiplocation], a_geoip[:jamisp]), a_client_id, a_addr, Score.create_locidispid(b_geoip[:geoiplocation], b_geoip[:jamisp]), b_client_id, b_addr, - score, score_dt, score_data) + score, score_dt, score_data, user_info) end def locidispid_from_ip(ip_address) diff --git a/web/app/controllers/api_scoring_controller.rb b/web/app/controllers/api_scoring_controller.rb index 3a577a95e..34420e355 100644 --- a/web/app/controllers/api_scoring_controller.rb +++ b/web/app/controllers/api_scoring_controller.rb @@ -82,7 +82,21 @@ class ApiScoringController < ApiController if bisp.nil? or bloc.nil? then render :json => {message: 'b\'s location or isp not found'}, :status => 404; return end blocidispid = bloc.locid*1000000+bisp.coid - JamRuby::Score.createx(alocidispid, aclientid, aAddr, blocidispid, bclientid, bAddr, score.ceil, nil, score_data) + + user_info = {} + if aconn.user_id + user_info[:auserid] = aconn.user_id + else + user_info[:alatencytestid] = aconn.latency_tester.id + end + + if bconn.user_id + user_info[:buserid] = bconn.user_id + else + user_info[:blatencytestid] = bconn.latency_tester.id + end + + JamRuby::Score.createx(alocidispid, aclientid, aAddr, blocidispid, bclientid, bAddr, score.ceil, nil, score_data, user_info) render :json => {}, :status => 200 end diff --git a/web/spec/controllers/api_scoring_controller_spec.rb b/web/spec/controllers/api_scoring_controller_spec.rb index eae99ebd9..0265a4539 100644 --- a/web/spec/controllers/api_scoring_controller_spec.rb +++ b/web/spec/controllers/api_scoring_controller_spec.rb @@ -367,6 +367,19 @@ describe ApiScoringController do score = Score.findx(MARY_LOCIDISPID, MIKE_LOCIDISPID) score.should_not be_nil score.should eq(20) + score = Score.where(:scorer => 0).first # there should only be 2 scores in the db; so grab the scorer = 0 one + score.auserid.should == @mary.id + score.aaddr.should == 1264334546 + score.anodeid.should == @mary_client_id + score.alocidispid.should == MARY_LOCIDISPID + score.alatencytestid.should be_nil + score.buserid.should == @mike.id + score.baddr.should == 2913758209 + score.bnodeid.should == @mike_client_id + score.blocidispid.should == MIKE_LOCIDISPID + score.blatencytestid.should be_nil + score.score.should == 20 + score.score_dt.should_not be_nil end it 'record with mary login, mary, mary_addr, mike, mike_addr, score (floating pt)' do diff --git a/web/spec/support/maxmind.rb b/web/spec/support/maxmind.rb index 89df561a9..ccb565277 100644 --- a/web/spec/support/maxmind.rb +++ b/web/spec/support/maxmind.rb @@ -177,11 +177,11 @@ end # attempts to make the creation of a score more straightforward. # a_geoip and b_geoip are hashes with keys jamisp and geoiplocation (like those created by austin_geoip and dallas_geoip) -def create_score(a_geoip, b_geoip, a_addr = a_geoip[:jamisp].beginip, b_addr = b_geoip[:jamisp].beginip, +def create_score(a_geoip, b_geoip, user_info = {}, a_addr = a_geoip[:jamisp].beginip, b_addr = b_geoip[:jamisp].beginip, a_client_id = 'a_client_id', b_client_id = 'b_client_id', score = 10, score_dt = Time.now, score_data = nil) Score.createx(Score.create_locidispid(a_geoip[:geoiplocation], a_geoip[:jamisp]), a_client_id, a_addr, Score.create_locidispid(b_geoip[:geoiplocation], b_geoip[:jamisp]), b_client_id, b_addr, - score, score_dt, score_data) + score, score_dt, score_data, user_info) end def locidispid_from_ip(ip_address)