Skip to content

Commit

Permalink
Backport elasticsearch mapping fix for v1.0.0a8
Browse files Browse the repository at this point in the history
Signed-off-by: Harri Hirvonsalo <[email protected]>
  • Loading branch information
hjhsalo committed Oct 6, 2021
1 parent 74b2730 commit 823d763
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 7 deletions.
2 changes: 1 addition & 1 deletion invenio_stats/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def anonymize_user(doc):
"""Preprocess an event by anonymizing user information."""
ip = doc.pop('ip_address', None)
if ip:
doc.update(get_geoip(ip))
doc.update({'country': get_geoip(ip)})

uid = doc.pop('user_id', '')
ua = doc.pop('user_agent', '')
Expand Down
6 changes: 2 additions & 4 deletions invenio_stats/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@
def get_geoip(ip):
"""Lookup country for IP address."""
reader = geolite2.reader()
ip_data = reader.get(ip)
if ip_data is not None:
return dict(country=ip_data['country'])
return {}
ip_data = reader.get(ip) or {}
return ip_data.get('country', {}).get('iso_code')


def get_user():
Expand Down
3 changes: 1 addition & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ def test_get_user(app, mock_users, request_headers):

def test_get_geoip():
"""Test looking up IP address."""
assert get_geoip("74.125.67.100")['country']['names']['en'] == \
"United States"
assert get_geoip("74.125.67.100") == 'US'


def test_obj_or_import_string(app):
Expand Down

0 comments on commit 823d763

Please sign in to comment.