faster signup

This commit is contained in:
Scott Comer 2014-03-11 17:35:00 -05:00
parent 3c3f191239
commit 60aa59e11f
6 changed files with 64 additions and 42 deletions

View File

@ -135,4 +135,5 @@ update_get_work_for_client_type.sql
events.sql
cascading_delete_constraints_for_release.sql
events_social_description.sql
fix_broken_cities.sql

View File

@ -0,0 +1,2 @@
alter table cities drop column regionname;
alter table cities drop column countryname;

View File

@ -132,7 +132,7 @@ module JamRuby
ActiveRecord::Base.logger.debug "DELETE FROM #{REGIONS_TABLE} returned sts #{sts.cmd_status}" if ActiveRecord::Base.logger
sts.check
sts = self.connection.execute "INSERT INTO #{REGIONS_TABLE} (region, countrycode) SELECT DISTINCT region, countrycode FROM #{CITIES_TABLE};"
sts = self.connection.execute "INSERT INTO #{REGIONS_TABLE} (region, regionname, countrycode) SELECT DISTINCT region, region, countrycode FROM #{CITIES_TABLE};"
ActiveRecord::Base.logger.debug "INSERT INTO #{REGIONS_TABLE} returned sts #{sts.cmd_status}" if ActiveRecord::Base.logger
sts.check
@ -140,7 +140,7 @@ module JamRuby
ActiveRecord::Base.logger.debug "DELETE FROM #{COUNTRIES_TABLE} returned sts #{sts.cmd_status}" if ActiveRecord::Base.logger
sts.check
sts = self.connection.execute "INSERT INTO #{COUNTRIES_TABLE} (countrycode) SELECT DISTINCT countrycode FROM #{REGIONS_TABLE};"
sts = self.connection.execute "INSERT INTO #{COUNTRIES_TABLE} (countrycode, countryname) SELECT DISTINCT countrycode, countrycode FROM #{REGIONS_TABLE};"
ActiveRecord::Base.logger.debug "INSERT INTO #{COUNTRIES_TABLE} returned sts #{sts.cmd_status}" if ActiveRecord::Base.logger
sts.check
end

View File

@ -12,25 +12,28 @@ class MaxMindManager < BaseManager
city = state = country = nil
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 $2 <= ip_end limit 1", [ip_as_int, ip_as_int]) do |result|
if !result.nil? && result.ntuples > 0
country = result[0]['country']
state = result[0]['region']
city = result[0]['city']
end
end
#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 $2 <= ip_end limit 1", [ip_as_int, ip_as_int]) do |result|
# if !result.nil? && result.ntuples > 0
# country = result[0]['country']
# state = result[0]['region']
# city = result[0]['city']
# end
# end
#end
ip_as_int = ip_address_to_int(ip_address)
block = GeoIpBlocks.lookup(ip_as_int)
location = block ? GeoIpLocations.lookup(block.locid) : nil
if location
country = location.countrycode
state = location.region
city = location.city
end
end
{
:city => city,
:state => state,
:country => country
}
a = {:city => city, :state => state, :country => country}
end
def self.lookup_isp(ip_address)
@ -53,32 +56,41 @@ class MaxMindManager < BaseManager
end
def self.countries()
ActiveRecord::Base.connection_pool.with_connection do |connection|
pg_conn = connection.instance_variable_get("@connection")
pg_conn.exec("SELECT DISTINCT country FROM max_mind_geo ORDER BY country ASC").map do |tuple|
tuple["country"]
end
end
#ActiveRecord::Base.connection_pool.with_connection do |connection|
# pg_conn = connection.instance_variable_get("@connection")
# pg_conn.exec("SELECT DISTINCT country FROM max_mind_geo ORDER BY country ASC").map do |tuple|
# tuple["country"]
# end
#end
# returns ordered array of Country objects (countrycode, countryname)
Country.get_all.map { |c| c.countrycode }
end
def self.regions(country)
ActiveRecord::Base.connection_pool.with_connection do |connection|
pg_conn = connection.instance_variable_get("@connection")
pg_conn.exec("SELECT DISTINCT region FROM max_mind_geo WHERE country = $1 ORDER BY region ASC", [country]).map do |tuple|
tuple["region"]
end
end
#ActiveRecord::Base.connection_pool.with_connection do |connection|
# pg_conn = connection.instance_variable_get("@connection")
# pg_conn.exec("SELECT DISTINCT region FROM max_mind_geo WHERE country = $1 ORDER BY region ASC", [country]).map do |tuple|
# tuple["region"]
# end
#end
# returns an ordered array of Region objects (region, regionname, countrycode)
Region.get_all(country).map { |r| r.region }
end
def self.cities(country, region)
ActiveRecord::Base.connection_pool.with_connection do |connection|
pg_conn = connection.instance_variable_get("@connection")
pg_conn.exec("SELECT DISTINCT city FROM max_mind_geo WHERE country = $1 AND region = $2 ORDER BY city ASC", [country, region]).map do |tuple|
tuple["city"]
end
end
#ActiveRecord::Base.connection_pool.with_connection do |connection|
# pg_conn = connection.instance_variable_get("@connection")
# pg_conn.exec("SELECT DISTINCT city FROM max_mind_geo WHERE country = $1 AND region = $2 ORDER BY city ASC", [country, region]).map do |tuple|
# tuple["city"]
# end
#end
# returns an ordered array of City (city, region, countrycode)
City.get_all(country, region).map { |c| c.city }
end
@ -116,7 +128,14 @@ class MaxMindManager < BaseManager
]).clear
end
@pg_conn.exec "DELETE FROM cities"
@pg_conn.exec "INSERT INTO cities (city, region, countrycode) SELECT DISTINCT city, region, country FROM max_mind_geo"
@pg_conn.exec "DELETE FROM regions"
@pg_conn.exec "INSERT INTO regions (region, regionname, countrycode) select distinct region, region, countrycode from cities"
@pg_conn.exec "DELETE FROM countries"
@pg_conn.exec "INSERT INTO countries (countrycode, countryname) SELECT DISTINCT countrycode, countrycode FROM regions"
end
private

View File

@ -20,7 +20,7 @@ describe MaxMindManager do
regions = MaxMindManager.regions("US")
regions.length.should == 4
regions.first.should == "AB"
regions.last.should == "DE" # Based on sort order this will be the top.
regions.last.should == "DE"
end
it "looks up cities successfully" do

View File

@ -31,9 +31,9 @@ describe UserManager do
@user.last_name.should == "smith"
@user.email.should == "userman1@jamkazam.com"
@user.email_confirmed.should be_false
@user.city.should be_nil
@user.state.should be_nil
@user.country.should be_nil
@user.city.should == 'Boston'
@user.state.should == 'MA'
@user.country.should == 'US'
@user.instruments.length.should == 1
@user.subscribe_email.should be_true
@user.signup_token.should_not be_nil
@ -93,8 +93,8 @@ describe UserManager do
signup_confirm_url: "http://localhost:3000/confirm" )
@user.errors.any?.should be_false
@user.city.should == 'City 127'
@user.state.should == 'DE'
@user.city.should == 'Boston'
@user.state.should == 'MA'
@user.country.should == 'US'
end