# 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
    # NOTE: these work up to ES_6
    MAPPINGS = {
        "mappings": {
            "aggregation-bookmark": {
                "date_detection": False,
                "properties": {
                    "date": {
                        "type": "date",
                        "format": "date_optional_time"
                    },
                    "aggregation_type": {
                        "type": "keyword"
                    }
                }
            }
        }
    }

    # NOTE: For ES7 mappings need one-level of less nesting
    MAPPINGS_ES7 = {
        "mappings": deepcopy(MAPPINGS['mappings']['aggregation-bookmark'])
    }

    def __init__(self, client, agg_type, agg_interval):
        """Construct bookmark instance.

        :param client: elasticsearch client
        :param agg_type: aggregation type for the bookmark
        """
        # NOTE: doc_type is going to be deprecated with ES_7

# 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

    # NOTE: these work up to ES_6
    MAPPINGS = {
        "mappings": {
            "aggregation-bookmark": {
                "date_detection": False,
                "properties": {
                    "date": {
                        "type": "date",
                        "format": "date_optional_time"
                    },
                    "aggregation_type": {
                        "type": "keyword"
                    }
                }
            }
        }
    }

    # NOTE: For ES7 mappings need one-level of less nesting
    MAPPINGS_ES7 = {
        "mappings": deepcopy(MAPPINGS['mappings']['aggregation-bookmark'])
    }

    def __init__(self, client, agg_type, agg_interval):
        """Construct bookmark instance.

        :param client: elasticsearch client
        :param agg_type: aggregation type for the bookmark
        """
        # NOTE: doc_type is going to be deprecated with ES_7

        self.doc_type = get_doctype('aggregation-bookmark')