# Path: invenio_stats/utils.py
# def get_bucket_size(client, index, agg_field, start_date=None, end_date=None):
#     """Function to help us define the size for our search query.
# 
#     :param client: search client
#     :param str index: prefixed search index
#     :param str agg_field: aggregation field
#     :param str start_date: string containing a formatted start date
#     :param str end_date: string containing a formatted end date
# 
#     """
#     time_range = {}
#     if start_date is not None:
#         time_range['gte'] = start_date
#     if end_date is not None:
#         time_range['lte'] = end_date
# 
#     search = Search(using=client, index=index)
#     if time_range:
#         search = search.filter('range', timestamp=time_range)
#     search.aggs.metric('unique_values', 'cardinality', field=agg_field)
# 
#     result = search.execute()
#     unique_values = result.aggregations.unique_values.value
# 
#     # NOTE: we increase the count by 10% in order to be safe
#     return int(ceil(unique_values + unique_values * 0.1))
# 
# def get_doctype(doc_type):
#     """Configure doc_type value according to ES version."""
#     return doc_type if ES_VERSION[0] < 7 else '_doc'


from collections import OrderedDict
from copy import deepcopy
from datetime import datetime
from functools import wraps
from dateutil import parser
from dateutil.relativedelta import relativedelta
from elasticsearch import VERSION as ES_VERSION
from elasticsearch.helpers import bulk
from elasticsearch_dsl import Index, Search
from invenio_search import current_search, current_search_client
from invenio_search.utils import prefix_index
from .utils import get_bucket_size, get_doctype
import six
        return parser.parse(result[0]['timestamp'])

    @property
    def doc_type(self):
        """Get document type for the aggregation."""
        return get_doctype('{0}-{1}-aggregation'.format(
            self.event, self.interval))

    def agg_iter(self, lower_limit, upper_limit):
        """Aggregate and return dictionary to be indexed in ES."""
        aggregation_data = {}
        start_date = format_range_dt(lower_limit, self.interval)
        end_date = format_range_dt(upper_limit, self.interval)
        self.agg_query = Search(using=self.client, index=self.event_index) \
            .filter('range', timestamp={
                'gte': start_date,
                'lte': end_date
            })

        # apply query modifiers
        for modifier in self.query_modifiers:
            self.agg_query = modifier(self.agg_query)

        # TODO: remove histogram bucket
        histogram = self.agg_query.aggs.bucket(
            'histogram',
            'date_histogram',
            field='timestamp',
            interval=self.interval
        )

# Path: invenio_stats/utils.py
# def get_bucket_size(client, index, agg_field, start_date=None, end_date=None):
#     """Function to help us define the size for our search query.
# 
#     :param client: search client
#     :param str index: prefixed search index
#     :param str agg_field: aggregation field
#     :param str start_date: string containing a formatted start date
#     :param str end_date: string containing a formatted end date
# 
#     """
#     time_range = {}
#     if start_date is not None:
#         time_range['gte'] = start_date
#     if end_date is not None:
#         time_range['lte'] = end_date
# 
#     search = Search(using=client, index=index)
#     if time_range:
#         search = search.filter('range', timestamp=time_range)
#     search.aggs.metric('unique_values', 'cardinality', field=agg_field)
# 
#     result = search.execute()
#     unique_values = result.aggregations.unique_values.value
# 
#     # NOTE: we increase the count by 10% in order to be safe
#     return int(ceil(unique_values + unique_values * 0.1))
# 
# def get_doctype(doc_type):
#     """Configure doc_type value according to ES version."""
#     return doc_type if ES_VERSION[0] < 7 else '_doc'


# Path: invenio_stats/aggregations.py
from collections import OrderedDict
from copy import deepcopy
from datetime import datetime
from functools import wraps
from dateutil import parser
from dateutil.relativedelta import relativedelta
from elasticsearch import VERSION as ES_VERSION
from elasticsearch.helpers import bulk
from elasticsearch_dsl import Index, Search
from invenio_search import current_search, current_search_client
from invenio_search.utils import prefix_index
from .utils import get_bucket_size, get_doctype
import six

        return parser.parse(result[0]['timestamp'])

    @property
    def doc_type(self):
        """Get document type for the aggregation."""
        return get_doctype('{0}-{1}-aggregation'.format(
            self.event, self.interval))

    def agg_iter(self, lower_limit, upper_limit):
        """Aggregate and return dictionary to be indexed in ES."""
        aggregation_data = {}
        start_date = format_range_dt(lower_limit, self.interval)
        end_date = format_range_dt(upper_limit, self.interval)
        self.agg_query = Search(using=self.client, index=self.event_index) \
            .filter('range', timestamp={
                'gte': start_date,
                'lte': end_date
            })

        # apply query modifiers
        for modifier in self.query_modifiers:
            self.agg_query = modifier(self.agg_query)

        # TODO: remove histogram bucket
        histogram = self.agg_query.aggs.bucket(
            'histogram',
            'date_histogram',
            field='timestamp',
            interval=self.interval
        )

        bucket_size = get_bucket_size(