Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Testing: Initial pass at moving to geoip2. #869

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions edx/analytics/tasks/export/events_obfuscation.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,10 @@ def _obfuscate_event(self, event):

def extra_modules(self):
import numpy
import pygeoip
return [numpy, pygeoip]
# import pygeoip
# return [numpy, pygeoip]
import geoip2
return [numpy, geoip2]


class EventObfuscationTask(ObfuscatorDownstreamMixin, MapReduceJobTaskMixin, luigi.WrapperTask):
Expand Down
25 changes: 17 additions & 8 deletions edx/analytics/tasks/util/geolocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
from edx.analytics.tasks.util.url import ExternalURL

try:
import pygeoip
# import pygeoip
import geoip2.database
except ImportError:
# The module will be imported on slave nodes even though they don't actually have the package installed.
# The module is hopefully exported for tasks that actually use the module.
pygeoip = NotImplemented
# pygeoip = NotImplemented
geoip2 = NotImplemented


UNKNOWN_COUNTRY = "UNKNOWN"
Expand All @@ -36,6 +38,7 @@ class GeolocationMixin(GeolocationDownstreamMixin):
"""Provides support for initializing a geolocation object."""

geoip = None
# Why is this set to none? geoip2 = None

def requires_local(self):
"""Adds geolocation_data as a local requirement."""
Expand Down Expand Up @@ -67,7 +70,8 @@ def init_reducer(self):
break
self.temporary_data_file.seek(0)

self.geoip = pygeoip.GeoIP(self.temporary_data_file.name, pygeoip.STANDARD)
# self.geoip = pygeoip.GeoIP(self.temporary_data_file.name, pygeoip.STANDARD)
self.geoip = geoip2.database.Reader(self.temporary_data_file.name)

def final_reducer(self):
"""Clean up after the reducer is done."""
Expand All @@ -77,12 +81,15 @@ def final_reducer(self):
return tuple()

def extra_modules(self):
"""Pygeoip is required by all tasks that perform geolocation."""
# """Pygeoip is required by all tasks that perform geolocation."""
"""geoip2 is required by all tasks that perform geolocation."""
modules = super(GeolocationMixin, self).extra_modules()
if not modules:
return [pygeoip]
# return [pygeoip]
return [geoip2]
else:
return modules.append(pygeoip)
# return modules.append(pygeoip)
return modules.append(geoip2)

def get_country_name(self, ip_address, debug_message=None):
"""
Expand All @@ -93,7 +100,8 @@ def get_country_name(self, ip_address, debug_message=None):

"""
try:
name = self.geoip.country_name_by_addr(ip_address)
# name = self.geoip.country_name_by_addr(ip_address)
name = (self.geoip.city(ip_address)).country.name
except Exception: # pylint: disable=broad-except
if debug_message:
log.exception("Encountered exception getting country name for ip_address '%s': %s.",
Expand All @@ -116,7 +124,8 @@ def get_country_code(self, ip_address, debug_message=None):

"""
try:
code = self.geoip.country_code_by_addr(ip_address)
# code = self.geoip.country_code_by_addr(ip_address)
code = (self.geoip.city(ip_address)).country.iso_code
except Exception: # pylint: disable=broad-except
if debug_message:
log.exception("Encountered exception getting country code for ip_address '%s': %s.",
Expand Down
5 changes: 3 additions & 2 deletions requirements/default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ paramiko==2.6.0
paypalrestsdk==1.9.0
pbr==5.4.3 # via stevedore
protobuf==3.10.0 # via google-cloud-core, googleapis-common-protos
psycopg2==2.6.2
# psycopg2==2.6.2
pyasn1-modules==0.2.7 # via google-auth, snowflake-connector-python
pyasn1==0.4.7 # via pyasn1-modules, rsa, snowflake-connector-python
pycparser==2.19
pycrypto==2.6.1
pycryptodomex==3.9.0 # via snowflake-connector-python
pygeoip==0.3.2
# pygeoip==0.3.2
geoip2==2.9.0
pyjwt==1.7.1 # via snowflake-connector-python
pymongo==3.9.0 # via edx-opaque-keys
pynacl==1.3.0
Expand Down
5 changes: 3 additions & 2 deletions requirements/docs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ paramiko==2.6.0
paypalrestsdk==1.9.0
pbr==5.4.3
protobuf==3.10.0
psycopg2==2.6.2
# psycopg2==2.6.2
pyasn1-modules==0.2.7
pyasn1==0.4.7
pycparser==2.19
pycrypto==2.6.1
pycryptodomex==3.9.0
pygeoip==0.3.2
geoip2==2.9.0
# pygeoip==0.3.2
pygments==2.4.2 # via sphinx
pyjwt==1.7.1
pymongo==3.9.0
Expand Down
5 changes: 3 additions & 2 deletions requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ paramiko==2.6.0
paypalrestsdk==1.9.0
pbr==5.4.3
protobuf==3.10.0
psycopg2==2.6.2
# psycopg2==2.6.2
pyasn1-modules==0.2.7
pyasn1==0.4.7
pycodestyle==2.3.1
pycparser==2.19
pycrypto==2.6.1
pycryptodomex==3.9.0
pygeoip==0.3.2
geoip2==2.9.0
# pygeoip==0.3.2
pygments==2.4.2 # via diff-cover
pyjwt==1.7.1
pylint==1.6.4
Expand Down