# Path: invenio_stats/proxies.py

# 
# Path: invenio_stats/tasks.py
# @shared_task
# def aggregate_events(aggregations, start_date=None, end_date=None,
#                      update_bookmark=True):
#     """Aggregate indexed events."""
#     start_date = dateutil_parse(start_date) if start_date else None
#     end_date = dateutil_parse(end_date) if end_date else None
#     results = []
#     for aggr_name in aggregations:
#         aggr_cfg = current_stats.aggregations[aggr_name]
#         aggregator = aggr_cfg.cls(name=aggr_cfg.name, **aggr_cfg.params)
#         results.append(aggregator.run(start_date, end_date, update_bookmark))
#     return results
# 
# @shared_task
# def process_events(event_types):
#     """Index statistics events."""
#     results = []
#     for event_name in event_types:
#         event_cfg = current_stats.events[event_name]
#         processor = event_cfg.cls(**event_cfg.params)
#         results.append((event_name, processor.run()))
#     return results


from functools import wraps
from dateutil.parser import parse as dateutil_parse
from flask.cli import with_appcontext
from werkzeug.local import LocalProxy
from .proxies import current_stats
from .tasks import aggregate_events, process_events
import click
    # to celery
    event_types = list(event_types or current_stats.events)
    process_task = process_events.si(event_types)
    if eager:
        process_task.apply(throw=True)
        click.secho('Events processed successfully.', fg='green')
    else:
        process_task.delay()
        click.secho('Events processing task sent...', fg='yellow')


@stats.group()
def aggregations():
    """Aggregation management commands."""


@aggregations.command('process')
@aggr_arg
@click.option('--start-date', callback=_parse_date)
@click.option('--end-date', callback=_parse_date)
@click.option('--update-bookmark', '-b', is_flag=True)
@click.option('--eager', '-e', is_flag=True)
@with_appcontext
def _aggregations_process(aggregation_types=None,
                          start_date=None, end_date=None,
                          update_bookmark=False, eager=False):
    """Process stats aggregations."""
    # NOTE: aggregation_types is a LocalProxy so it needs to be casted to be
    # passed to celery
    aggregation_types = list(aggregation_types or current_stats.aggregations)

# Path: invenio_stats/proxies.py

# 
# Path: invenio_stats/tasks.py
# @shared_task
# def aggregate_events(aggregations, start_date=None, end_date=None,
#                      update_bookmark=True):
#     """Aggregate indexed events."""
#     start_date = dateutil_parse(start_date) if start_date else None
#     end_date = dateutil_parse(end_date) if end_date else None
#     results = []
#     for aggr_name in aggregations:
#         aggr_cfg = current_stats.aggregations[aggr_name]
#         aggregator = aggr_cfg.cls(name=aggr_cfg.name, **aggr_cfg.params)
#         results.append(aggregator.run(start_date, end_date, update_bookmark))
#     return results
# 
# @shared_task
# def process_events(event_types):
#     """Index statistics events."""
#     results = []
#     for event_name in event_types:
#         event_cfg = current_stats.events[event_name]
#         processor = event_cfg.cls(**event_cfg.params)
#         results.append((event_name, processor.run()))
#     return results


# Path: invenio_stats/cli.py
from functools import wraps
from dateutil.parser import parse as dateutil_parse
from flask.cli import with_appcontext
from werkzeug.local import LocalProxy
from .proxies import current_stats
from .tasks import aggregate_events, process_events
import click

    # to celery
    event_types = list(event_types or current_stats.events)
    process_task = process_events.si(event_types)
    if eager:
        process_task.apply(throw=True)
        click.secho('Events processed successfully.', fg='green')
    else:
        process_task.delay()
        click.secho('Events processing task sent...', fg='yellow')


@stats.group()
def aggregations():
    """Aggregation management commands."""


@aggregations.command('process')
@aggr_arg
@click.option('--start-date', callback=_parse_date)
@click.option('--end-date', callback=_parse_date)
@click.option('--update-bookmark', '-b', is_flag=True)
@click.option('--eager', '-e', is_flag=True)
@with_appcontext
def _aggregations_process(aggregation_types=None,
                          start_date=None, end_date=None,
                          update_bookmark=False, eager=False):
    """Process stats aggregations."""
    # NOTE: aggregation_types is a LocalProxy so it needs to be casted to be
    # passed to celery
    aggregation_types = list(aggregation_types or current_stats.aggregations)

    agg_task = aggregate_events.si(