require 'ipaddr' module JamRuby class JamIsp < ActiveRecord::Base self.table_name = 'jamisp' COMPANY_TABLE = 'jamcompany' GEOIPISP_TABLE = 'geoipisp' def self.ip_to_num(ip_addr) begin i = IPAddr.new(ip_addr) return i.to_i if i.ipv4? nil rescue IPAddr::InvalidAddressError nil end end def self.lookup(ipnum) JamIsp.select(:coid) .where('geom && ST_MakePoint(?, 0) AND ? BETWEEN beginip AND endip', ipnum, ipnum) .limit(1) .first end def self.createx(beginip, endip, coid) c = connection.raw_connection 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))", [beginip, endip, coid]) end def self_delete() raise "mother trucker" end def self.delete_all() raise "mother trucker" end def self.import_from_max_mind(file) # File Geo-124 # Format: # startIpNum,endIpNum,isp self.transaction do self.connection.execute "delete from #{GEOIPISP_TABLE}" 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 stmt = "insert into #{GEOIPISP_TABLE} (beginip, endip, company) values" 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 beginip = MaxMindIsp.ip_address_to_int(MaxMindIsp.strip_quotes(row[0])) endip = MaxMindIsp.ip_address_to_int(MaxMindIsp.strip_quotes(row[1])) company = row[2] vals = vals+sep+"(#{beginip}, #{endip}, #{MaxMindIsp.quote_value(company)})" 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 ActiveRecord::Base.logger.debug "... logging inserts into #{GEOIPISP_TABLE} suspended ..." ActiveRecord::Base.logger.level = 1 end if ActiveRecord::Base.logger and count%10000 < n then ActiveRecord::Base.logger.level = saved_level ActiveRecord::Base.logger.debug "... inserted #{count} into #{GEOIPISP_TABLE} ..." 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 ActiveRecord::Base.logger.debug "loaded #{count} records into #{GEOIPISP_TABLE}" end sts = GeoIpLocations.connection.execute "DELETE FROM #{COMPANY_TABLE};" ActiveRecord::Base.logger.debug "DELETE FROM #{COMPANY_TABLE} returned sts #{sts.cmd_status}" if ActiveRecord::Base.logger sts.check sts = GeoIpLocations.connection.execute "ALTER SEQUENCE #{COMPANY_TABLE}_coid_seq RESTART WITH 1;" ActiveRecord::Base.logger.debug "ALTER SEQUENCE #{COMPANY_TABLE}_coid_seq returned sts #{sts.cmd_status}" if ActiveRecord::Base.logger sts.check sts = GeoIpLocations.connection.execute "INSERT INTO #{COMPANY_TABLE} (company) SELECT DISTINCT company FROM #{GEOIPISP_TABLE} ORDER BY company;" ActiveRecord::Base.logger.debug "INSERT INTO #{COMPANY_TABLE} returned sts #{sts.cmd_status}" if ActiveRecord::Base.logger sts.check sts = GeoIpLocations.connection.execute "DELETE FROM #{self.table_name};" ActiveRecord::Base.logger.debug "DELETE FROM #{self.table_name} returned sts #{sts.cmd_status}" if ActiveRecord::Base.logger sts.check sts = GeoIpLocations.connection.execute "INSERT INTO #{self.table_name} (beginip, endip, coid) SELECT x.beginip, x.endip, y.coid FROM #{GEOIPISP_TABLE} x, #{COMPANY_TABLE} y WHERE x.company = y.company;" ActiveRecord::Base.logger.debug "INSERT INTO #{self.table_name} returned sts #{sts.cmd_status}" if ActiveRecord::Base.logger sts.check sts = GeoIpLocations.connection.execute "ALTER TABLE #{self.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] sts = GeoIpLocations.connection.execute "ALTER TABLE #{self.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 sts = GeoIpLocations.connection.execute "UPDATE #{self.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 sts = GeoIpLocations.connection.execute "CREATE INDEX #{self.table_name}_geom_gix ON #{self.table_name} USING GIST (geom);" ActiveRecord::Base.logger.debug "CREATE INDEX #{self.table_name}_geom_gix returned sts #{sts.cmd_status}" if ActiveRecord::Base.logger sts.check end end end end end