vrfs-774: replaced original max_geo_isp db cols

This commit is contained in:
Jonathan Kolyer 2013-11-03 02:45:54 -06:00
parent a3ee0eb533
commit 2b19744683
1 changed files with 13 additions and 4 deletions

View File

@ -14,6 +14,7 @@ class MaxMindManager < BaseManager
unless ip_address.nil? || ip_address !~ /^\d+\.\d+\.\d+\.\d+$/
ActiveRecord::Base.connection_pool.with_connection do |connection|
pg_conn = connection.instance_variable_get("@connection")
ip_as_int = ip_address_to_int(ip_address)
pg_conn.exec("SELECT country, region, city FROM max_mind_geo WHERE ip_start <= $1 AND ip_end >= $2", [ip_address, ip_address]) do |result|
if !result.nil? && result.ntuples > 0
country = result.getvalue(0, 0)
@ -39,7 +40,8 @@ class MaxMindManager < BaseManager
unless ip_address.nil? || ip_address !~ /^\d+\.\d+\.\d+\.\d+$/
ActiveRecord::Base.connection_pool.with_connection do |connection|
pg_conn = connection.instance_variable_get("@connection")
pg_conn.exec("SELECT isp FROM max_mind_isp WHERE ip_star <= $1 AND ip_end >= $2", [ip_address, ip_address]) do |result|
ip_as_int = ip_address_to_int(ip_address)
pg_conn.exec("SELECT isp FROM max_mind_isp WHERE ip_bottom <= $1 AND ip_top >= $2", [ip_as_int, ip_as_int]) do |result|
if !result.nil? && result.ntuples > 0
isp = result.getvalue(0, 0)
end
@ -105,10 +107,10 @@ class MaxMindManager < BaseManager
clear_isp_table
(0..255).each do |top_octet|
@pg_conn.exec("INSERT INTO max_mind_isp (ip_start, ip_end, isp, country) VALUES ($1, $2, $3, $4)",
@pg_conn.exec("INSERT INTO max_mind_isp (ip_bottom, ip_top, isp, country) VALUES ($1, $2, $3, $4)",
[
"#{top_octet}.0.0.0",
"#{top_octet}.255.255.255",
self.class.ip_address_to_int("#{top_octet}.0.0.0"),
self.class.ip_address_to_int("#{top_octet}.255.255.255"),
"ISP #{top_octet}",
"US"
]).clear
@ -118,6 +120,13 @@ class MaxMindManager < BaseManager
end
private
# Make an IP address fit in a signed int. Just divide it by 2, as the least significant part
# just can't possibly matter. We can verify this if needed. My guess is the entire bottom octet is
# actually irrelevant
def self.ip_address_to_int(ip)
ip.split('.').inject(0) {|total,value| (total << 8 ) + value.to_i} / 2
end
def clear_location_table
@pg_conn.exec("DELETE FROM max_mind_geo").clear