# Path: invenio_stats/utils.py
# def get_user():
#     """User information.
# 
#     .. note::
# 
#        **Privacy note** A users IP address, user agent string, and user id
#        (if logged in) is sent to a message queue, where it is stored for about
#        5 minutes. The information is used to:
# 
#        - Detect robot visits from the user agent string.
#        - Generate an anonymized visitor id (using a random salt per day).
#        - Detect the users host contry based on the IP address.
# 
#        The information is then discarded.
#     """
#     return dict(
#         ip_address=request.remote_addr,
#         user_agent=request.user_agent.string,
#         user_id=(
#             current_user.get_id() if current_user.is_authenticated else None
#         ),
#         session_id=session.get('sid_s')
#     )


import datetime
from flask import request
from ..utils import get_user
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2017-2018 CERN.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""Signal receivers for certain events."""

from __future__ import absolute_import, print_function





def file_download_event_builder(event, sender_app, obj=None, **kwargs):
    """Build a file-download event."""
    event.update(dict(
        # When:
        timestamp=datetime.datetime.utcnow().isoformat(),
        # What:
        bucket_id=str(obj.bucket_id),
        file_id=str(obj.file_id),
        file_key=obj.key,
        size=obj.file.size,
        referrer=request.referrer,
        # Who:

# Path: invenio_stats/utils.py
# def get_user():
#     """User information.
# 
#     .. note::
# 
#        **Privacy note** A users IP address, user agent string, and user id
#        (if logged in) is sent to a message queue, where it is stored for about
#        5 minutes. The information is used to:
# 
#        - Detect robot visits from the user agent string.
#        - Generate an anonymized visitor id (using a random salt per day).
#        - Detect the users host contry based on the IP address.
# 
#        The information is then discarded.
#     """
#     return dict(
#         ip_address=request.remote_addr,
#         user_agent=request.user_agent.string,
#         user_id=(
#             current_user.get_id() if current_user.is_authenticated else None
#         ),
#         session_id=session.get('sid_s')
#     )


# Path: invenio_stats/contrib/event_builders.py
import datetime
from flask import request
from ..utils import get_user

# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2017-2018 CERN.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""Signal receivers for certain events."""

from __future__ import absolute_import, print_function





def file_download_event_builder(event, sender_app, obj=None, **kwargs):
    """Build a file-download event."""
    event.update(dict(
        # When:
        timestamp=datetime.datetime.utcnow().isoformat(),
        # What:
        bucket_id=str(obj.bucket_id),
        file_id=str(obj.file_id),
        file_key=obj.key,
        size=obj.file.size,
        referrer=request.referrer,
        # Who:

        **get_user()