# Path: user_metrics/etl/aggregator.py
# METRIC_AGG_METHOD_FLAG = 'metric_agg_flag'
# 
# METRIC_AGG_METHOD_NAME = 'metric_agg_name'
# 
# METRIC_AGG_METHOD_HEAD = 'metric_agg_head'
# 
# METRIC_AGG_METHOD_KWARGS = 'metric_agg_kwargs'
# 
# Path: user_metrics/etl/aggregator.py
# def decorator_builder(header):
#     """
#         Decorator method to annotate aggregation methods to ensure the correct
#         data model is exposed by::
# 
#             metric_agg = decorator_builder(Metric.header())(metric_agg)
#     """
#     def eval_data_model(f):
#         def wrapper(metric, **kwargs):
#             if hasattr(metric, 'header'):
#                 header_arg = metric.header()
#                 if all(
#                         header_arg[i] == header[i]
#                         for i in range(len(header)-1)):
#                     return f(metric, **kwargs)
#             else:
#                 raise AggregatorError('This aggregator (%s) does not operate '
#                                       'on this data type.' % f.__name__)
#         return wrapper
#     return eval_data_model
# 
# def boolean_rate(iter, **kwargs):
#     """
#         Computes the fraction of rows meeting a comparison criteria defined
#         by `cmp_method`.  The index containing the value is passed as a kwarg
#         (`bool_idx`).
# 
#         Useful aggregator for boolean metrics: threshold, survival,
#                 live_accounts
#     """
# 
#     def cmp_method_default(x):
#         return x > 0
# 
#     val_idx = kwargs['val_idx'] if 'val_idx' in kwargs else 1
#     cmp_method = kwargs['cmp_method'] if 'cmp_method' in kwargs \
#         else cmp_method_default
# 
#     total = 0
#     pos = 0
#     for r in iter.__iter__():
#         try:
#             if cmp_method(r[val_idx]):
#                 pos += 1
#             total += 1
#         except IndexError:
#             continue
#         except TypeError:
#             continue
#     if total:
#         return [total, pos, float(pos) / total]
#     else:
#         return [total, pos, 0.0]


from user_metrics.config import logging
from user_metrics.etl.aggregator import METRIC_AGG_METHOD_FLAG,\
    METRIC_AGG_METHOD_NAME, METRIC_AGG_METHOD_HEAD, METRIC_AGG_METHOD_KWARGS
from collections import namedtuple
from os import getpid
from dateutil.parser import parse as date_parse
from datetime import datetime
from user_metrics.etl.aggregator import decorator_builder, boolean_rate
from user_metrics.metrics import query_mod
import user_metric as um
import user_metrics.utils.multiprocessing_wrapper as mpw
        except Exception:
            continue

        if diff <= thread_args.t:
            results[user] = 1
        else:
            results[user] = 0

    return [(str(key), results[key]) for key in results]


@decorator_builder(LiveAccount.header())
def live_accounts_agg(metric):
    """ Computes the fraction of editors that have "live" accounts """
    total = 0
    pos = 0
    for r in metric.__iter__():
        try:
            if r[1]:
                pos += 1
            total += 1
        except (IndexError, TypeError):
            continue
    if total:
        return [total, pos, float(pos) / total]
    else:
        return [total, pos, 0.0]

# Build "rate" decorator
live_accounts_agg = boolean_rate

# Path: user_metrics/etl/aggregator.py
# METRIC_AGG_METHOD_FLAG = 'metric_agg_flag'
# 
# METRIC_AGG_METHOD_NAME = 'metric_agg_name'
# 
# METRIC_AGG_METHOD_HEAD = 'metric_agg_head'
# 
# METRIC_AGG_METHOD_KWARGS = 'metric_agg_kwargs'
# 
# Path: user_metrics/etl/aggregator.py
# def decorator_builder(header):
#     """
#         Decorator method to annotate aggregation methods to ensure the correct
#         data model is exposed by::
# 
#             metric_agg = decorator_builder(Metric.header())(metric_agg)
#     """
#     def eval_data_model(f):
#         def wrapper(metric, **kwargs):
#             if hasattr(metric, 'header'):
#                 header_arg = metric.header()
#                 if all(
#                         header_arg[i] == header[i]
#                         for i in range(len(header)-1)):
#                     return f(metric, **kwargs)
#             else:
#                 raise AggregatorError('This aggregator (%s) does not operate '
#                                       'on this data type.' % f.__name__)
#         return wrapper
#     return eval_data_model
# 
# def boolean_rate(iter, **kwargs):
#     """
#         Computes the fraction of rows meeting a comparison criteria defined
#         by `cmp_method`.  The index containing the value is passed as a kwarg
#         (`bool_idx`).
# 
#         Useful aggregator for boolean metrics: threshold, survival,
#                 live_accounts
#     """
# 
#     def cmp_method_default(x):
#         return x > 0
# 
#     val_idx = kwargs['val_idx'] if 'val_idx' in kwargs else 1
#     cmp_method = kwargs['cmp_method'] if 'cmp_method' in kwargs \
#         else cmp_method_default
# 
#     total = 0
#     pos = 0
#     for r in iter.__iter__():
#         try:
#             if cmp_method(r[val_idx]):
#                 pos += 1
#             total += 1
#         except IndexError:
#             continue
#         except TypeError:
#             continue
#     if total:
#         return [total, pos, float(pos) / total]
#     else:
#         return [total, pos, 0.0]


# Path: user_metrics/metrics/live_account.py
from user_metrics.config import logging
from user_metrics.etl.aggregator import METRIC_AGG_METHOD_FLAG,\
    METRIC_AGG_METHOD_NAME, METRIC_AGG_METHOD_HEAD, METRIC_AGG_METHOD_KWARGS
from collections import namedtuple
from os import getpid
from dateutil.parser import parse as date_parse
from datetime import datetime
from user_metrics.etl.aggregator import decorator_builder, boolean_rate
from user_metrics.metrics import query_mod
import user_metric as um
import user_metrics.utils.multiprocessing_wrapper as mpw

        except Exception:
            continue

        if diff <= thread_args.t:
            results[user] = 1
        else:
            results[user] = 0

    return [(str(key), results[key]) for key in results]


@decorator_builder(LiveAccount.header())
def live_accounts_agg(metric):
    """ Computes the fraction of editors that have "live" accounts """
    total = 0
    pos = 0
    for r in metric.__iter__():
        try:
            if r[1]:
                pos += 1
            total += 1
        except (IndexError, TypeError):
            continue
    if total:
        return [total, pos, float(pos) / total]
    else:
        return [total, pos, 0.0]

# Build "rate" decorator
live_accounts_agg = boolean_rate

live_accounts_agg = decorator_builder(LiveAccount.header())(live_accounts_agg)