# Path: invenio_stats/utils.py
# def get_anonymization_salt(ts):
#     """Get the anonymization salt based on the event timestamp's day."""
#     salt_key = 'stats:salt:{}'.format(ts.date().isoformat())
#     salt = current_cache.get(salt_key)
#     if not salt:
#         salt_bytes = os.urandom(32)
#         salt = b64encode(salt_bytes).decode('utf-8')
#         current_cache.set(salt_key, salt, timeout=60 * 60 * 24)
#     return salt
# 
# def get_doctype(doc_type):
#     """Configure doc_type value according to ES version."""
#     return doc_type if ES_VERSION[0] < 7 else '_doc'
# 
# def get_geoip(ip):
#     """Lookup country for IP address."""
#     reader = geolite2.reader()
#     ip_data = reader.get(ip) or {}
#     return ip_data.get('country', {}).get('iso_code')
# 
# def obj_or_import_string(value, default=None):
#     """Import string or return object.
# 
#     :params value: Import path or class object to instantiate.
#     :params default: Default object to return if the import fails.
#     :returns: The imported object.
#     """
#     if isinstance(value, six.string_types):
#         return import_string(value)
#     elif value:
#         return value
#     return default


import hashlib
import elasticsearch
from time import mktime
from counter_robots import is_machine, is_robot
from dateutil import parser
from flask import current_app
from invenio_search import current_search_client
from invenio_search.utils import prefix_index
from pytz import utc
from .utils import get_anonymization_salt, get_doctype, get_geoip, \
    obj_or_import_string
    """Simple events indexer.

    Subclass this class in order to provide custom indexing behaviour.
    """

    default_preprocessors = [flag_robots, anonymize_user]
    """Default preprocessors ran on every event."""

    def __init__(self, queue, prefix='events', suffix='%Y-%m-%d', client=None,
                 preprocessors=None, double_click_window=10):
        """Initialize indexer.

        :param prefix: prefix appended to elasticsearch indices' name.
        :param suffix: suffix appended to elasticsearch indices' name.
        :param double_click_window: time window during which similar events are
            deduplicated (counted as one occurence).
        :param client: elasticsearch client.
        :param preprocessors: a list of functions which are called on every
            event before it is indexed. Each function should return the
            processed event. If it returns None, the event is filtered and
            won't be indexed.
        """
        self.queue = queue
        self.client = client or current_search_client
        self.doctype = get_doctype(queue.routing_key)
        self.index = prefix_index('{0}-{1}'.format(
            prefix, self.queue.routing_key))
        self.suffix = suffix
        # load the preprocessors
        self.preprocessors = [

# Path: invenio_stats/utils.py
# def get_anonymization_salt(ts):
#     """Get the anonymization salt based on the event timestamp's day."""
#     salt_key = 'stats:salt:{}'.format(ts.date().isoformat())
#     salt = current_cache.get(salt_key)
#     if not salt:
#         salt_bytes = os.urandom(32)
#         salt = b64encode(salt_bytes).decode('utf-8')
#         current_cache.set(salt_key, salt, timeout=60 * 60 * 24)
#     return salt
# 
# def get_doctype(doc_type):
#     """Configure doc_type value according to ES version."""
#     return doc_type if ES_VERSION[0] < 7 else '_doc'
# 
# def get_geoip(ip):
#     """Lookup country for IP address."""
#     reader = geolite2.reader()
#     ip_data = reader.get(ip) or {}
#     return ip_data.get('country', {}).get('iso_code')
# 
# def obj_or_import_string(value, default=None):
#     """Import string or return object.
# 
#     :params value: Import path or class object to instantiate.
#     :params default: Default object to return if the import fails.
#     :returns: The imported object.
#     """
#     if isinstance(value, six.string_types):
#         return import_string(value)
#     elif value:
#         return value
#     return default


# Path: invenio_stats/processors.py
import hashlib
import elasticsearch
from time import mktime
from counter_robots import is_machine, is_robot
from dateutil import parser
from flask import current_app
from invenio_search import current_search_client
from invenio_search.utils import prefix_index
from pytz import utc
from .utils import get_anonymization_salt, get_doctype, get_geoip, \
    obj_or_import_string

    """Simple events indexer.

    Subclass this class in order to provide custom indexing behaviour.
    """

    default_preprocessors = [flag_robots, anonymize_user]
    """Default preprocessors ran on every event."""

    def __init__(self, queue, prefix='events', suffix='%Y-%m-%d', client=None,
                 preprocessors=None, double_click_window=10):
        """Initialize indexer.

        :param prefix: prefix appended to elasticsearch indices' name.
        :param suffix: suffix appended to elasticsearch indices' name.
        :param double_click_window: time window during which similar events are
            deduplicated (counted as one occurence).
        :param client: elasticsearch client.
        :param preprocessors: a list of functions which are called on every
            event before it is indexed. Each function should return the
            processed event. If it returns None, the event is filtered and
            won't be indexed.
        """
        self.queue = queue
        self.client = client or current_search_client
        self.doctype = get_doctype(queue.routing_key)
        self.index = prefix_index('{0}-{1}'.format(
            prefix, self.queue.routing_key))
        self.suffix = suffix
        # load the preprocessors
        self.preprocessors = [

            obj_or_import_string(preproc) for preproc in preprocessors