Exemple #1
0
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()
 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)
Exemple #3
0
    def _get_aggregated_query(self, start, limit):
        max_size = (start or 0) + (limit or 0)
        query = HQESQuery(self.table_name).size(0)
        for filter in self.filters:
            query = query.filter(filter)

        top_agg = TermsAggregation(self.aggregation_columns[0],
                                   self.aggregation_columns[0],
                                   size=max_size)
        for agg_column in self.aggregation_columns[1:]:
            # todo support multiple aggregations
            pass

        aggregations = []
        for col in self.top_level_columns:
            if col.type == 'expanded':
                for sub_col in get_expanded_column_config(
                        self.config, col, 'en').columns:
                    aggregations.append(sub_col.aggregation)
            elif col.type == 'field':
                if col.aggregation == 'sum':
                    # todo push this to the column
                    aggregations.append(SumAggregation(col.field, col.field))

        for agg in aggregations:
            top_agg = top_agg.aggregation(agg)

        if self.order_by:
            # todo sort by more than one column
            # todo sort by by something other than the first aggregate column
            col, desc = self.order_by[0]
            if col == self.aggregation_columns[
                    0] or col == self.top_level_columns[0].field:
                top_agg = top_agg.order('_count', desc)

        query = query.aggregation(top_agg)

        return query.run()