# Path: zenodo/modules/metrics/api.py
# class ZenodoMetric(object):
#     """API class for Zenodo Metrics."""
# 
#     @staticmethod
#     def get_data_transfer():
#         """Get file transfer volume in TB."""
#         time_range = {'gte': current_metrics.metrics_start_date.isoformat()}
# 
#         search = Search(
#             using=current_search_client,
#             index=build_alias_name('stats-file-download-*')
#         ).filter(
#             'range', timestamp=time_range,
#         ).filter(
#             'term', is_parent=False,
#         )
#         search.aggs.metric('download_volume', 'sum', field='volume')
#         result = search[:0].execute().aggregations.to_dict()
#         download_volume = result.get('download_volume', {}).get('value', 0)
# 
#         search = Search(
#             using=current_search_client,
#             index=build_alias_name('records')
#         ).filter('range', created=time_range)
#         search.aggs.metric('upload_volume', 'sum', field='size')
#         result = search[:0].execute().aggregations.to_dict()
#         upload_volume = result.get('upload_volume', {}).get('value', 0)
# 
#         return int(download_volume + upload_volume)
# 
#     @staticmethod
#     def get_visitors():
#         """Get number of unique zenodo users."""
#         time_range = {'gte': current_metrics.metrics_start_date.isoformat()}
# 
#         search = Search(
#             using=current_search_client,
#             index=build_alias_name('events-stats-*')
#         ).filter('range', timestamp=time_range)
# 
#         search.aggs.metric(
#             'visitors_count', 'cardinality', field='visitor_id'
#         )
#         result = search[:0].execute()
# 
#         if 'visitors_count' not in result.aggregations:
#             return 0
# 
#         return int(result.aggregations.visitors_count.value)
# 
#     @staticmethod
#     def get_uptime():
#         """Get Zenodo uptime."""
#         metrics = current_app.config['ZENODO_METRICS_UPTIME_ROBOT_METRIC_IDS']
#         url = current_app.config['ZENODO_METRICS_UPTIME_ROBOT_URL']
#         api_key = current_app.config['ZENODO_METRICS_UPTIME_ROBOT_API_KEY']
# 
#         end = datetime.utcnow().replace(
#             day=1, hour=0, minute=0, second=0, microsecond=0)
#         start = (end - timedelta(days=1)).replace(day=1)
#         end_ts = calendar.timegm(end.utctimetuple())
#         start_ts = calendar.timegm(start.utctimetuple())
# 
#         res = requests.post(url, json={
#             'api_key': api_key,
#             'custom_uptime_ranges': '{}_{}'.format(start_ts, end_ts),
#         })
# 
#         return sum(
#             float(d['custom_uptime_ranges'])
#             for d in res.json()['monitors']
#             if d['id'] in metrics
#         ) / len(metrics)
# 
#     @staticmethod
#     def get_researchers():
#         """Get number of unique zenodo users."""
#         return User.query.filter(
#             User.confirmed_at.isnot(None),
#             User.active.is_(True),
#         ).count()
# 
#     @staticmethod
#     def get_files():
#         """Get number of files."""
#         return FileInstance.query.count()
# 
#     @staticmethod
#     def get_communities():
#         """Get number of active communities."""
#         return Community.query.filter(
#             Community.deleted_at.is_(None)
#         ).count()


