def get_ledger_section_entry_combinations(domain): """Get all section / entry combinations in a domain. :returns: a generator of namedtuples with fields ``section_id``, ``entry_id``, ``doc_count`` """ terms = [ AggregationTerm('section_id', 'section_id'), AggregationTerm('entry_id', 'entry_id'), ] query = LedgerES().domain(domain) return NestedTermAggregationsHelper(base_query=query, terms=terms).get_data()
def get_app_submission_breakdown_es(domain_name, monthspan): terms = [ AggregationTerm('app_id', 'app_id'), AggregationTerm('device_id', 'form.meta.deviceID'), AggregationTerm('user_id', 'form.meta.userID'), AggregationTerm('username', 'form.meta.username'), ] query = FormES().domain(domain_name).submitted( gte=monthspan.startdate, lt=monthspan.computed_enddate, ) return NestedTermAggregationsHelper(base_query=query, terms=terms).get_data()
def get_app_submission_breakdown_es(domain_name, monthspan, user_ids=None): # takes > 1 m to load at 50k worker scale terms = [ AggregationTerm('app_id', 'app_id'), AggregationTerm('device_id', 'form.meta.deviceID'), AggregationTerm('user_id', 'form.meta.userID'), AggregationTerm('username', 'form.meta.username'), ] query = FormES(es_instance_alias=ES_EXPORT_INSTANCE).domain(domain_name).submitted( gte=monthspan.startdate, lt=monthspan.computed_enddate, ) if user_ids is not None: query = query.user_id(user_ids) return NestedTermAggregationsHelper(base_query=query, terms=terms).get_data()
def test_nested_terms_helper(self): json_output = { "query": { "filtered": { "filter": { "and": [{ "match_all": {} }] }, "query": { "match_all": {} } } }, "aggs": { "app_id": { "terms": { "field": "app_id", "size": SIZE_LIMIT, }, "aggs": { "user_id": { "terms": { "field": "user_id", "size": SIZE_LIMIT, }, "aggs": { "balance": { "sum": { "field": "balance" } } } } } } }, "size": SIZE_LIMIT } base_query = HQESQuery('cases') query = NestedTermAggregationsHelper( base_query=base_query, terms=[ AggregationTerm('app_id', 'app_id'), AggregationTerm('user_id', 'user_id') ], inner_most_aggregation=SumAggregation('balance', 'balance')).query self.checkQuery(query, json_output)
def initialize_ledger_combinations(apps, schema_editor): terms = [ AggregationTerm('domain', 'domain'), AggregationTerm('section_id', 'section_id'), AggregationTerm('entry_id', 'entry_id'), ] combos = [ a for a in NestedTermAggregationsHelper(base_query=LedgerES(), terms=terms).get_data() ] for combo in combos: LedgerSectionEntry.objects.get_or_create( domain=combo.domain, section_id=combo.section_id, entry_id=combo.entry_id, )
def get_aggregated_ledger_values(domain, case_ids, section_id, entry_ids=None): # todo: figure out why this causes circular import query = LedgerES().domain(domain).section(section_id).case(case_ids) if entry_ids: query = query.entry(entry_ids) terms = [ AggregationTerm('entry_id', 'entry_id'), ] return NestedTermAggregationsHelper( base_query=query, terms=terms, inner_most_aggregation=SumAggregation('balance', 'balance'), ).get_data()