fix problem with locidispid being int instead of bigint; add scores; flush use of prepared statements for geoip stuff; implement connection manager update of connection and user records with location information
This commit is contained in:
parent
7de74f89af
commit
f69cdc3b4d
|
|
@ -121,3 +121,4 @@ scores_mod_connections.sql
|
|||
scores_create_schemas_and_extensions.sql
|
||||
scores_create_tables.sql
|
||||
remove_is_downloadable.sql
|
||||
scores_mod_connections2.sql
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
-- fix locidispid should be bigint
|
||||
|
||||
ALTER TABLE connections DROP COLUMN locidispid;
|
||||
ALTER TABLE connections ADD COLUMN locidispid BIGINT;
|
||||
ALTER TABLE connections ALTER COLUMN locidispid SET NOT NULL;
|
||||
CREATE INDEX connections_locidispid_ndx ON connections (locidispid);
|
||||
|
|
@ -125,6 +125,7 @@ require "jam_ruby/models/feed"
|
|||
require "jam_ruby/models/jam_isp"
|
||||
require "jam_ruby/models/geo_ip_blocks"
|
||||
require "jam_ruby/models/geo_ip_locations"
|
||||
require "jam_ruby/models/score"
|
||||
|
||||
include Jampb
|
||||
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ module JamRuby
|
|||
end
|
||||
|
||||
if ip_address
|
||||
# todo turn ip_address string into a number, then fetch the isp and block records
|
||||
# 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)
|
||||
#puts("============= JamIsp.ip_to_num returns #{addr} for #{ip_address} =============")
|
||||
|
|
@ -70,15 +70,23 @@ module JamRuby
|
|||
if block.nil? then locid = 0 else locid = block.locid end
|
||||
|
||||
location = GeoIpLocations.lookup(locid)
|
||||
if location.nil?
|
||||
locidispid = 0
|
||||
latitude = 0.0
|
||||
longitude = 0.0
|
||||
countrycode = 'US'
|
||||
region = 'TX'
|
||||
city = 'Austin'
|
||||
else
|
||||
locidispid = locid*1000000+ispid
|
||||
latitude = location.latitude
|
||||
longitude = location.longitude
|
||||
countrycode = location.countrycode
|
||||
region = location.region
|
||||
city = location.city
|
||||
end
|
||||
|
||||
locidispid = 0
|
||||
latitude = 0.0
|
||||
longitude = 0.0
|
||||
countrycode = 'US'
|
||||
region = 'TX'
|
||||
city = 'Austin'
|
||||
|
||||
# todo stuff this stuff into the connection records
|
||||
conn.update(ip_address: ip_address, locidispid: locidispid, latitude: latitude, longitude: longitude, countrycode: countrycode, region:region, city: city)
|
||||
end
|
||||
|
||||
sql =<<SQL
|
||||
|
|
@ -190,7 +198,7 @@ SQL
|
|||
ConnectionManager.active_record_transaction do |connection_manager|
|
||||
conn = connection_manager.pg_conn
|
||||
|
||||
# todo turn ip_address string into a number, then fetch the isp and block records
|
||||
# turn ip_address string into a number, then fetch the isp and block records
|
||||
|
||||
addr = JamIsp.ip_to_num(ip_address)
|
||||
#puts("============= JamIsp.ip_to_num returns #{addr} for #{ip_address} =============")
|
||||
|
|
@ -204,18 +212,26 @@ SQL
|
|||
if block.nil? then locid = 0 else locid = block.locid end
|
||||
|
||||
location = GeoIpLocations.lookup(locid)
|
||||
|
||||
locidispid = 0
|
||||
latitude = 0.0
|
||||
longitude = 0.0
|
||||
countrycode = 'US'
|
||||
region = 'TX'
|
||||
city = 'Austin'
|
||||
if location.nil?
|
||||
locidispid = 0
|
||||
latitude = 0.0
|
||||
longitude = 0.0
|
||||
countrycode = 'US'
|
||||
region = 'TX'
|
||||
city = 'Austin'
|
||||
else
|
||||
locidispid = locid*1000000+ispid
|
||||
latitude = location.latitude
|
||||
longitude = location.longitude
|
||||
countrycode = location.countrycode
|
||||
region = location.region
|
||||
city = location.city
|
||||
end
|
||||
|
||||
lock_connections(conn)
|
||||
|
||||
conn.exec("INSERT INTO connections (user_id, client_id, addr, locidispid, latitude, longitude, countrycode, region, city, aasm_state) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)",
|
||||
[user_id, client_id, addr, locidispid, latitude, longitude, countrycode, region, city, Connection::CONNECT_STATE.to_s]).clear
|
||||
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
|
||||
|
||||
# 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|
|
||||
|
|
|
|||
|
|
@ -4,17 +4,14 @@ module JamRuby
|
|||
self.table_name = 'geoipblocks'
|
||||
|
||||
def self.lookup(ipnum)
|
||||
GeoIpBlocks.select(:locid)
|
||||
.where('geom && ST_MakePoint(?, 0) AND ? BETWEEN beginip AND endip', ipnum, ipnum)
|
||||
GeoIpBlocks.where('geom && ST_MakePoint(?, 0) AND ? BETWEEN beginip AND endip', ipnum, ipnum)
|
||||
.limit(1)
|
||||
.first
|
||||
end
|
||||
|
||||
def self.make_row(beginip, endip, locid)
|
||||
c = ActiveRecord::Base.connection.raw_connection
|
||||
c.prepare('blah', 'insert into geoipblocks (beginip, endip, locid, geom) values($1::bigint, $2::bigint, $3, ST_MakeEnvelope($1::bigint, -1, $2::bigint, 1))')
|
||||
c.exec_prepared('blah', [beginip, endip, locid])
|
||||
c.exec("deallocate blah")
|
||||
def self.createx(beginip, endip, locid)
|
||||
c = connection.raw_connection
|
||||
c.exec_params('insert into geoipblocks (beginip, endip, locid, geom) values($1::bigint, $2::bigint, $3, ST_MakeEnvelope($1::bigint, -1, $2::bigint, 1))', [beginip, endip, locid])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -4,17 +4,15 @@ module JamRuby
|
|||
self.table_name = 'geoiplocations'
|
||||
|
||||
def self.lookup(locid)
|
||||
GeoIpLocations.select('locid, countrycode, region, city, latitude, longitude')
|
||||
.where(:locid => locid)
|
||||
GeoIpLocations.where(locid: locid)
|
||||
.limit(1)
|
||||
.first
|
||||
end
|
||||
|
||||
def self.make_row(locid, countrycode, region, city, postalcode, latitude, longitude, metrocode, areacode)
|
||||
c = ActiveRecord::Base.connection.raw_connection
|
||||
c.prepare('blah', 'insert into geoiplocations (locid, countrycode, region, city, postalcode, latitude, longitude, metrocode, areacode, geog) values($1, $2, $3, $4, $5, $6, $7, $8, $9, ST_SetSRID(ST_MakePoint($7, $6), 4326)::geography)')
|
||||
c.exec_prepared('blah', [locid, countrycode, region, city, postalcode, latitude, longitude, metrocode, areacode])
|
||||
c.exec("deallocate blah")
|
||||
def self.createx(locid, countrycode, region, city, postalcode, latitude, longitude, metrocode, areacode)
|
||||
c = connection.raw_connection
|
||||
c.exec_params('insert into geoiplocations (locid, countrycode, region, city, postalcode, latitude, longitude, metrocode, areacode, geog) values($1, $2, $3, $4, $5, $6, $7, $8, $9, ST_SetSRID(ST_MakePoint($7, $6), 4326)::geography)',
|
||||
[locid, countrycode, region, city, postalcode, latitude, longitude, metrocode, areacode])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -18,11 +18,10 @@ module JamRuby
|
|||
.first
|
||||
end
|
||||
|
||||
def self.make_row(beginip, endip, coid)
|
||||
c = ActiveRecord::Base.connection.raw_connection
|
||||
c.prepare('blah', 'insert into jamisp (beginip, endip, coid, geom) values($1::bigint, $2::bigint, $3, ST_MakeEnvelope($1::bigint, -1, $2::bigint, 1))')
|
||||
c.exec_prepared('blah', [beginip, endip, coid])
|
||||
c.exec("deallocate blah")
|
||||
def self.createx(beginip, endip, coid)
|
||||
c = connection.raw_connection
|
||||
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
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
require 'ipaddr'
|
||||
|
||||
module JamRuby
|
||||
class Score < ActiveRecord::Base
|
||||
|
||||
self.table_name = 'scores'
|
||||
|
||||
default_scope order('score_dt desc')
|
||||
|
||||
def self.createx(alocidispid, anodeid, aaddr, blocidispid, bnodeid, baddr, score, score_dt)
|
||||
score_dt = Time.new.utc if score_dt.nil?
|
||||
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
|
||||
|
||||
def self.deletex(alocidispid, blocidispid)
|
||||
Score.where(alocidispid: alocidispid, blocidispid: blocidispid).delete_all
|
||||
Score.where(alocidispid: blocidispid, blocidispid: alocidispid).delete_all if alocidispid != blocidispid
|
||||
end
|
||||
|
||||
def self.findx(alocidispid, blocidispid)
|
||||
s = Score.where(alocidispid: alocidispid, blocidispid: blocidispid).first
|
||||
return -1 if s.nil?
|
||||
return s.score
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -4,16 +4,22 @@ describe GeoIpBlocks do
|
|||
|
||||
before do
|
||||
GeoIpBlocks.delete_all
|
||||
GeoIpBlocks.make_row(0x00000000, 0xffffffff, 17192)
|
||||
GeoIpBlocks.createx(0x01020300, 0x010203ff, 1)
|
||||
GeoIpBlocks.createx(0x02030400, 0x020304ff, 2)
|
||||
end
|
||||
|
||||
it "count" do GeoIpBlocks.count.should == 1 end
|
||||
after do
|
||||
GeoIpBlocks.delete_all
|
||||
GeoIpBlocks.createx(0x00000000, 0xffffffff, 17192)
|
||||
end
|
||||
|
||||
it "count" do GeoIpBlocks.count.should == 2 end
|
||||
|
||||
let(:first) { GeoIpBlocks.lookup(0x01020304) }
|
||||
let(:second) { GeoIpBlocks.lookup(0x02030405) }
|
||||
let(:seventh) { GeoIpBlocks.lookup(9999999999) } # bogus
|
||||
let(:third) { GeoIpBlocks.lookup(9999999999) } # bogus
|
||||
|
||||
it "first.locid" do first.locid.should == 17192 end
|
||||
it "second.locid" do second.locid.should == 17192 end
|
||||
it "seventh" do seventh.should be_nil 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
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ describe GeoIpLocations do
|
|||
|
||||
before do
|
||||
GeoIpLocations.delete_all
|
||||
GeoIpLocations.make_row(17192, 'US', 'TX', 'Austin', '78749', 30.2076, -97.8587, 635, '512')
|
||||
GeoIpLocations.make_row(48086, 'MX', '28', 'Matamoros', '', 25.8833, -97.5000, nil, '')
|
||||
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
|
||||
|
|
|
|||
|
|
@ -4,17 +4,17 @@ describe JamIsp do
|
|||
|
||||
before do
|
||||
JamIsp.delete_all
|
||||
JamIsp.make_row(0x01020300, 0x010203ff, 1)
|
||||
JamIsp.make_row(0x02030400, 0x020304ff, 2)
|
||||
JamIsp.make_row(0x03040500, 0x030405ff, 3)
|
||||
JamIsp.make_row(0x04050600, 0x040506ff, 4)
|
||||
JamIsp.make_row(0xc0A80100, 0xc0A801ff, 5)
|
||||
JamIsp.make_row(0xfffefd00, 0xfffefdff, 6)
|
||||
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.make_row(0x00000000, 0xffffffff, 1)
|
||||
JamIsp.createx(0x00000000, 0xffffffff, 1)
|
||||
end
|
||||
|
||||
it "count" do JamIsp.count.should == 6 end
|
||||
|
|
|
|||
|
|
@ -0,0 +1,95 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Score do
|
||||
|
||||
before do
|
||||
Score.delete_all
|
||||
Score.createx(1234, 'anodeid', 0x01020304, 2345, 'bnodeid', 0x02030405, 20, nil)
|
||||
Score.createx(1234, 'anodeid', 0x01020304, 3456, 'cnodeid', 0x03040506, 30, nil)
|
||||
Score.createx(1234, 'anodeid', 0x01020304, 3456, 'cnodeid', 0x03040506, 40, Time.new.utc-3600)
|
||||
end
|
||||
|
||||
it "count" do
|
||||
Score.count.should == 6
|
||||
end
|
||||
|
||||
it 'a to b' do
|
||||
s = Score.where(alocidispid: 1234, blocidispid: 2345).limit(1).first
|
||||
s.should_not be_nil
|
||||
s.alocidispid.should == 1234
|
||||
s.anodeid.should eql('anodeid')
|
||||
s.aaddr.should == 0x01020304
|
||||
s.blocidispid.should == 2345
|
||||
s.bnodeid.should eql('bnodeid')
|
||||
s.baddr.should == 0x02030405
|
||||
s.score.should == 20
|
||||
s.scorer.should == 0
|
||||
s.score_dt.should_not be_nil
|
||||
end
|
||||
|
||||
it 'b to a' do
|
||||
s = Score.where(alocidispid: 2345, blocidispid: 1234).limit(1).first
|
||||
s.should_not be_nil
|
||||
s.alocidispid.should == 2345
|
||||
s.anodeid.should eql('bnodeid')
|
||||
s.aaddr.should == 0x02030405
|
||||
s.blocidispid.should == 1234
|
||||
s.bnodeid.should eql('anodeid')
|
||||
s.baddr.should == 0x01020304
|
||||
s.score.should == 20
|
||||
s.scorer.should == 1
|
||||
s.score_dt.should_not be_nil
|
||||
end
|
||||
|
||||
it 'a to c' do
|
||||
s = Score.where(alocidispid: 1234, blocidispid: 3456).limit(1).first
|
||||
s.should_not be_nil
|
||||
s.alocidispid.should == 1234
|
||||
s.anodeid.should eql('anodeid')
|
||||
s.aaddr.should == 0x01020304
|
||||
s.blocidispid.should == 3456
|
||||
s.bnodeid.should eql('cnodeid')
|
||||
s.baddr.should == 0x03040506
|
||||
s.score.should == 30
|
||||
s.scorer.should == 0
|
||||
s.score_dt.should_not be_nil
|
||||
end
|
||||
|
||||
it 'c to a' do
|
||||
s = Score.where(alocidispid: 3456, blocidispid: 1234).limit(1).first
|
||||
s.should_not be_nil
|
||||
s.alocidispid.should == 3456
|
||||
s.anodeid.should eql('cnodeid')
|
||||
s.aaddr.should == 0x03040506
|
||||
s.blocidispid.should == 1234
|
||||
s.bnodeid.should eql('anodeid')
|
||||
s.baddr.should == 0x01020304
|
||||
s.score.should == 30
|
||||
s.scorer.should == 1
|
||||
s.score_dt.should_not be_nil
|
||||
end
|
||||
|
||||
it 'delete a to c' do
|
||||
Score.deletex(1234, 3456)
|
||||
Score.count.should == 2
|
||||
Score.where(alocidispid: 1234, blocidispid: 3456).limit(1).first.should be_nil
|
||||
Score.where(alocidispid: 3456, blocidispid: 1234).limit(1).first.should be_nil
|
||||
Score.where(alocidispid: 1234, blocidispid: 2345).limit(1).first.should_not be_nil
|
||||
Score.where(alocidispid: 2345, blocidispid: 1234).limit(1).first.should_not be_nil
|
||||
end
|
||||
|
||||
it 'findx' do
|
||||
Score.findx(1234, 1234).should == -1
|
||||
Score.findx(1234, 2345).should == 20
|
||||
Score.findx(1234, 3456).should == 30
|
||||
|
||||
Score.findx(2345, 1234).should == 20
|
||||
Score.findx(2345, 2345).should == -1
|
||||
Score.findx(2345, 3456).should == -1
|
||||
|
||||
Score.findx(3456, 1234).should == 30
|
||||
Score.findx(3456, 2345).should == -1
|
||||
Score.findx(3456, 3456).should == -1
|
||||
end
|
||||
|
||||
end
|
||||
Loading…
Reference in New Issue