def aggregate(self, aggregates=None, drilldowns=None, cuts=None, order=None, page=None, page_size=None): """ Main aggregation function. This is used to compute a given set of aggregates, grouped by a given set of drilldown dimensions (i.e. dividers). The query can also be filtered and sorted. """ q = select() cuts, q = Cuts(self).apply(q, cuts) aggregates, q = Aggregates(self).apply(q, aggregates) summary = first_result(self, q) attributes, q = Drilldowns(self).apply(q, drilldowns) count = count_results(self, q) page, q = Pagination(self).apply(q, page, page_size) ordering, q = Ordering(self).apply(q, order) return { 'total_cell_count': count, 'cells': list(generate_results(self, q)), 'summary': summary, 'cell': cuts, 'aggregates': aggregates, 'attributes': attributes, 'order': ordering, 'page': page['page'], 'page_size': page['page_size'] }
def prep(cuts, ref, order, columns=None): q = select(columns=columns) bindings = [] cuts, q, bindings = Cuts(self).apply(q, bindings, cuts) fields, q, bindings = \ Fields(self).apply(q, bindings, ref, distinct=True) ordering, q, bindings = \ Ordering(self).apply(q, bindings, order, distinct=fields[0]) q = self.restrict_joins(q, bindings) return q, bindings, cuts, fields, ordering
def prep(cuts, drilldowns=False, aggregates=False, columns=None): q = select(columns) bindings = [] cuts, q, bindings = Cuts(self).apply(q, bindings, cuts) attributes = None if drilldowns is not False: attributes, q, bindings = Drilldowns(self).apply( q, bindings, drilldowns) if aggregates is not False: aggregates, q, bindings = Aggregates(self).apply( q, bindings, aggregates) q = self.restrict_joins(q, bindings) return q, bindings, attributes, aggregates, cuts
def members(self, ref, cuts=None, order=None, page=None, page_size=None): """ List all the distinct members of the given reference, filtered and paginated. If the reference describes a dimension, all attributes are returned. """ q = select(distinct=True) cuts, q = Cuts(self).apply(q, cuts) fields, q = Fields(self).apply(q, ref) ordering, q = Ordering(self).apply(q, order) count = count_results(self, q) page, q = Pagination(self).apply(q, page, page_size) return { 'total_member_count': count, 'data': list(generate_results(self, q)), 'cell': cuts, 'fields': fields, 'order': ordering, 'page': page['page'], 'page_size': page['page_size'] }
def facts(self, fields=None, cuts=None, order=None, page=None, page_size=None): """ List all facts in the cube, returning only the specified references if these are specified. """ q = select() cuts, q = Cuts(self).apply(q, cuts) fields, q = Fields(self).apply(q, fields) count = count_results(self, q) ordering, q = Ordering(self).apply(q, order) page, q = Pagination(self).apply(q, page, page_size) return { 'total_fact_count': count, 'data': list(generate_results(self, q)), 'cell': cuts, 'fields': fields, 'order': ordering, 'page': page['page'], 'page_size': page['page_size'] }
def test_cuts_multiple_int_first(self, cube): cuts = Cuts(cube).parse('bar:5|foo:bar') assert len(cuts) == 2, cuts cuts = [(c[0], c[1], list(c[2])) for c in cuts] assert ('bar', ':', [5]) in list(cuts), cuts
def test_null_filter(self): cuts = Cuts(self.cube).parse(None) assert isinstance(cuts, list) assert not len(cuts)
def test_cuts_null(self): cuts = Cuts(self.cube).parse('foo:') assert cuts[0][2] is None, cuts
def test_cuts_invalid(self): Cuts(self.cube).parse('f oo:2015-01-04')
def test_cuts_date(self): cuts = Cuts(self.cube).parse('foo:2015-01-04') assert cuts[0][2] == date(2015, 01, 04), cuts
def test_cuts_date_set(self, cube): cuts = Cuts(cube).parse('foo:2015-01-04;2015-01-05') assert len(cuts) == 1, cuts assert list(cuts[0][2]) == [date(2015, 1, 4), date(2015, 1, 5)], cuts
def test_cuts_int_set(self, cube): cuts = Cuts(cube).parse('foo:3;22') assert len(cuts) == 1, cuts cuts = [(c[0], c[1], list(c[2])) for c in cuts] assert ('foo', ':', [3, 22]) in cuts, cuts
def prep(cuts, columns=None): q = select(columns=columns).select_from(self.fact_table) bindings = [] _, q, bindings = Cuts(self).apply(q, bindings, cuts) q = self.restrict_joins(q, bindings) return q, bindings
def test_cuts(self): cuts = Cuts(self.cube).parse('foo:bar') assert len(cuts) == 1, cuts
def test_cuts_unquoted_string(self, cube): cuts = Cuts(cube).parse('foo:bar') assert len(cuts) == 1, cuts cuts = [(c[0], c[1], list(c[2])) for c in cuts] assert ('foo', ':', ['bar']) in cuts, cuts
def test_cuts_invalid(self, cube): with pytest.raises(QueryException): Cuts(cube).parse('f oo:2015-01-04')
def test_cuts_int_prefixed_string(self, cube): cuts = Cuts(cube).parse('foo:2015M01') assert list(cuts[0][2]) == ['2015M01'], cuts
def test_cuts_int(self, cube): cuts = Cuts(cube).parse('foo:2015') assert list(cuts[0][2]) == [2015], cuts
def test_cuts_quotes(self): cuts = Cuts(self.cube).parse('foo:"bar|lala"|bar:5') assert len(cuts) == 2, cuts
def test_cuts_quoted(self): cuts = Cuts(self.cube).parse('foo:"bar lala"') assert len(cuts) == 1, cuts
def test_cuts_date(self, cube): cuts = Cuts(cube).parse('foo:2015-01-04') assert list(cuts[0][2]) == [date(2015, 1, 4)], cuts
def test_cuts_multiple(self): cuts = Cuts(self.cube).parse('foo:bar|bar:5') assert len(cuts) == 2, cuts assert ('bar', ':', 5) in cuts, cuts
def test_cuts_string_set(self, cube): cuts = Cuts(cube).parse('foo:"bar";"lala"') assert len(cuts) == 1, cuts cuts = [(c[0], c[1], list(c[2])) for c in cuts] assert ('foo', ':', ['bar', 'lala']) in cuts, cuts