# Path: invenio_stats/errors.py
# class InvalidRequestInputError(RESTException):
#     """Error raised when the request input is invalid."""
# 
#     code = 400
# 
#     def __init__(self, description, **kwargs):
#         """Initialize exception."""
#         super(RESTException, self).__init__(**kwargs)
#         self.description = description
# 
# class UnknownQueryError(RESTException):
#     """Error raised when the request input is invalid."""
# 
#     def __init__(self, query_name):
#         """Constructor.
# 
#         :param query_name: name of the unknown query.
#         """
#         super(RESTException, self).__init__()
#         self.query_name = query_name
#         self.description = 'Unknown statistic \'{}\''.format(query_name)
#     code = 400
# 
# Path: invenio_stats/proxies.py

# 
# Path: invenio_stats/utils.py
# def get_anonymization_salt(ts):
# def get_bucket_size(client, index, agg_field, start_date=None, end_date=None):
# def get_doctype(doc_type):
# def get_geoip(ip):
# def get_user():
# def obj_or_import_string(value, default=None):
# def load_or_import_from_config(key, app=None, default=None):
# def default_permission_factory(query_name, params):


from elasticsearch.exceptions import NotFoundError
from flask import Blueprint, abort, jsonify, request
from invenio_rest.views import ContentNegotiatedMethodView
from .errors import InvalidRequestInputError, UnknownQueryError
from .proxies import current_stats
from .utils import current_user
                'application/json':
                lambda data, *args, **kwargs: jsonify(data),
            },
            default_method_media_type={
                'GET': 'application/json',
            },
            default_media_type='application/json',
            **kwargs)

    def post(self, **kwargs):
        """Get statistics."""
        data = request.get_json(force=False)
        if data is None:
            data = {}

        result = {}
        for query_name, config in data.items():
            if config is None or not isinstance(config, dict) \
                    or (set(config.keys()) != {'stat', 'params'} and
                        set(config.keys()) != {'stat'}):
                raise InvalidRequestInputError(
                    'Invalid Input. It should be of the form '
                    '{ STATISTIC_NAME: { "stat": STAT_TYPE, '
                    '"params": STAT_PARAMS }}'
                )
            stat = config['stat']
            params = config.get('params', {})
            try:
                query_cfg = current_stats.queries[stat]
            except KeyError:

# Path: invenio_stats/errors.py
# class InvalidRequestInputError(RESTException):
#     """Error raised when the request input is invalid."""
# 
#     code = 400
# 
#     def __init__(self, description, **kwargs):
#         """Initialize exception."""
#         super(RESTException, self).__init__(**kwargs)
#         self.description = description
# 
# class UnknownQueryError(RESTException):
#     """Error raised when the request input is invalid."""
# 
#     def __init__(self, query_name):
#         """Constructor.
# 
#         :param query_name: name of the unknown query.
#         """
#         super(RESTException, self).__init__()
#         self.query_name = query_name
#         self.description = 'Unknown statistic \'{}\''.format(query_name)
#     code = 400
# 
# Path: invenio_stats/proxies.py

# 
# Path: invenio_stats/utils.py
# def get_anonymization_salt(ts):
# def get_bucket_size(client, index, agg_field, start_date=None, end_date=None):
# def get_doctype(doc_type):
# def get_geoip(ip):
# def get_user():
# def obj_or_import_string(value, default=None):
# def load_or_import_from_config(key, app=None, default=None):
# def default_permission_factory(query_name, params):


# Path: invenio_stats/views.py
from elasticsearch.exceptions import NotFoundError
from flask import Blueprint, abort, jsonify, request
from invenio_rest.views import ContentNegotiatedMethodView
from .errors import InvalidRequestInputError, UnknownQueryError
from .proxies import current_stats
from .utils import current_user

                'application/json':
                lambda data, *args, **kwargs: jsonify(data),
            },
            default_method_media_type={
                'GET': 'application/json',
            },
            default_media_type='application/json',
            **kwargs)

    def post(self, **kwargs):
        """Get statistics."""
        data = request.get_json(force=False)
        if data is None:
            data = {}

        result = {}
        for query_name, config in data.items():
            if config is None or not isinstance(config, dict) \
                    or (set(config.keys()) != {'stat', 'params'} and
                        set(config.keys()) != {'stat'}):
                raise InvalidRequestInputError(
                    'Invalid Input. It should be of the form '
                    '{ STATISTIC_NAME: { "stat": STAT_TYPE, '
                    '"params": STAT_PARAMS }}'
                )
            stat = config['stat']
            params = config.get('params', {})
            try:
                query_cfg = current_stats.queries[stat]
            except KeyError:

                raise UnknownQueryError(stat)