2014-02-23 04:00:32 +00:00
require 'ipaddr'
2020-09-01 18:33:04 +00:00
= begin
2014-02-23 04:00:32 +00:00
module JamRuby
class JamIsp < ActiveRecord :: Base
2014-07-20 02:11:16 +00:00
# index names created on the copied table used during import.
# they do not exist except during import
GEOIPISP_INDEX_NAME = 'geoipisp_company_ndx'
COPIED_GEOIPISP_INDEX_NAME = 'geoipisp_copied_company_ndx'
JAMCOMPANY_UNIQUE_INDEX = 'jamcompany_company_ndx'
COPIED_JAMCOMPANY_UNIQUE_INDEX = 'jamcompany_copied_company_ndx'
JAMCOMPANY_PRIMARY_KEY_NAME = 'jamcompany_pkey'
COPIED_JAMCOMPANY_PRIMARY_KEY_NAME = 'jamcompany_copied_pkey'
COPIED_JAMCOMPANY_COID_SEQUENCE = 'jamcompany_copied_coid_seq'
JAMCOMPANY_COID_SEQUENCE = 'jamcompany_coid_seq'
JAMISP_GEOM_INDEX_NAME = 'jamisp_geom_gix'
COPIED_JAMISP_GEOM_INDEX_NAME = 'jamisp_copied_geom_gix'
JAMISP_COID_INDEX_NAME = 'jamisp_coid_ndx'
COPIED_JAMISP_COID_INDEX_NAME = 'jamisp_copied_coid_ndx'
2014-07-21 21:42:30 +00:00
COMPANY_TABLE = 'jamcompany'
GEOIPISP_TABLE = 'geoipisp'
2014-07-20 02:11:16 +00:00
@@log = Logging . logger [ JamIsp ]
2014-07-21 21:42:30 +00:00
belongs_to :jam_company , class_name : 'JamRuby::JamCompany' , foreign_key : 'coid'
2014-02-23 04:00:32 +00:00
self . table_name = 'jamisp'
def self . ip_to_num ( ip_addr )
2014-03-02 02:36:51 +00:00
begin
i = IPAddr . new ( ip_addr )
return i . to_i if i . ipv4?
nil
rescue IPAddr :: InvalidAddressError
nil
end
2014-02-23 04:00:32 +00:00
end
def self . lookup ( ipnum )
JamIsp . select ( :coid )
. where ( 'geom && ST_MakePoint(?, 0) AND ? BETWEEN beginip AND endip' , ipnum , ipnum )
. limit ( 1 )
. first
end
2014-02-24 21:19:46 +00:00
def self . createx ( beginip , endip , coid )
c = connection . raw_connection
2014-03-09 22:35:12 +00:00
c . exec_params ( " insert into #{ self . table_name } (beginip, endip, coid, geom) values($1::bigint, $2::bigint, $3, ST_MakeEnvelope($1::bigint, -1, $2::bigint, 1)) " ,
2014-07-20 02:11:16 +00:00
[ beginip , endip , coid ] )
2014-02-23 04:00:32 +00:00
end
2014-03-03 05:16:48 +00:00
def self_delete ( )
raise " mother trucker "
end
def self . delete_all ( )
raise " mother trucker "
end
2014-03-09 06:22:51 +00:00
2014-07-20 02:11:16 +00:00
def self . import_from_max_mind ( options )
file = options [ :file ]
use_copy = options [ :use_copy ]
2014-03-09 22:35:12 +00:00
# File Geo-124
# Format:
# startIpNum,endIpNum,isp
2014-07-20 02:11:16 +00:00
start = Time . now
copied_table_name = Database . copy_table ( GEOIPISP_TABLE )
copied_jamcompany_table_name = Database . copy_table ( COMPANY_TABLE )
copied_jamisp_table_name = Database . copy_table ( self . table_name )
if use_copy
Database . copy ( copied_table_name , file )
else
2014-03-09 22:35:12 +00:00
File . open ( file , 'r:ISO-8859-1' ) do | io |
#s = io.gets.strip # eat the copyright line. gah, why do they have that in their file??
#unless s.eql? 'Copyright (c) 2012 MaxMind LLC. All Rights Reserved.'
# puts s
# puts 'Copyright (c) 2012 MaxMind LLC. All Rights Reserved.'
# raise 'file does not start with expected copyright (line 1): Copyright (c) 2012 MaxMind LLC. All Rights Reserved.'
#end
#s = io.gets.strip # eat the headers line
#unless s.eql? 'locId,country,region,city,postalCode,latitude,longitude,metroCode,areaCode'
# puts s
# puts 'locId,country,region,city,postalCode,latitude,longitude,metroCode,areaCode'
# raise 'file does not start with expected header (line 2): locId,country,region,city,postalCode,latitude,longitude,metroCode,areaCode'
#end
saved_level = ActiveRecord :: Base . logger ? ActiveRecord :: Base . logger . level : 0
count = 0
2014-07-20 02:11:16 +00:00
stmt = " INSERT INTO #{ copied_table_name } (beginip, endip, company) VALUES "
2014-03-09 22:35:12 +00:00
vals = ''
sep = ''
i = 0
n = 20
csv = :: CSV . new ( io , { encoding : 'ISO-8859-1' , headers : false } )
csv . each do | row |
raise " file does not have expected number of columns (3): #{ row . length } " unless row . length == 3
2014-07-20 02:11:16 +00:00
beginip = ip_address_to_int ( strip_quotes ( row [ 0 ] ) )
endip = ip_address_to_int ( strip_quotes ( row [ 1 ] ) )
2014-03-09 22:35:12 +00:00
company = row [ 2 ]
2016-07-17 15:16:27 +00:00
vals = vals + sep + " ( #{ beginip } , #{ endip } , #{ quote_value ( company , nil ) } ) "
2014-03-09 22:35:12 +00:00
sep = ','
i += 1
if count == 0 or i > = n then
GeoIpLocations . connection . execute stmt + vals
count += i
vals = ''
sep = ''
i = 0
if ActiveRecord :: Base . logger and ActiveRecord :: Base . logger . level > 1 then
2014-07-20 02:11:16 +00:00
ActiveRecord :: Base . logger . debug " ... logging inserts into #{ copied_table_name } suspended ... "
2014-03-09 22:35:12 +00:00
ActiveRecord :: Base . logger . level = 1
end
if ActiveRecord :: Base . logger and count % 10000 < n then
ActiveRecord :: Base . logger . level = saved_level
2014-07-20 02:11:16 +00:00
ActiveRecord :: Base . logger . debug " ... inserted #{ count } into #{ copied_table_name } ... "
2014-03-09 22:35:12 +00:00
ActiveRecord :: Base . logger . level = 1
end
end
end
if i > 0 then
GeoIpLocations . connection . execute stmt + vals
count += i
end
if ActiveRecord :: Base . logger then
ActiveRecord :: Base . logger . level = saved_level
2014-07-20 02:11:16 +00:00
ActiveRecord :: Base . logger . debug " loaded #{ count } records into #{ copied_table_name } "
2014-03-09 22:35:12 +00:00
end
2014-07-20 02:11:16 +00:00
end
end
2014-03-09 22:35:12 +00:00
2014-07-20 02:11:16 +00:00
# add index to copied geoipisp table
GeoIpLocations . connection . execute ( " CREATE INDEX #{ COPIED_GEOIPISP_INDEX_NAME } ON #{ copied_table_name } (company) " ) . check
2014-03-09 22:35:12 +00:00
2014-07-20 02:11:16 +00:00
# add sequence to copied_jamcompany table
GeoIpLocations . connection . execute ( " ALTER TABLE #{ copied_jamcompany_table_name } ALTER COLUMN coid DROP DEFAULT " ) . check
GeoIpLocations . connection . execute ( " CREATE SEQUENCE #{ COPIED_JAMCOMPANY_COID_SEQUENCE } START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1 " ) . check
GeoIpLocations . connection . execute ( " ALTER SEQUENCE #{ COPIED_JAMCOMPANY_COID_SEQUENCE } OWNED BY #{ copied_jamcompany_table_name } .coid " ) . check
GeoIpLocations . connection . execute ( " ALTER TABLE ONLY #{ copied_jamcompany_table_name } ALTER COLUMN coid SET DEFAULT nextval(' #{ COPIED_JAMCOMPANY_COID_SEQUENCE } '::regclass) " ) . check
2014-03-09 22:35:12 +00:00
2014-07-20 02:11:16 +00:00
sts = GeoIpLocations . connection . execute ( " INSERT INTO #{ copied_jamcompany_table_name } (company) SELECT DISTINCT company FROM #{ copied_table_name } ORDER BY company " ) . check
ActiveRecord :: Base . logger . debug " INSERT INTO #{ copied_table_name } returned sts #{ sts . cmd_status } " if ActiveRecord :: Base . logger
sts . check
2014-03-09 22:35:12 +00:00
2014-07-20 02:11:16 +00:00
# add unique index to copied jamcompany table
GeoIpLocations . connection . execute ( " CREATE UNIQUE INDEX #{ COPIED_JAMCOMPANY_UNIQUE_INDEX } ON #{ copied_jamcompany_table_name } (company) " ) . check
# add primary index to copied jamcompany table
GeoIpLocations . connection . execute ( " CREATE UNIQUE INDEX #{ COPIED_JAMCOMPANY_PRIMARY_KEY_NAME } ON #{ copied_jamcompany_table_name } USING btree (coid) " ) . check
GeoIpLocations . connection . execute ( " ALTER TABLE #{ copied_jamcompany_table_name } ADD CONSTRAINT #{ COPIED_JAMCOMPANY_PRIMARY_KEY_NAME } PRIMARY KEY USING INDEX #{ COPIED_JAMCOMPANY_PRIMARY_KEY_NAME } " ) . check
2014-03-09 22:35:12 +00:00
2014-07-20 02:11:16 +00:00
sts = GeoIpLocations . connection . execute " INSERT INTO #{ copied_jamisp_table_name } (beginip, endip, coid) SELECT x.beginip, x.endip, y.coid FROM #{ copied_table_name } x, #{ copied_jamcompany_table_name } y WHERE x.company = y.company "
ActiveRecord :: Base . logger . debug " INSERT INTO #{ copied_jamisp_table_name } returned sts #{ sts . cmd_status } " if ActiveRecord :: Base . logger
sts . check
2014-03-09 22:35:12 +00:00
2014-07-20 02:11:16 +00:00
sts = GeoIpLocations . connection . execute " ALTER TABLE #{ copied_jamisp_table_name } DROP COLUMN geom "
ActiveRecord :: Base . logger . debug " DROP COLUMN geom returned sts #{ sts . cmd_status } " if ActiveRecord :: Base . logger
#sts.check [we don't care]
2014-03-09 22:35:12 +00:00
2014-07-20 02:11:16 +00:00
sts = GeoIpLocations . connection . execute " ALTER TABLE #{ copied_jamisp_table_name } ADD COLUMN geom geometry(polygon) "
ActiveRecord :: Base . logger . debug " ADD COLUMN geom returned sts #{ sts . cmd_status } " if ActiveRecord :: Base . logger
sts . check
2014-03-09 22:35:12 +00:00
2014-07-20 02:11:16 +00:00
sts = GeoIpLocations . connection . execute " UPDATE #{ copied_jamisp_table_name } SET geom = ST_MakeEnvelope(beginip, -1, endip, 1) "
ActiveRecord :: Base . logger . debug " SET geom returned sts #{ sts . cmd_tuples } " if ActiveRecord :: Base . logger
sts . check
# recreate indexes on jamisp
sts = GeoIpLocations . connection . execute " CREATE INDEX #{ COPIED_JAMISP_GEOM_INDEX_NAME } ON #{ copied_jamisp_table_name } USING GIST (geom) "
ActiveRecord :: Base . logger . debug " CREATE INDEX #{ COPIED_JAMISP_GEOM_INDEX_NAME } returned sts #{ sts . cmd_status } " if ActiveRecord :: Base . logger
sts . check
GeoIpLocations . connection . execute ( " CREATE INDEX #{ COPIED_JAMISP_COID_INDEX_NAME } ON #{ copied_jamisp_table_name } (coid) " ) . check
elapsed = Time . now - start
@@log . debug ( " #{ copied_jamisp_table_name } import took #{ elapsed } seconds " )
end
def self . after_maxmind_import
# handle jamisp
self . connection . execute ( " DROP TABLE #{ self . table_name } " ) . check
self . connection . execute ( " ALTER INDEX #{ COPIED_JAMISP_GEOM_INDEX_NAME } RENAME TO #{ JAMISP_GEOM_INDEX_NAME } " ) . check
self . connection . execute ( " ALTER INDEX #{ COPIED_JAMISP_COID_INDEX_NAME } RENAME TO #{ JAMISP_COID_INDEX_NAME } " ) . check
self . connection . execute ( " ALTER TABLE #{ self . table_name } _copied RENAME TO #{ self . table_name } " ) . check
# handle geoipisp
self . connection . execute ( " DROP TABLE #{ GEOIPISP_TABLE } " ) . check
self . connection . execute ( " ALTER INDEX #{ COPIED_GEOIPISP_INDEX_NAME } RENAME TO #{ GEOIPISP_INDEX_NAME } " ) . check
self . connection . execute ( " ALTER TABLE #{ GEOIPISP_TABLE } _copied RENAME TO #{ GEOIPISP_TABLE } " ) . check
# handle jamcompany
self . connection . execute ( " DROP TABLE #{ COMPANY_TABLE } " ) . check
self . connection . execute ( " ALTER INDEX #{ COPIED_JAMCOMPANY_UNIQUE_INDEX } RENAME TO #{ JAMCOMPANY_UNIQUE_INDEX } " ) . check
self . connection . execute ( " ALTER INDEX #{ COPIED_JAMCOMPANY_PRIMARY_KEY_NAME } RENAME TO #{ JAMCOMPANY_PRIMARY_KEY_NAME } " ) . check
self . connection . execute ( " ALTER SEQUENCE #{ COPIED_JAMCOMPANY_COID_SEQUENCE } RENAME TO #{ JAMCOMPANY_COID_SEQUENCE } " ) . check
self . connection . execute ( " ALTER TABLE #{ COMPANY_TABLE } _copied RENAME TO #{ COMPANY_TABLE } " ) . check
2014-03-09 06:22:51 +00:00
end
2014-02-23 04:00:32 +00:00
end
end
2020-09-01 18:33:04 +00:00
= end