Merge lp:~cjwatson/launchpad/geoip-country into lp:launchpad

Proposed by Colin Watson
Status: Merged
Merged at revision: 19029
Proposed branch: lp:~cjwatson/launchpad/geoip-country
Merge into: lp:launchpad
Prerequisite: lp:~cjwatson/launchpad/geoip-ip-address-handling
Diff against target: 159 lines (+16/-35)
8 files modified
configs/development/launchpad-lazr.conf (+0/-1)
configs/testrunner/launchpad-lazr.conf (+0/-1)
lib/lp/services/apachelogparser/base.py (+2/-5)
lib/lp/services/apachelogparser/tests/apache-log-files/launchpadlibrarian.net.access-log (+1/-1)
lib/lp/services/config/schema-lazr.conf (+1/-1)
lib/lp/services/geoip/doc/geoip.txt (+6/-19)
lib/lp/services/geoip/interfaces.py (+2/-2)
lib/lp/services/geoip/model.py (+4/-5)
To merge this branch: bzr merge lp:~cjwatson/launchpad/geoip-country
Reviewer Review Type Date Requested Status
William Grant code Approve
Review via email: mp+371044@code.launchpad.net

Commit message

Switch to the GeoIP country database.

Description of the change

The only piece of information we use from this now is the country code, so there's no point reading the much larger city database.