import datetime
from .api import ZenodoMetric
            'name': 'zenodo_nexus_unique_visitors_web_total',
            'help': (
                'Total of daily unique visitors on Zenodo portal during the '
                'OpenAIRE-NEXUS project (i.e. from 2021-01-01).'
            ),
            'type': 'counter',
            'value': ZenodoMetric.get_visitors
        },
        {
            'name': 'zenodo_last_month_uptime_ratio',
            'help': 'Zenodo uptime percentage for the last month.',
            'type': 'gauge',
            'value': ZenodoMetric.get_uptime
        },
        {
            'name': 'zenodo_researchers',
            'help': 'Number of researchers registered on Zenodo',
            'type': 'gauge',
            'value': ZenodoMetric.get_researchers
        },
        {
            'name': 'zenodo_files',
            'help': 'Number of files hosted on Zenodo',
            'type': 'gauge',
            'value': ZenodoMetric.get_files
        },
        {
            'name': 'zenodo_communities',
            'help': 'Number of Zenodo communities created',
            'type': 'gauge',

# Path: zenodo/modules/metrics/api.py
# class ZenodoMetric(object):
#     """API class for Zenodo Metrics."""
# 
#     @staticmethod
#     def get_data_transfer():
#         """Get file transfer volume in TB."""
#         time_range = {'gte': current_metrics.metrics_start_date.isoformat()}
# 
#         search = Search(
#             using=current_search_client,
#             index=build_alias_name('stats-file-download-*')
#         ).filter(
#             'range', timestamp=time_range,
#         ).filter(
#             'term', is_parent=False,
#         )
#         search.aggs.metric('download_volume', 'sum', field='volume')
#         result = search[:0].execute().aggregations.to_dict()
#         download_volume = result.get('download_volume', {}).get('value', 0)
# 
#         search = Search(
#             using=current_search_client,
#             index=build_alias_name('records')
#         ).filter('range', created=time_range)
#         search.aggs.metric('upload_volume', 'sum', field='size')
#         result = search[:0].execute().aggregations.to_dict()
#         upload_volume = result.get('upload_volume', {}).get('value', 0)
# 
#         return int(download_volume + upload_volume)
# 
#     @staticmethod
#     def get_visitors():
#         """Get number of unique zenodo users."""
#         time_range = {'gte': current_metrics.metrics_start_date.isoformat()}
# 
#         search = Search(
#             using=current_search_client,
#             index=build_alias_name('events-stats-*')
#         ).filter('range', timestamp=time_range)
# 
#         search.aggs.metric(
#             'visitors_count', 'cardinality', field='visitor_id'
#         )
#         result = search[:0].execute()
# 
#         if 'visitors_count' not in result.aggregations:
#             return 0
# 
#         return int(result.aggregations.visitors_count.value)
# 
#     @staticmethod
#     def get_uptime():
#         """Get Zenodo uptime."""
#         metrics = current_app.config['ZENODO_METRICS_UPTIME_ROBOT_METRIC_IDS']
#         url = current_app.config['ZENODO_METRICS_UPTIME_ROBOT_URL']
#         api_key = current_app.config['ZENODO_METRICS_UPTIME_ROBOT_API_KEY']
# 
#         end = datetime.utcnow().replace(
#             day=1, hour=0, minute=0, second=0, microsecond=0)
#         start = (end - timedelta(days=1)).replace(day=1)
#         end_ts = calendar.timegm(end.utctimetuple())
#         start_ts = calendar.timegm(start.utctimetuple())
# 
#         res = requests.post(url, json={
#             'api_key': api_key,
#             'custom_uptime_ranges': '{}_{}'.format(start_ts, end_ts),
#         })
# 
#         return sum(
#             float(d['custom_uptime_ranges'])
#             for d in res.json()['monitors']
#             if d['id'] in metrics
#         ) / len(metrics)
# 
#     @staticmethod
#     def get_researchers():
#         """Get number of unique zenodo users."""
#         return User.query.filter(
#             User.confirmed_at.isnot(None),
#             User.active.is_(True),
#         ).count()
# 
#     @staticmethod
#     def get_files():
#         """Get number of files."""
#         return FileInstance.query.count()
# 
#     @staticmethod
#     def get_communities():
#         """Get number of active communities."""
#         return Community.query.filter(
#             Community.deleted_at.is_(None)
#         ).count()


# Path: zenodo/modules/metrics/config.py
import datetime
from .api import ZenodoMetric

            'name': 'zenodo_nexus_unique_visitors_web_total',
            'help': (
                'Total of daily unique visitors on Zenodo portal during the '
                'OpenAIRE-NEXUS project (i.e. from 2021-01-01).'
            ),
            'type': 'counter',
            'value': ZenodoMetric.get_visitors
        },
        {
            'name': 'zenodo_last_month_uptime_ratio',
            'help': 'Zenodo uptime percentage for the last month.',
            'type': 'gauge',
            'value': ZenodoMetric.get_uptime
        },
        {
            'name': 'zenodo_researchers',
            'help': 'Number of researchers registered on Zenodo',
            'type': 'gauge',
            'value': ZenodoMetric.get_researchers
        },
        {
            'name': 'zenodo_files',
            'help': 'Number of files hosted on Zenodo',
            'type': 'gauge',
            'value': ZenodoMetric.get_files
        },
        {
            'name': 'zenodo_communities',
            'help': 'Number of Zenodo communities created',
            'type': 'gauge',

            'value': ZenodoMetric.get_communities