From 823d763171e1014ad8e7ee31a8ac97956a3b3b3c Mon Sep 17 00:00:00 2001 From: Harri Hirvonsalo Date: Wed, 6 Oct 2021 14:51:47 +0300 Subject: [PATCH] Backport elasticsearch mapping fix for v1.0.0a8 Signed-off-by: Harri Hirvonsalo --- invenio_stats/processors.py | 2 +- invenio_stats/utils.py | 6 ++---- tests/test_utils.py | 3 +-- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/invenio_stats/processors.py b/invenio_stats/processors.py index 001429e1..342af47b 100644 --- a/invenio_stats/processors.py +++ b/invenio_stats/processors.py @@ -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', '') diff --git a/invenio_stats/utils.py b/invenio_stats/utils.py index 54a7e43e..91081fa2 100644 --- a/invenio_stats/utils.py +++ b/invenio_stats/utils.py @@ -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(): diff --git a/tests/test_utils.py b/tests/test_utils.py index c13425a5..9aa797b2 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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):