To post a comment you must log in.
Revision history for this message
William Grant (wgrant) :
review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'configs/development/launchpad-lazr.conf'
--- configs/development/launchpad-lazr.conf 2019-06-18 17:30:52 +0000
+++ configs/development/launchpad-lazr.conf 2019-08-07 15:43:20 +0000
@@ -109,7 +109,6 @@
109summary_list_size: 5109summary_list_size: 5
110max_bug_feed_cache_minutes: 30110max_bug_feed_cache_minutes: 30
111bzr_imports_root_url: file:///tmp/bazaar-branches111bzr_imports_root_url: file:///tmp/bazaar-branches
112geoip_database: /usr/share/GeoIP/GeoLiteCity.dat
113feature_flags_endpoint: http://xmlrpc-private.launchpad.test:8087/featureflags/112feature_flags_endpoint: http://xmlrpc-private.launchpad.test:8087/featureflags/
114113
115[launchpad_session]114[launchpad_session]
116115
=== modified file 'configs/testrunner/launchpad-lazr.conf'
--- configs/testrunner/launchpad-lazr.conf 2019-05-22 14:57:45 +0000
+++ configs/testrunner/launchpad-lazr.conf 2019-08-07 15:43:20 +0000
@@ -103,7 +103,6 @@
103[launchpad]103[launchpad]
104basic_auth_password: test104basic_auth_password: test
105max_attachment_size: 1024105max_attachment_size: 1024
106geoip_database: /usr/share/GeoIP/GeoLiteCity.dat
107logparser_max_parsed_lines: 100000106logparser_max_parsed_lines: 100000
108homepage_recent_posts_feed: http://launchpad.test:8093/blog-feed107homepage_recent_posts_feed: http://launchpad.test:8093/blog-feed
109openid_canonical_root: http://testopenid.test/108openid_canonical_root: http://testopenid.test/
110109
=== modified file 'lib/lp/services/apachelogparser/base.py'
--- lib/lp/services/apachelogparser/base.py 2019-07-25 15:00:18 +0000
+++ lib/lp/services/apachelogparser/base.py 2019-08-07 15:43:20 +0000
@@ -1,4 +1,4 @@
1# Copyright 2009-2017 Canonical Ltd. This software is licensed under the1# Copyright 2009-2019 Canonical Ltd. This software is licensed under the
2# GNU Affero General Public License version 3 (see the file LICENSE).2# GNU Affero General Public License version 3 (see the file LICENSE).
33
4from datetime import datetime4from datetime import datetime
@@ -148,10 +148,7 @@
148 file_downloads[day] = {}148 file_downloads[day] = {}
149 daily_downloads = file_downloads[day]149 daily_downloads = file_downloads[day]
150150
151 country_code = None151 country_code = geoip.getCountryCodeByAddr(host)
152 geoip_record = geoip.getRecordByAddress(host)
153 if geoip_record is not None:
154 country_code = geoip_record['country_code']
155 if country_code not in daily_downloads:152 if country_code not in daily_downloads:
156 daily_downloads[country_code] = 0153 daily_downloads[country_code] = 0
157 daily_downloads[country_code] += 1154 daily_downloads[country_code] += 1
158155
=== modified file 'lib/lp/services/apachelogparser/tests/apache-log-files/launchpadlibrarian.net.access-log'
--- lib/lp/services/apachelogparser/tests/apache-log-files/launchpadlibrarian.net.access-log 2010-06-17 15:23:40 +0000
+++ lib/lp/services/apachelogparser/tests/apache-log-files/launchpadlibrarian.net.access-log 2019-08-07 15:43:20 +0000
@@ -2,5 +2,5 @@
2121.44.28.210 - - [13/Jun/2008:14:55:06 +0100] "GET /9096290/me-tv-icon-14x14.png HTTP/1.1" 200 730 "https://launchpad.net/me-tv" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008060900 Firefox/3.0"2121.44.28.210 - - [13/Jun/2008:14:55:06 +0100] "GET /9096290/me-tv-icon-14x14.png HTTP/1.1" 200 730 "https://launchpad.net/me-tv" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008060900 Firefox/3.0"
3121.44.28.210 - - [13/Jun/2008:14:55:13 +0100] "GET /12060796/me-tv-icon-64x64.png HTTP/1.1" 200 6378 "https://launchpad.net/me-tv" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008060900 Firefox/3.0"3121.44.28.210 - - [13/Jun/2008:14:55:13 +0100] "GET /12060796/me-tv-icon-64x64.png HTTP/1.1" 200 6378 "https://launchpad.net/me-tv" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008060900 Firefox/3.0"
4157.92.18.21 - - [13/Jun/2008:14:55:15 +0100] "GET /8196569/mediumubuntulogo.png HTTP/1.1" 200 3420 "https://bugs.launchpad.net/ubuntu/+source/acpi-support/+bug/59695/comments/14" "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9) Gecko/2008052623 Iceweasel/3.0 (Debian-3.0~rc1-1)"4157.92.18.21 - - [13/Jun/2008:14:55:15 +0100] "GET /8196569/mediumubuntulogo.png HTTP/1.1" 200 3420 "https://bugs.launchpad.net/ubuntu/+source/acpi-support/+bug/59695/comments/14" "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9) Gecko/2008052623 Iceweasel/3.0 (Debian-3.0~rc1-1)"
5210.79.178.243 - - [13/Jun/2008:14:55:21 +0100] "GET /8196569/mediumubuntulogo.png HTTP/1.1" 200 3420 "https://launchpad.net/ubuntu/hardy/hppa/xserver-xorg-driver-savage" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008060309 Firefox/3.0"5202.214.194.147 - - [13/Jun/2008:14:55:21 +0100] "GET /8196569/mediumubuntulogo.png HTTP/1.1" 200 3420 "https://launchpad.net/ubuntu/hardy/hppa/xserver-xorg-driver-savage" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008060309 Firefox/3.0"
669.233.136.42 - - [13/Jun/2008:14:55:22 +0100] "GET /15018215/ul_logo_64x64.png HTTP/1.1" 200 2261 "https://launchpad.net/~ubuntulite/+archive" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9b5) Gecko/2008041514 Firefox/3.0b5"669.233.136.42 - - [13/Jun/2008:14:55:22 +0100] "GET /15018215/ul_logo_64x64.png HTTP/1.1" 200 2261 "https://launchpad.net/~ubuntulite/+archive" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9b5) Gecko/2008041514 Firefox/3.0b5"
77
=== modified file 'lib/lp/services/config/schema-lazr.conf'
--- lib/lp/services/config/schema-lazr.conf 2019-07-11 13:27:09 +0000
+++ lib/lp/services/config/schema-lazr.conf 2019-08-07 15:43:20 +0000
@@ -1112,7 +1112,7 @@
1112code_homepage_product_cloud_size: 1201112code_homepage_product_cloud_size: 120
11131113
1114# The database used by our GeoIP library.1114# The database used by our GeoIP library.
1115geoip_database: /usr/share/GeoIP/GeoIPCity.dat1115geoip_database: /usr/share/GeoIP/GeoIP.dat
11161116
1117# The maximum number of lines that should be parsed by the launchpad1117# The maximum number of lines that should be parsed by the launchpad
1118# log parser. The default value of None means there is no maximum.1118# log parser. The default value of None means there is no maximum.
11191119
=== modified file 'lib/lp/services/geoip/doc/geoip.txt'
--- lib/lp/services/geoip/doc/geoip.txt 2019-08-07 15:42:37 +0000
+++ lib/lp/services/geoip/doc/geoip.txt 2019-08-07 15:43:20 +0000
@@ -37,32 +37,19 @@
37 >>> print(geoip.getCountryByAddr('fc00::1'))37 >>> print(geoip.getCountryByAddr('fc00::1'))
38 None38 None
3939
40IGeoIP also provides a getRecordByAddress() method, which returns a dict40IGeoIP also provides a getCountryCodeByAddr() method, which returns just the
41containing a bunch more information about that IP's location.41country code without looking it up in the database.
4242
43 >>> record = geoip.getRecordByAddress('201.13.165.145')43 >>> print(geoip.getCountryCodeByAddr('201.13.165.145'))
44 >>> for key, value in sorted(record.items()):44 BR
45 ... print "%s: %s" % (key, value)
46 area_code: ...
47 city: ...
48 country_code: BR
49 country_code3: BRA
50 country_name: Brazil
51 dma_code: ...
52 latitude: ...
53 longitude: ...
54 postal_code: ...
55 region: ...
56 region_name: ...
57 time_zone: ...
5845
59And again we'll return None if the address is private.46And again we'll return None if the address is private.
6047
61 >>> print(geoip.getRecordByAddress('127.0.0.1'))48 >>> print(geoip.getCountryCodeByAddr('127.0.0.1'))
62 None49 None
6350
64If it can't find a GeoIP record for the given IP address, it will return51If it can't find a GeoIP record for the given IP address, it will return
65None.52None.
6653
67 >>> print geoip.getRecordByAddress('255.255.255.255')54 >>> print(geoip.getCountryCodeByAddr('255.255.255.255'))
68 None55 None
6956
=== modified file 'lib/lp/services/geoip/interfaces.py'
--- lib/lp/services/geoip/interfaces.py 2019-08-07 15:42:37 +0000
+++ lib/lp/services/geoip/interfaces.py 2019-08-07 15:43:20 +0000
@@ -14,8 +14,8 @@
14class IGeoIP(Interface):14class IGeoIP(Interface):
15 """The GeoIP utility, which represents the GeoIP database."""15 """The GeoIP utility, which represents the GeoIP database."""
1616
17 def getRecordByAddress(ip_address):17 def getCountryCodeByAddr(ip_address):
18 """Return the GeoIP record for the given IP address, or None."""18 """Return the country code for the given IP address, or None."""
1919
20 def getCountryByAddr(ip_address):20 def getCountryByAddr(ip_address):
21 """Find and return an ICountry based on the given IP address.21 """Find and return an ICountry based on the given IP address.
2222
=== modified file 'lib/lp/services/geoip/model.py'
--- lib/lp/services/geoip/model.py 2019-08-07 15:42:37 +0000
+++ lib/lp/services/geoip/model.py 2019-08-07 15:43:20 +0000
@@ -41,12 +41,12 @@
41 return libGeoIP.open(41 return libGeoIP.open(
42 config.launchpad.geoip_database, libGeoIP.GEOIP_MEMORY_CACHE)42 config.launchpad.geoip_database, libGeoIP.GEOIP_MEMORY_CACHE)
4343
44 def getRecordByAddress(self, ip_address):44 def getCountryCodeByAddr(self, ip_address):
45 """See `IGeoIP`."""45 """See `IGeoIP`."""
46 if not ipaddress_is_global(ip_address):46 if not ipaddress_is_global(ip_address):
47 return None47 return None
48 try:48 try:
49 return self._gi.record_by_addr(ip_address)49 return self._gi.country_code_by_addr(ip_address)
50 except SystemError:50 except SystemError:
51 # libGeoIP may raise a SystemError if it doesn't find a record for51 # libGeoIP may raise a SystemError if it doesn't find a record for
52 # some IP addresses (e.g. 255.255.255.255), so we need to catch52 # some IP addresses (e.g. 255.255.255.255), so we need to catch
@@ -57,10 +57,9 @@
57 """See `IGeoIP`."""57 """See `IGeoIP`."""
58 if not ipaddress_is_global(ip_address):58 if not ipaddress_is_global(ip_address):
59 return None59 return None
60 geoip_record = self.getRecordByAddress(ip_address)60 countrycode = self.getCountryCodeByAddr(ip_address)
61 if geoip_record is None:61 if countrycode is None:
62 return None62 return None
63 countrycode = geoip_record['country_code']
6463
65 countryset = getUtility(ICountrySet)64 countryset = getUtility(ICountrySet)
66 try:65 try: