update user with connection address on login or reconnect; make the better test data useable; fix up tests for geo stuff

This commit is contained in:
Scott Comer 2014-03-02 23:16:48 -06:00
parent b801902ea4
commit 1e8d2bb724
8 changed files with 116 additions and 73 deletions

4
.gitignore vendored
View File

@ -5,4 +5,6 @@
*/vendor/cache
HTML
.DS_Store
coverage/
coverage
update2

View File

@ -44,7 +44,7 @@ module JamRuby
end
# reclaim the existing connection, if ip_address is not nil then perhaps a new address as well
def reconnect(conn, reconnect_music_session_id, ip_address)
def reconnect(conn, reconnect_music_session_id, ip_address, &blk)
music_session_id = nil
reconnected = false
@ -55,7 +55,7 @@ module JamRuby
music_session_id_expression = "(CASE WHEN music_session_id='#{reconnect_music_session_id}' THEN music_session_id ELSE NULL END)"
end
if ip_address
if ip_address and !ip_address.eql?(conn.ip_address)
# turn ip_address string into a number, then fetch the isp and block records and update location info
addr = JamIsp.ip_to_num(ip_address)
@ -71,6 +71,7 @@ module JamRuby
location = GeoIpLocations.lookup(locid)
if location.nil?
# todo what's a better default location?
locidispid = 0
latitude = 0.0
longitude = 0.0
@ -87,6 +88,7 @@ module JamRuby
end
conn.ip_address = ip_address
conn.addr = addr
conn.locidispid = locidispid
conn.latitude = latitude
conn.longitude = longitude
@ -94,6 +96,9 @@ module JamRuby
conn.region = region
conn.city = city
conn.save!(validate: false)
# we're passing all this stuff so that the user record might be updated as well...
blk.call(addr, locidispid, latitude, longitude, countrycode, region, city) unless blk.nil?
end
sql =<<SQL
@ -220,6 +225,7 @@ SQL
location = GeoIpLocations.lookup(locid)
if location.nil?
# todo what's a better default location?
locidispid = 0
latitude = 0.0
longitude = 0.0
@ -237,13 +243,14 @@ SQL
lock_connections(conn)
conn.exec("INSERT INTO connections (user_id, client_id, addr, locidispid, latitude, longitude, countrycode, region, city, aasm_state, ip_address) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)",
[user_id, client_id, addr, locidispid, latitude, longitude, countrycode, region, city, Connection::CONNECT_STATE.to_s, ip_address]).clear
conn.exec("INSERT INTO connections (user_id, client_id, ip_address, addr, locidispid, latitude, longitude, countrycode, region, city, aasm_state) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)",
[user_id, client_id, ip_address, addr, locidispid, latitude, longitude, countrycode, region, city, Connection::CONNECT_STATE.to_s]).clear
# we just created a new connection-if this is the first time the user has shown up, we need to send out a message to his friends
conn.exec("SELECT count(user_id) FROM connections WHERE user_id = $1", [user_id]) do |result|
count = result.getvalue(0, 0) .to_i
blk.call(conn, count) unless blk.nil?
# we're passing all this stuff so that the user record might be updated as well...
blk.call(conn, count, addr, locidispid, latitude, longitude, countrycode, region, city) unless blk.nil?
end
return count
end

View File

@ -27,5 +27,13 @@ module JamRuby
c.exec_params('insert into jamisp (beginip, endip, coid, geom) values($1::bigint, $2::bigint, $3, ST_MakeEnvelope($1::bigint, -1, $2::bigint, 1))',
[beginip, endip, coid])
end
def self_delete()
raise "mother trucker"
end
def self.delete_all()
raise "mother trucker"
end
end
end

View File

