from __future__ import unicode_literals
from datetime import timedelta
import logging
import os

from core.analytics import ScheduledAnalytics
from core.errors import ObservableValidationError
import geoip2.database
from geoip2.errors import AddressNotFoundError


try:
    path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "GeoLite2-City.mmdb")
    reader = geoip2.database.Reader(path)
except IOError as e:
    logging.error(
        "Could not open GeoLite2-City.mmdb. Will proceed without GeoIP data")
    logging.error(e)
    reader = False




class ProcessIp(ScheduledAnalytics):

    default_values = {
        "frequency": timedelta(hours=1),
        "name": "ProcessIp",
        "description": "Extracts information from IP addresses",
    }

    ACTS_ON = 'Ip'
    EXPIRATION = None  # only run this once

    @staticmethod
    def each(ip):
        try:
            if reader:
                response = reader.city(ip.value)
                ip.geoip = {
                    "country": response.country.iso_code,
                    "city": response.city.name
                }
            ip.save()
        except ObservableValidationError:
            logging.error("An error occurred when trying to add {} to the database".format(ip.value))
        except AddressNotFoundError:
            logging.error("{} was not found in the GeoIp database".format(ip.value))

@@ -429,21 +429,20 @@
 logging.
-error
+info
 (%0A      
@@ -525,21 +525,20 @@
 logging.
-error
+info
 (e)%0A    

Change log level