diff --git a/db/manifest b/db/manifest index 00a8e8bb2..a94679598 100755 --- a/db/manifest +++ b/db/manifest @@ -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 diff --git a/db/up/scores_mod_connections2.sql b/db/up/scores_mod_connections2.sql new file mode 100644 index 000000000..016652d85 --- /dev/null +++ b/db/up/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); diff --git a/ruby/lib/jam_ruby.rb b/ruby/lib/jam_ruby.rb index 10eb7eef3..e20d94512 100755 --- a/ruby/lib/jam_ruby.rb +++ b/ruby/lib/jam_ruby.rb @@ -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 diff --git a/ruby/lib/jam_ruby/connection_manager.rb b/ruby/lib/jam_ruby/connection_manager.rb index 086b14a22..4b7bc2de6 100644 --- a/ruby/lib/jam_ruby/connection_manager.rb +++ b/ruby/lib/jam_ruby/connection_manager.rb @@ -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 =< 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 diff --git a/ruby/lib/jam_ruby/models/jam_isp.rb b/ruby/lib/jam_ruby/models/jam_isp.rb index 4157b1ce3..680cd302a 100644 --- a/ruby/lib/jam_ruby/models/jam_isp.rb +++ b/ruby/lib/jam_ruby/models/jam_isp.rb @@ -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 diff --git a/ruby/lib/jam_ruby/models/score.rb b/ruby/lib/jam_ruby/models/score.rb new file mode 100644 index 000000000..7bf736971 --- /dev/null +++ b/ruby/lib/jam_ruby/models/score.rb @@ -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 diff --git a/ruby/spec/jam_ruby/models/geo_ip_blocks_spec.rb b/ruby/spec/jam_ruby/models/geo_ip_blocks_spec.rb index dd914143c..9c826a7c5 100644 --- a/ruby/spec/jam_ruby/models/geo_ip_blocks_spec.rb +++ b/ruby/spec/jam_ruby/models/geo_ip_blocks_spec.rb @@ -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 diff --git a/ruby/spec/jam_ruby/models/geo_ip_locations_spec.rb b/ruby/spec/jam_ruby/models/geo_ip_locations_spec.rb index c6be8a6df..a9a7bc3fa 100644 --- a/ruby/spec/jam_ruby/models/geo_ip_locations_spec.rb +++ b/ruby/spec/jam_ruby/models/geo_ip_locations_spec.rb @@ -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 diff --git a/ruby/spec/jam_ruby/models/jam_isp_spec.rb b/ruby/spec/jam_ruby/models/jam_isp_spec.rb index b68b309f7..4d88f23c5 100644 --- a/ruby/spec/jam_ruby/models/jam_isp_spec.rb +++ b/ruby/spec/jam_ruby/models/jam_isp_spec.rb @@ -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 diff --git a/ruby/spec/jam_ruby/models/score_spec.rb b/ruby/spec/jam_ruby/models/score_spec.rb new file mode 100644 index 000000000..84efb6127 --- /dev/null +++ b/ruby/spec/jam_ruby/models/score_spec.rb @@ -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