@ -2,24 +2,32 @@ require 'spec_helper'
describe GeoIpBlocks do
before do
GeoIpBlocks.delete_all
GeoIpBlocks.createx(0x01020300, 0x010203ff, 1)
GeoIpBlocks.createx(0x02030400, 0x020304ff, 2)
end
#before do
#GeoIpBlocks.delete_all
#GeoIpBlocks.createx(0x01020300, 0x010203ff, 1)
#GeoIpBlocks.createx(0x02030400, 0x020304ff, 2)
#end
after do
GeoIpBlocks.delete_all
GeoIpBlocks.createx(0x00000000, 0xffffffff, 17192)
end
#after do
#GeoIpBlocks.delete_all
#GeoIpBlocks.createx(0x00000000, 0xffffffff, 17192)
#end
it "count" do GeoIpBlocks.count.should == 2 end
it "count" do GeoIpBlocks.count.should == 16 end
let(:first) { GeoIpBlocks.lookup(0x01020304) }
let(:second) { GeoIpBlocks.lookup(0x02030405) }
let(:third) { GeoIpBlocks.lookup(9999999999) } # bogus
let(:first) { GeoIpBlocks.lookup(0x01020304) } # 17192
let(:second) { GeoIpBlocks.lookup(0x12030405) } # 667
let(:third) { GeoIpBlocks.lookup(0xffff0001) } # bogus
it "first" do first.should_not be_nil end
it "first.beginip" do first.beginip.should == 0x00000000 end
it "first.endip" do first.endip.should == 0x0fffffff end
it "first.locid" do first.locid.should == 17192 end
it "second" do second.should_not be_nil end
it "second.beginip" do second.beginip.should == 0x10000000 end
it "second.endip" do second.endip.should == 0x1fffffff end
it "second.locid" do second.locid.should == 667 end
it "first.locid" do first.locid.should == 1 end
it "second.locid" do second.locid.should == 2 end
it "third" do third.should be_nil end
end

View File

@ -3,33 +3,35 @@ require 'spec_helper'
describe GeoIpLocations do
before do
GeoIpLocations.delete_all
GeoIpLocations.createx(17192, 'US', 'TX', 'Austin', '78749', 30.2076, -97.8587, 635, '512')
GeoIpLocations.createx(48086, 'MX', '28', 'Matamoros', '', 25.8833, -97.5000, nil, '')
#GeoIpLocations.delete_all
#GeoIpLocations.createx(17192, 'US', 'TX', 'Austin', '78749', 30.2076, -97.8587, 635, '512')
#GeoIpLocations.createx(48086, 'MX', '28', 'Matamoros', '', 25.8833, -97.5000, nil, '')
end
it "count" do GeoIpLocations.count.should == 2 end
it "count" do GeoIpLocations.count.should == 16 end
let(:first) { GeoIpLocations.lookup(17192) }
let(:second) { GeoIpLocations.lookup(48086) }
let(:third) { GeoIpLocations.lookup(999999) } # bogus
let(:first) { GeoIpLocations.lookup(17192) }
let(:second) { GeoIpLocations.lookup(1539) }
let(:third) { GeoIpLocations.lookup(999999) } # bogus
it "first" do
first.locid.should == 17192
first.countrycode.should eql('US')
first.region.should eql('TX')
first.city.should eql('Austin')
first.latitude.should == 30.2076
first.longitude.should == -97.8587
describe "first" do
it "first" do first.should_not be_nil end
it "first.locid" do first.locid.should == 17192 end
it "first.countrycode" do first.countrycode.should eql('US') end
it "first.region" do first.region.should eql('TX') end
it "first.city" do first.city.should eql('Austin') end
it "first.latitude" do first.latitude.should == 30.2076 end
it "first.longitude" do first.longitude.should == -97.8587 end
end
it "second" do
second.locid.should == 48086
second.countrycode.should eql('MX')
second.region.should eql('28')
second.city.should eql('Matamoros')
second.latitude.should == 25.8833
second.longitude.should == -97.5000
describe "second" do
it "second" do first.should_not be_nil end
it "second.locid" do second.locid.should == 1539 end
it "second.countrycode" do second.countrycode.should eql('US') end
it "second.region" do second.region.should eql('WA') end
it "second.city" do second.city.should eql('Seattle') end
it "second.latitude" do second.latitude.should == 47.6103 end
it "second.longitude" do second.longitude.should == -122.3341 end
end
it "third" do third.should be_nil end

View File

