def get_last_form_submissions_by_user(domain, user_ids, app_id=None, xmlns=None): missing_users = None in user_ids query = ( FormES() .domain(domain) .user_ids_handle_unknown(user_ids) .remove_default_filter('has_user') .aggregation( TermsAggregation('user_id', 'form.meta.userID').aggregation( TopHitsAggregation( 'top_hits_last_form_submissions', 'received_on', is_ascending=False, ) ) ) .size(0) ) if app_id: query = query.app(app_id) if xmlns: query = query.xmlns(xmlns) result = {} if missing_users: query = query.aggregation( MissingAggregation('missing_user_id', 'form.meta.userID').aggregation( TopHitsAggregation( 'top_hits_last_form_submissions', 'received_on', is_ascending=False, ) ) ) aggregations = query.run().aggregations if missing_users: result[MISSING_KEY] = aggregations.missing_user_id.bucket.top_hits_last_form_submissions.hits buckets_dict = aggregations.user_id.buckets_dict for user_id, bucket in six.iteritems(buckets_dict): result[user_id] = bucket.top_hits_last_form_submissions.hits return result
def get_last_submission_time_for_users(domain, user_ids, datespan, es_instance_alias=ES_DEFAULT_INSTANCE): def convert_to_date(date): return string_to_datetime(date).date() if date else None query = (FormES(es_instance_alias=es_instance_alias).domain( domain).user_id(user_ids).completed( gte=datespan.startdate.date(), lte=datespan.enddate.date()).aggregation( TermsAggregation('user_id', 'form.meta.userID').aggregation( TopHitsAggregation( 'top_hits_last_form_submissions', 'form.meta.timeEnd', is_ascending=False, include='form.meta.timeEnd', ))).size(0)) aggregations = query.run().aggregations buckets_dict = aggregations.user_id.buckets_dict result = {} for user_id, bucket in buckets_dict.items(): result[user_id] = convert_to_date( bucket.top_hits_last_form_submissions.hits[0]['form']['meta'] ['timeEnd']) return result
def get_last_forms_by_app(user_id): """ gets the last form submission for each app for a given user id :param user_id: id of a couch user :return: last form submission for every app that user has submitted """ query = ( FormES() .user_id(user_id) .aggregation( TermsAggregation('app_id', 'app_id').aggregation( TopHitsAggregation( 'top_hits_last_form_submissions', 'received_on', is_ascending=False, ) ) ) .size(0) ) aggregations = query.run().aggregations buckets_dict = aggregations.app_id.buckets_dict result = [] for app_id, bucket in buckets_dict.items(): result.append(bucket.top_hits_last_form_submissions.hits[0]) return result
def get_last_submission_time_for_users(domain, user_ids, datespan, for_export=False): def convert_to_date(date): return string_to_datetime(date).date() if date else None query = (FormES( for_export=for_export).domain(domain).user_id(user_ids).submitted( gte=datespan.startdate.date(), lte=datespan.enddate.date()).aggregation( TermsAggregation('user_id', 'form.meta.userID').aggregation( TopHitsAggregation( 'top_hits_last_form_submissions', 'received_on', is_ascending=False, include='received_on', ))).size(0)) aggregations = query.run().aggregations buckets_dict = aggregations.user_id.buckets_dict result = {} for user_id, bucket in buckets_dict.items(): result[user_id] = convert_to_date( bucket.top_hits_last_form_submissions.hits[0]['received_on']) return result
def get_forms_for_users(domain, user_ids, start, end): query = (FormES().domain(domain).submitted( gte=start, lte=end).user_id(user_ids).aggregation( TermsAggregation('user_id', 'form.meta.userID').aggregation( TopHitsAggregation(name='top_hits_user_submissions', size=1000000, include=['form.case', 'form.@xmlns']))).size(0)) aggregations = query.run().aggregations return aggregations.user_id.buckets_dict
def get_latest_build_ids(domain, app_id, user_ids): query = (FormES(es_instance_alias=ES_EXPORT_INSTANCE).domain(domain).app( [app_id]).user_id(user_ids).aggregation( TermsAggregation('user_id', 'form.meta.userID').aggregation( TopHitsAggregation( 'top_hits_last_form_submissions', 'received_on', is_ascending=False, include=['build_id'], ))).size(0)) aggregations = query.run().aggregations buckets_dict = aggregations.user_id.buckets_dict result = {} for user_id, bucket in buckets_dict.items(): result[user_id] = bucket.top_hits_last_form_submissions.hits[0][ 'build_id'] return result
def test_top_hits_aggregation(self): json_output = { "query": { "filtered": { "filter": { "and": [ {"match_all": {}} ] }, "query": {"match_all": {}} } }, "aggs": { "name_top_hits": { "top_hits": { "sort": [{ "my_awesome_field": { "order": "desc" } }], "_source": { "include": [ "title" ] }, "size": 2 }, }, }, "size": SIZE_LIMIT } query = HQESQuery('cases').aggregation( TopHitsAggregation( 'name_top_hits', field='my_awesome_field', is_ascending=False, size=2, include=['title']) ) self.checkQuery(query, json_output)