예제 #1
0
def get_real_sms_messages_data(domains, datespan, interval,
        datefield='date', is_commtrack=False, additional_params_es={}):
    """
    Returns SMS sent in timespan.
    Returned based on date SMS was sent
    """
    sms_after_date = (SMSES()
            .domain(domains)
            .received(gte=datespan.startdate, lte=datespan.enddate)
            .date_histogram('date', datefield, interval)
            .size(0))
    if additional_params_es:
        sms_after_date = add_params_to_query(
            sms_after_date,
            additional_params_es
        )
    if is_commtrack:
        sms_after_date = sms_after_date.to_commcare_user_or_case()

    histo_data = sms_after_date.run().aggregations.date.as_facet_result()

    sms_before_date = (SMSES()
            .domain(domains)
            .received(lt=datespan.startdate)
            .size(0))

    if additional_params_es:
        sms_before_date = add_params_to_query(
            sms_before_date,
            additional_params_es
        )
    if is_commtrack:
        sms_before_date = sms_before_date.to_commcare_user_or_case()

    sms_before_date = sms_before_date.run().total

    return format_return_data(histo_data, sms_before_date, datespan)
예제 #2
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
예제 #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
예제 #4
0
    def test_processed_or_incoming(self):
        json_output = {
            "query": {
                "filtered": {
                    "filter": {
                        "and": [
                            {
                                "term": {
                                    "domain.exact": "demo"
                                }
                            },
                            {
                                "not": {
                                    "and": (
                                        {
                                            "term": {
                                                "direction": "o"
                                            }
                                        },
                                        {
                                            "term": {
                                                "processed": False
                                            }
                                        },
                                    )
                                }
                            },
                            {
                                "match_all": {}
                            },
                        ]
                    },
                    "query": {
                        "match_all": {}
                    }
                }
            },
            "size": SIZE_LIMIT
        }

        query = SMSES().domain('demo').processed_or_incoming_messages()

        self.checkQuery(query, json_output)
예제 #5
0
def get_sms_count(domain, direction=None, days=None):
    """
    :param domain: domain name
    :param direction: can specify INCOMING or OUTGOING, or None to retrieve both
    :param days: only return count of sms docs from the past N days
    :return: number of sms docs fetched based on query parameters specified
    """
    assert direction in (INCOMING, OUTGOING, None), repr(direction)
    query = SMSES().domain(domain).size(0)

    if direction == INCOMING:
        query = query.incoming_messages()
    elif direction == OUTGOING:
        query = query.outgoing_messages()

    if days:
        days = int(days) if isinstance(days, str) else days
        query = query.received(date.today() - relativedelta(days=days))

    return query.run().total
예제 #6
0
def get_submitted_users(domains):
    real_form_users = set(
        FormES()
        .user_aggregation()
        .size(0)
    )

    real_sms_users = set(
        SMSES()
        .terms_aggregation('couch_recipient', 'user')
        .incoming_messages()
        .size(0)
    )

    if domains:
        real_form_users = real_form_users.domain(domains)
        real_sms_users = real_sms_users.domain(domains)

    real_form_users = real_form_users.run().aggregations.users.keys
    real_sms_users = real_sms_users.run().aggregations.users.keys

    return real_form_users | real_sms_users
예제 #7
0
def get_sms_only_domain_stats_data(domains, datespan, interval,
        datefield='date_created'):
    """
    Returns domains that have only used SMS and not forms.
    Returned based on date domain is created
    """
    histo_data = []

    sms = (SMSES()
            .domain(domains)
            .terms_facet('domain', 'domains', size=DOMAIN_COUNT_UPPER_BOUND)
            .size(0))
    forms = (FormES()
             .domain(domains)
             .terms_facet('domain', 'domains', size=DOMAIN_COUNT_UPPER_BOUND)
             .size(0))

    sms_domains = {x['term'] for x in sms.run().facet('domains', 'terms')}
    form_domains = {x['term'] for x in forms.run().facet('domains', 'terms')}

    sms_only_domains = sms_domains - form_domains

    domains_after_date = (DomainES()
            .in_domains(sms_only_domains)
            .created(gte=datespan.startdate, lte=datespan.enddate)
            .date_histogram('date', datefield, interval)
            .size(0))

    histo_data = domains_after_date.run().facet('date', 'entries')

    domains_before_date = (DomainES()
            .in_domains(sms_only_domains)
            .created(lt=datespan.startdate)
            .size(0))

    domains_before_date = domains_before_date.run().total
    return format_return_data(histo_data, domains_before_date, datespan)
예제 #8
0
def get_sms_only_domain_stats_data(domains, datespan, interval,
        datefield='date_created'):
    """
    Returns domains that have only used SMS and not forms.
    Returned based on date domain is created
    """
    histo_data = []

    sms = (SMSES()
            .domain(domains)
            .terms_aggregation('domain', 'domains')
            .size(0))
    forms = (FormES()
             .domain(domains)
             .terms_aggregation('domain', 'domains')
             .size(0))

    sms_domains = set(sms.run().aggregations.domains.keys)
    form_domains = set(forms.run().aggregations.domains.keys)

    sms_only_domains = sms_domains - form_domains

    domains_after_date = (DomainES()
            .in_domains(sms_only_domains)
            .created(gte=datespan.startdate, lte=datespan.enddate)
            .date_histogram('date', datefield, interval)
            .size(0))

    histo_data = domains_after_date.run().aggregations.date.as_facet_result()

    domains_before_date = (DomainES()
            .in_domains(sms_only_domains)
            .created(lt=datespan.startdate)
            .size(0))

    domains_before_date = domains_before_date.run().total
    return format_return_data(histo_data, domains_before_date, datespan)
예제 #9
0
 def test_sms_count(self):
     sms_doc = self.create_sms_in_es(self.domain.name, INCOMING)
     self.addCleanup(self.delete_sms_in_es, sms_doc)
     self.assertEqual(SMSES().count(), 1)
     self.assertEqual(sms(self.domain.name, INCOMING), 1)
     self.assertEqual(sms(self.domain.name, OUTGOING), 0)
예제 #10
0
 def test_sms(self):
     self.assertEqual(SMSES().count(), 1)
     self.assertEqual(sms(self.domain.name, INCOMING), 1)
     self.assertEqual(sms(self.domain.name, OUTGOING), 0)
예제 #11
0
def get_sms_query(begin, end, facet_name, facet_terms, domains):
    return (SMSES().domain(domains).received(
        gte=begin, lte=end).terms_aggregation(facet_terms, facet_name).size(0))
예제 #12
0
def get_sms_export_base_query(domain):
    return (SMSES(es_instance_alias=ES_EXPORT_INSTANCE).domain(
        domain).processed_or_incoming_messages().sort("date"))
예제 #13
0
def get_sms_export_base_query(domain):
    return (SMSES(for_export=True).domain(
        domain).processed_or_incoming_messages().sort("date"))