@ -2,22 +2,22 @@ require 'spec_helper'
describe JamIsp do
before do
JamIsp.delete_all
JamIsp.createx(0x01020300, 0x010203ff, 1)
JamIsp.createx(0x02030400, 0x020304ff, 2)
JamIsp.createx(0x03040500, 0x030405ff, 3)
JamIsp.createx(0x04050600, 0x040506ff, 4)
JamIsp.createx(0xc0A80100, 0xc0A801ff, 5)
JamIsp.createx(0xfffefd00, 0xfffefdff, 6)
end
#before do
# JamIsp.delete_all
# JamIsp.createx(0x01020300, 0x010203ff, 1)
# JamIsp.createx(0x02030400, 0x020304ff, 2)
# JamIsp.createx(0x03040500, 0x030405ff, 3)
# JamIsp.createx(0x04050600, 0x040506ff, 4)
# JamIsp.createx(0xc0A80100, 0xc0A801ff, 5)
# JamIsp.createx(0xfffefd00, 0xfffefdff, 6)
#end
after do
JamIsp.delete_all
JamIsp.createx(0x00000000, 0xffffffff, 1)
end
#after do
# JamIsp.delete_all
# JamIsp.createx(0x00000000, 0xffffffff, 1)
#end
it "count" do JamIsp.count.should == 6 end
it "count" do JamIsp.count.should == 16 end
let(:first_addr) { JamIsp.ip_to_num('1.2.3.4') }
let(:second_addr) { JamIsp.ip_to_num('2.3.4.5') }
@ -34,18 +34,12 @@ describe JamIsp do
it "sixth_addr" do sixth_addr.should == 0xfffefdfc end
let(:first) { JamIsp.lookup(0x01020304) }
let(:second) { JamIsp.lookup(0x02030405) }
let(:third) { JamIsp.lookup(0x03040506) }
let(:fourth) { JamIsp.lookup(0x04050607) }
let(:fifth) { JamIsp.lookup(0xc0A8016b) }
let(:sixth) { JamIsp.lookup(0xfffefdfc) }
let(:seventh) { JamIsp.lookup(0) } # bogus
let(:second) { JamIsp.lookup(0x12030405) }
let(:third) { JamIsp.lookup(0x43040506) }
let(:seventh) { JamIsp.lookup(0xffff0123) } # bogus
it "first.coid" do first.coid.should == 1 end
it "second.coid" do second.coid.should == 2 end
it "third.coid" do third.coid.should == 3 end
it "fourth.coid" do fourth.coid.should == 4 end
it "fifth.coid" do fifth.coid.should == 5 end
it "sixth.coid" do sixth.coid.should == 6 end
it "first.coid" do first.coid.should == 2 end
it "second.coid" do second.coid.should == 3 end
it "third.coid" do third.coid.should == 4 end
it "seventh" do seventh.should be_nil end
end

View File

@ -24,7 +24,7 @@ require 'database_cleaner'
require 'factories'
# uncomment this to see active record logs
# ActiveRecord::Base.logger = Logger.new(STDOUT) if defined?(ActiveRecord::Base)
#ActiveRecord::Base.logger = Logger.new(STDOUT) if defined?(ActiveRecord::Base)
include JamRuby
@ -74,8 +74,8 @@ Spork.prefork do
config.filter_run_excluding aws: true unless run_tests? :aws
config.before(:suite) do
DatabaseCleaner.strategy = :truncation, {:except => %w[instruments genres icecast_server_groups] }
DatabaseCleaner.clean_with(:truncation, {:except => %w[instruments genres icecast_server_groups] })
DatabaseCleaner.strategy = :truncation, {:except => %w[instruments genres icecast_server_groups jamcompany jamisp geoipblocks geoipisp geoiplocations] }
DatabaseCleaner.clean_with(:truncation, {:except => %w[instruments genres icecast_server_groups jamcompany jamisp geoipblocks geoipisp geoiplocations] })
end
config.before(:each) do

View File

@ -485,7 +485,19 @@ module JamWebsockets
context = nil
ConnectionManager.active_record_transaction do |connection_manager|
music_session_id, reconnected = connection_manager.reconnect(connection, reconnect_music_session_id, remote_ip)
music_session_id, reconnected = connection_manager.reconnect(connection, reconnect_music_session_id, remote_ip) do |addr, locidispid, latitude, longitude, countrycode, region, city|
# update user
user.ip_address = remote_ip
user.addr = addr
user.locidispid = locidispid
user.lat = latitude
user.lng = longitude
user.country = countrycode
user.state = region
user.city = city
user.save!(validate: false)
end
context = @client_lookup[client_id]
if music_session_id.nil?
# if this is a reclaim of a connection, but music_session_id comes back null, then we need to check if this connection was IN a music session before.
@ -522,7 +534,17 @@ module JamWebsockets
unless connection
# log this connection in the database
ConnectionManager.active_record_transaction do |connection_manager|
connection_manager.create_connection(user.id, client.client_id, remote_ip) do |conn, count|
connection_manager.create_connection(user.id, client.client_id, remote_ip) do |conn, count, addr, locidispid, latitude, longitude, countrycode, region, city|
# first update the user record with the current location
user.addr = addr
user.locidispid = locidispid
user.lat = latitude
user.lng = longitude
user.country = countrycode
user.state = region
user.city = city;
user.save!
if count == 1
Notification.send_friend_update(user.id, true, conn)
end