Ejemplo n.º 1
0
def active_mobile_users(domain, *args):
    """
    Returns the number of mobile users who have submitted a form or SMS in the
    last 30 days
    """
    now = datetime.utcnow()
    then = (now - timedelta(days=30))

    user_ids = get_mobile_users(domain)

    form_users = {q['term'] for q in (
        FormES()
        .domain(domain)
        .user_facet(size=USER_COUNT_UPPER_BOUND)
        .submitted(gte=then)
        .user_id(user_ids)
        .size(0)
        .run()
        .facets.user.result
    )}

    sms_users = {q['term'] for q in (
        SMSES()
        .user_facet(size=USER_COUNT_UPPER_BOUND)
        .to_commcare_user()
        .domain(domain)
        .received(gte=then)
        .size(0)
        .run()
        .facets.user.result
    )}

    num_users = len(form_users | sms_users)
    return num_users if 'inactive' not in args else len(user_ids) - num_users
Ejemplo n.º 2
0
def active_mobile_users(domain, *args):
    """
    Returns the number of mobile users who have submitted a form or SMS in the
    last 30 days
    """
    now = datetime.utcnow()
    then = (now - timedelta(days=30))

    user_ids = get_mobile_users(domain)

    form_users = set(
        FormES()
        .domain(domain)
        .user_aggregation()
        .submitted(gte=then)
        .user_id(user_ids)
        .size(0)
        .run()
        .aggregations.user.keys
    )

    sms_users = set(
        SMSES()
        .incoming_messages()
        .user_aggregation()
        .to_commcare_user()
        .domain(domain)
        .received(gte=then)
        .size(0)
        .run()
        .aggregations.user.keys
    )

    num_users = len(form_users | sms_users)
    return num_users if 'inactive' not in args else len(user_ids) - num_users
Ejemplo n.º 3
0
def active_mobile_users(domain, *args):
    """
    Returns the number of mobile users who have submitted a form or SMS in the
    last 30 days
    """
    now = datetime.utcnow()
    then = (now - timedelta(days=30))

    user_ids = get_mobile_users(domain)

    form_users = set(
        FormES()
        .domain(domain)
        .user_aggregation()
        .submitted(gte=then)
        .user_id(user_ids)
        .size(0)
        .run()
        .aggregations.user.keys
    )

    sms_users = set(
        SMSES()
        .incoming_messages()
        .user_aggregation()
        .to_commcare_user()
        .domain(domain)
        .received(gte=then)
        .size(0)
        .run()
        .aggregations.user.keys
    )

    num_users = len(form_users | sms_users)
    return num_users if 'inactive' not in args else len(user_ids) - num_users
Ejemplo n.º 4
0
def active_mobile_users(domain, start, end, *args):
    """
    Returns the number of mobile users who have submitted a form or SMS in the
    time specified
    """

    user_ids = get_mobile_users(domain.name)

    form_users = (FormES()
                  .domain(domain.name)
                  .user_aggregation()
                  .submitted(gte=start, lt=end)
                  .user_id(user_ids)
                  .size(0)
                  .run()
                  .aggregations.user.counts_by_bucket())

    sms_users = set(
        SMSES()
        .incoming_messages()
        .user_aggregation()
        .to_commcare_user()
        .domain(domain.name)
        .received(gte=start, lt=end)
        .size(0)
        .run()
        .aggregations.user.keys
    )

    return set(user_ids), form_users, sms_users
Ejemplo n.º 5
0
def active_mobile_users(domain, *args):
    """
    Returns the number of mobile users who have submitted a form or SMS in the
    last 30 days
    """
    now = datetime.utcnow()
    then = (now - timedelta(days=30))

    user_ids = get_mobile_users(domain)

    form_users = {
        q['term']
        for q in (FormES().domain(domain).user_facet(
            size=USER_COUNT_UPPER_BOUND).submitted(
                gte=then).user_id(user_ids).size(0).run().facets.user.result)
    }

    sms_users = {
        q['term']
        for q in (SMSES().user_facet(
            size=USER_COUNT_UPPER_BOUND).to_commcare_user().domain(
                domain).received(gte=then).size(0).run().facets.user.result)
    }

    num_users = len(form_users | sms_users)
    return num_users if 'inactive' not in args else len(user_ids) - num_users
Ejemplo n.º 6
0
def get_possibly_experienced(domain, start):

    user_ids = get_mobile_users(domain.name)
    threshold = domain.internal.experienced_threshold or DEFAULT_EXPERIENCED_THRESHOLD
    months = threshold - 2
    threshold_month = add_months(start.startdate.year, start.startdate.month,
                                 -months)
    end_month = datetime.date(day=1,
                              year=threshold_month[0],
                              month=threshold_month[1])

    form_users = set(FormES().domain(domain.name).user_aggregation().submitted(
        lt=end_month).user_id(user_ids).size(0).run().aggregations.user.keys)

    return set(form_users)
Ejemplo n.º 7
0
def active_mobile_users(domain, start, end, *args):
    """
    Returns the number of mobile users who have submitted a form or SMS in the
    time specified
    """

    user_ids = get_mobile_users(domain.name)

    form_users = (FormES().domain(domain.name).user_aggregation().submitted(
        gte=start, lt=end).user_id(user_ids).size(
            0).run().aggregations.user.counts_by_bucket())

    sms_users = set(SMSES().incoming_messages().user_aggregation(
    ).to_commcare_user().domain(domain.name).received(
        gte=start, lt=end).size(0).run().aggregations.user.keys)

    return set(user_ids), form_users, sms_users
Ejemplo n.º 8
0
def get_possibly_experienced(domain, start):

    user_ids = get_mobile_users(domain.name)
    threshold = domain.internal.experienced_threshold or DEFAULT_EXPERIENCED_THRESHOLD
    months = threshold - 2
    threshold_month = add_months(start.startdate.year, start.startdate.month, -months)
    end_month = datetime.date(day=1, year=threshold_month[0], month=threshold_month[1])

    form_users = set(
        FormES()
        .domain(domain.name)
        .user_aggregation()
        .submitted(lt=end_month)
        .user_id(user_ids)
        .size(0)
        .run()
        .aggregations.user.keys
    )

    return set(form_users)