jam-cloud/ruby/lib/jam_ruby/models/geo_ip_blocks.rb

112 lines
4.3 KiB
Ruby

module JamRuby
class GeoIpBlocks < ActiveRecord::Base
self.table_name = 'geoipblocks'
def self.lookup(ipnum)
self.where('geom && ST_MakePoint(?, 0) AND ? BETWEEN beginip AND endip', ipnum, ipnum)
.limit(1)
.first
end
def self.createx(beginip, endip, locid)
c = connection.raw_connection
c.exec_params("insert into #{self.table_name} (beginip, endip, locid, geom) values($1::bigint, $2::bigint, $3, ST_MakeEnvelope($1::bigint, -1, $2::bigint, 1))", [beginip, endip, locid])
end
def self.import_from_max_mind(file)
# File Geo-134
# Format:
# startIpNum,endIpNum,locId
self.transaction do
self.delete_all
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) 2011 MaxMind Inc. All Rights Reserved.'
puts s
puts 'Copyright (c) 2011 MaxMind Inc. All Rights Reserved.'
raise 'file does not start with expected copyright (line 1): Copyright (c) 2011 MaxMind Inc. All Rights Reserved.'
end
s = io.gets.strip # eat the headers line
unless s.eql? 'startIpNum,endIpNum,locId'
puts s
puts 'startIpNum,endIpNum,locId'
raise 'file does not start with expected header (line 2): startIpNum,endIpNum,locId'
end
saved_level = ActiveRecord::Base.logger ? ActiveRecord::Base.logger.level : 0
count = 0
stmt = "insert into #{self.table_name} (beginip, endip, locid) 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]))
locid = row[2]
vals = vals+sep+"(#{beginip}, #{endip}, #{locid})"
sep = ','
i += 1
if count == 0 or i >= n then
self.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 #{self.table_name} 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 #{self.table_name} ..."
ActiveRecord::Base.logger.level = 1
end
end
end
if i > 0 then
self.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 #{self.table_name}"
end
sts = self.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 = self.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 = self.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 = self.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