def _cases_updated_per_user_per_month(self): results = (CaseES(es_instance_alias=ES_EXPORT_INSTANCE).domain( self.domain).active_in_range( gte=self.date_start, lt=self.date_end).aggregation( TermsAggregation( 'cases_per_user', 'owner_id', size=100).aggregation( NestedAggregation( 'actions', 'actions').aggregation( DateHistogram( 'cases_by_date', 'server_date', interval='month')))).size(0).run()) stats = defaultdict(list) cases_per_user = results.aggregations.cases_per_user for bucket in cases_per_user.buckets_list: counts_by_date = { b['key_as_string']: b['doc_count'] for b in bucket.actions.cases_by_date.normalized_buckets } for key, count in counts_by_date.items(): stats[key].append(count) final_stats = [] for month, case_count_list in sorted(list(stats.items()), key=lambda r: r[0]): final_stats.append( (month, sum(case_count_list) // len(case_count_list))) self._print_table(['Month', 'Cases updated per user'], final_stats)
def test_nested_aggregation(self): json_output = { "query": { "filtered": { "filter": { "and": [{ "match_all": {} }] }, "query": { "match_all": {} } } }, "aggs": { "case_actions": { "nested": { "path": "actions" } }, }, "size": SIZE_LIMIT } query = HQESQuery('cases').aggregation( NestedAggregation( 'case_actions', 'actions', )) self.checkQuery(query, json_output)
def get_case_types_from_apps(domain): """ Get the case types of modules in applications in the domain. :returns: A set of case_types """ case_types_agg = NestedAggregation('modules', 'modules').aggregation( TermsAggregation('case_types', 'modules.case_type.exact')) q = (AppES().domain(domain).is_build(False).size(0).aggregation( case_types_agg)) return set(q.run().aggregations.modules.case_types.keys) - {''}