Beispiel #1
0
def get_form_duration_stats_by_user(
        domain,
        app_id,
        xmlns,
        user_ids,
        startdate,
        enddate,
        by_submission_time=True):
    """Gets stats on the duration of a selected form grouped by users"""
    date_filter_fn = submitted_filter if by_submission_time else completed_filter

    missing_users = None in user_ids

    query = (
        FormES()
        .domain(domain)
        .user_ids_handle_unknown(user_ids)
        .remove_default_filter('has_user')
        .xmlns(xmlns)
        .filter(date_filter_fn(gte=startdate, lt=enddate))
        .aggregation(
            TermsAggregation('user_id', 'form.meta.userID').aggregation(
                ExtendedStatsAggregation(
                    'duration_stats',
                    'form.meta.timeStart',
                    script="doc['form.meta.timeEnd'].value - doc['form.meta.timeStart'].value",
                )
            )
        )
        .size(0)
    )

    if app_id:
        query = query.app(app_id)

    if missing_users:
        query = query.aggregation(
            MissingAggregation('missing_user_id', 'form.meta.userID').aggregation(
                ExtendedStatsAggregation(
                    'duration_stats',
                    'form.meta.timeStart',
                    script="doc['form.meta.timeEnd'].value - doc['form.meta.timeStart'].value",
                )
            )
        )

    result = {}
    aggregations = query.run().aggregations

    if missing_users:
        result[MISSING_KEY] = aggregations.missing_user_id.bucket.duration_stats.result

    buckets_dict = aggregations.user_id.buckets_dict
    for user_id, bucket in buckets_dict.iteritems():
        result[user_id] = bucket.duration_stats.result
    return result
Beispiel #2
0
def get_form_duration_stats_for_users(domain,
                                      app_id,
                                      xmlns,
                                      user_ids,
                                      startdate,
                                      enddate,
                                      by_submission_time=True):
    """Gets the form duration stats for a group of users"""
    date_filter_fn = submitted_filter if by_submission_time else completed_filter

    query = (FormES().domain(domain).user_ids_handle_unknown(
        user_ids
    ).remove_default_filter('has_user').xmlns(xmlns).filter(
        date_filter_fn(gte=startdate, lt=enddate)
    ).aggregation(
        ExtendedStatsAggregation(
            'duration_stats',
            'form.meta.timeStart',
            script=
            "doc['form.meta.timeEnd'].value - doc['form.meta.timeStart'].value",
        )).size(0))

    if app_id:
        query = query.app(app_id)

    return query.run().aggregations.duration_stats.result
 def test_extended_stats_aggregation(self):
     json_output = {
         "query": {
             "filtered": {
                 "filter": {
                     "and": [{
                         "match_all": {}
                     }]
                 },
                 "query": {
                     "match_all": {}
                 }
             }
         },
         "aggs": {
             "name_stats": {
                 "extended_stats": {
                     "field": "name",
                     "script": "MY weird script"
                 }
             },
         },
         "size": SIZE_LIMIT
     }
     query = HQESQuery('cases').aggregation(
         ExtendedStatsAggregation('name_stats',
                                  'name',
                                  script='MY weird script'))
     self.checkQuery(query, json_output)