def test_hierarchy_compatibility(self): cut = cubes.PointCut("date", [2000]) cell = cubes.Cell(self.cube, [cut]) self.assertRaises(HierarchyError, self.browser.aggregate, cell, drilldown=[("date", "yqmd", None)]) cut = cubes.PointCut("date", [2000], hierarchy="yqmd") cell = cubes.Cell(self.cube, [cut]) result = self.browser.aggregate(cell, drilldown=[("date", "yqmd", None)]) self.assertEqual(4, result.total_cell_count) cut = cubes.PointCut("date", [2000], hierarchy="yqmd") cell = cubes.Cell(self.cube, [cut]) self.assertRaises(HierarchyError, self.browser.aggregate, cell, drilldown=[("date", "ywd", None)]) cut = cubes.PointCut("date", [2000], hierarchy="ywd") cell = cubes.Cell(self.cube, [cut]) result = self.browser.aggregate(cell, drilldown=[("date", "ywd", None)]) self.assertEqual(54, result.total_cell_count)
def test_cell_details(self): cell = cubes.Cell(self.cube, [cubes.PointCut("date", [2012])]) details = self.browser.cell_details(cell) self.assertEqual(1, len(details)) cell = cubes.Cell(self.cube, [cubes.PointCut("product", [10])]) details = self.browser.cell_details(cell) self.assertEqual(1, len(details))
def test_drilldown(self): cell = cubes.Cell(self.cube) result = self.browser.aggregate(cell, drilldown=["date"]) self.assertEqual(1, result.total_cell_count) result = self.browser.aggregate(cell, drilldown=["date:month"]) self.assertEqual(12, result.total_cell_count) result = self.browser.aggregate(cell, drilldown=[("date", None, "month")]) self.assertEqual(12, result.total_cell_count) result = self.browser.aggregate(cell, drilldown=[("date", None, "day")]) self.assertEqual(366, result.total_cell_count) # Test year-quarter-month-day hier = self.model.dimension("date").hierarchy("yqmd") result = self.browser.aggregate(cell, drilldown=[("date", "yqmd", "day")]) self.assertEqual(366, result.total_cell_count) result = self.browser.aggregate(cell, drilldown=[("date", "yqmd", "quarter") ]) self.assertEqual(4, result.total_cell_count)
def report(self, cube): """Create multi-query report response.""" self.create_browser(cube) self.prepare_cell() report_request = self.json_request() try: queries = report_request["queries"] except KeyError: help = "Wrap all your report queries under a 'queries' key. The " \ "old documentation was mentioning this requirement, however it " \ "was not correctly implemented and wrong example was provided." raise RequestError("Report request does not contain 'queries' key", help=help) cell_cuts = report_request.get("cell") if cell_cuts: # Override URL cut with the one in report cuts = [cubes.cut_from_dict(cut) for cut in cell_cuts] cell = cubes.Cell(self.browser.cube, cuts) self.logger.info("using cell from report specification (URL parameters are ignored)") else: cell = self.cell result = self.browser.report(cell, queries) return self.json_response(result)
def test_cell(self): cell = cubes.Cell(self.cube) result = self.browser.aggregate(cell) self.assertEqual(366, result.summary["record_count"]) cut = cubes.PointCut("date", [2000, 2]) cell = cubes.Cell(self.cube, [cut]) result = self.browser.aggregate(cell) self.assertEqual(29, result.summary["record_count"]) cut = cubes.PointCut("date", [2000, 2], hierarchy="ywd") cell = cubes.Cell(self.cube, [cut]) result = self.browser.aggregate(cell) self.assertEqual(7, result.summary["record_count"]) cut = cubes.PointCut("date", [2000, 1], hierarchy="yqmd") cell = cubes.Cell(self.cube, [cut]) result = self.browser.aggregate(cell) self.assertEqual(91, result.summary["record_count"])
def prepare_cell(self): cut_string = self.args.get("cut") if cut_string: self.logger.debug("preparing cell from string: '%s'" % cut_string) cuts = cubes.cuts_from_string(cut_string) else: self.logger.debug("preparing cell as whole cube") cuts = [] self.cell = cubes.Cell(self.cube, cuts)
def test_slice_drilldown(self): cut = cubes.browser.PointCut("date", []) original_cell = cubes.Cell(self.cube, [cut]) cell = original_cell.drilldown("date", 2010) self.assertEqual([2010], cell.cut_for_dimension("date").path) cell = cell.drilldown("date", 1) self.assertEqual([2010, 1], cell.cut_for_dimension("date").path) cell = cell.drilldown("date", 2) self.assertEqual([2010, 1, 2], cell.cut_for_dimension("date").path)
def test_cell_details(self): cell = cubes.Cell(self.cube, [cubes.PointCut("date", [2012])]) details = self.browser.cell_details(cell) self.assertEqual(1, len(details)) self.assertEqual([[{ "date.year": 2012, "_key": 2012, "_label": 2012 }]], details) cell = cubes.Cell(self.cube, [cubes.PointCut("product", [10])]) details = self.browser.cell_details(cell) self.assertEqual(1, len(details)) self.assertEqual([[{ "product.category": 10, "product.category_name": "Things", "_key": 10, "_label": "Things" }]], details) cell = cubes.Cell( self.cube, [cubes.PointCut("date", [2012]), cubes.PointCut("product", [10])]) facts = list(self.browser.values(cell, "product", 1)) details = self.browser.cell_details(cell) self.assertEqual(2, len(details)) self.assertEqual([[{ "date.year": 2012, "_key": 2012, "_label": 2012 }], [{ "product.category": 10, "product.category_name": "Things", "_key": 10, "_label": "Things" }]], details)
def index_dimension(self, dimension, dimension_tag, locale, locale_tag, **options): """Create dimension index records.""" hierarchy = dimension.hierarchy() # Switch browser locale self.browser.set_locale(locale) cell = cubes.Cell(self.cube) labels_only = bool(options.get("labels_only")) for depth_m1, level in enumerate(hierarchy.levels): depth = depth_m1 + 1 levels = hierarchy.levels[0:depth] keys = [level.key.ref() for level in levels] level_key = keys[-1] level_label = (level.label_attribute.ref()) if labels_only: attributes = [level.label_attribute] else: attributes = [] for attr in level.attributes: if not attr.info or \ (attr.info and not attr.info.get("no_search")): attributes.append(attr) for record in self.browser.values(cell, dimension, depth): path = [record[key] for key in keys] path_string = cubes.string_from_path(path) for attr in attributes: fname = attr.ref() irecord = { "locale_tag": locale_tag, "dimension": dimension.name, "dimension_tag": dimension_tag, "level": level.name, "depth": depth, "path": path_string, "attribute": attr.name, "value": record[fname], "level_key": record[level_key], "level_label": record[level_label] } self.add(irecord)
def index_dimension(self, dimension, dimension_tag, locale, locale_tag, **options): """Create dimension index records. If `Attribute.info` has key `no_search` set to `True`, then the field is skipped """ print "indexing %s, locale: %s" % (dimension, locale) hierarchy = dimension.hierarchy() # Switch browser locale self.browser.set_locale(locale) cell = cubes.Cell(self.cube) for depth_m1, level in enumerate(hierarchy.levels): depth = depth_m1 + 1 levels = hierarchy.levels[0:depth] keys = [level.key.ref() for level in levels] level_key = keys[-1] level_label = (level.label_attribute.ref()) if options.get("labels_only"): attributes = [level.label_attribute] else: attributes = [] for attr in level.attributes: if not attr.info or \ (attr.info and not attr.info.get("no_search")): attributes.append(attr) for record in self.browser.values(cell, dimension, depth): path = [record[key] for key in keys] path_string = cubes.string_from_path(path) for attr in attributes: ref = unicode(attr.ref()) self.writer.add_document(locale=unicode(locale), dimension=dimension.name, level=level.name, depth=depth, path=path_string, level_key=record[level_key], level_label=record[level_label], attribute=attr.name, value=unicode(record[ref]))
def test_implicit_level(self): cut = cubes.PointCut("date", [2000]) cell = cubes.Cell(self.cube, [cut]) result = self.browser.aggregate(cell, drilldown=["date"]) self.assertEqual(12, result.total_cell_count) result = self.browser.aggregate(cell, drilldown=["date:month"]) self.assertEqual(12, result.total_cell_count) result = self.browser.aggregate(cell, drilldown=[("date", None, "month")]) self.assertEqual(12, result.total_cell_count) result = self.browser.aggregate(cell, drilldown=[("date", None, "day")]) self.assertEqual(366, result.total_cell_count)
def report(dim_name=None): global model browser = get_browser() if not dim_name: return render_template('report_star.html', dimensions=model.dimensions) # First we need to get the hierarchy to know the order of levels. Cubes # supports multiple hierarchies internally. dimension = model.dimension(dim_name) hierarchy = dimension.hierarchy() # Parse the`cut` request parameter and convert it to a list of # actual cube cuts. Think of this as of multi-dimensional path, even that # for this simple example, we are goint to use only one dimension for # browsing. cutstr = request.args.get("cut") cell = cubes.Cell(browser.cube, cubes.cuts_from_string(cutstr)) # Get the cut of actually browsed dimension, so we know "where we are" - # the current dimension path cut = cell.cut_for_dimension(dimension) if cut: path = cut.path else: path = [] # # Do the work, do the aggregation. # result = browser.aggregate(cell, drilldown=[dim_name]) # If we have no path, then there is no cut for the dimension, # therefore # there is no corresponding detail. if path: breadcrumbs = browser.cell_details(cell, dimension)[0] else: breadcrumbs = {} # Find what level we are on and what is going to be the drill-down level # in the hierarchy levels = hierarchy.levels_for_path(path) if levels: next_level = hierarchy.next_level(levels[-1]) else: next_level = hierarchy.levels[0] # To have human-readable table, we are not going to display keys or codes, # but actual human-readable labels that are also stored within dimension. # The dimension provides information in which attribute the label is # stored. label_attribute = next_level.label_attribute.ref(simplify=True) # We also need to know key attribute for the level, so we can generate # appropriate URL links with cut of drilled-down cells. key = next_level.key.ref(simplify=True) # Are we at the very detailed level? is_last = len(path) >= len(hierarchy.levels) - 1 # Finally, we render it return render_template('report_star.html', dimensions=model.dimensions, dimension=dimension, levels=levels, next_level=next_level, label_attribute=label_attribute, level_key=key, result=result, cell=cell, is_last=is_last, breadcrumbs=breadcrumbs)
def test_range_drilldown(self): cut = cubes.RangeCut("date", [2000, 1], [2000, 3]) cell = cubes.Cell(self.cube, [cut]) result = self.browser.aggregate(cell, drilldown=["date"]) # This should test that it does not drilldown on range self.assertEqual(1, result.total_cell_count)
def test_levels_from_drilldown(self): cell = cubes.Cell(self.cube) dim = self.cube.dimension("date") l_year = dim.level("year") l_month = dim.level("month") l_day = dim.level("day") drilldown = [("date", None, "year")] expected = [(dim, [l_year])] self.assertEqual(expected, cubes.levels_from_drilldown(cell, drilldown)) drilldown = ["date"] self.assertEqual(expected, cubes.levels_from_drilldown(cell, drilldown)) drilldown = [("date", None, "year")] expected = [(dim, [l_year])] self.assertEqual(expected, cubes.levels_from_drilldown(cell, drilldown)) # Try "next level" cut = cubes.PointCut("date", [2010]) cell = cubes.Cell(self.cube, [cut]) drilldown = [("date", None, "year")] expected = [(dim, [l_year])] self.assertEqual(expected, cubes.levels_from_drilldown(cell, drilldown)) drilldown = ["date"] expected = [(dim, [l_year, l_month])] self.assertEqual(expected, cubes.levels_from_drilldown(cell, drilldown)) # Try with range cell # cut = cubes.RangeCut("date", [2009], [2010]) # cell = cubes.Cell(self.cube, [cut]) # drilldown = ["date"] # expected = [(dim, [l_year, l_month])] # self.assertEqual(expected, cubes.levels_from_drilldown(cell, drilldown)) # drilldown = [("date", None, "year")] # expected = [(dim, [l_year])] # self.assertEqual(expected, cubes.levels_from_drilldown(cell, drilldown)) # cut = cubes.RangeCut("date", [2009], [2010, 1]) # cell = cubes.Cell(self.cube, [cut]) # drilldown = ["date"] # expected = [(dim, [l_year, l_month, l_day])] # self.assertEqual(expected, cubes.levels_from_drilldown(cell, drilldown)) # Try "last level" cut = cubes.PointCut("date", [2010, 1, 2]) cell = cubes.Cell(self.cube, [cut]) drilldown = [("date", None, "day")] expected = [(dim, [l_year, l_month, l_day])] self.assertEqual(expected, cubes.levels_from_drilldown(cell, drilldown)) drilldown = ["date"] expected = [(dim, [l_year, l_month])] self.assertRaises(HierarchyError, cubes.levels_from_drilldown, cell, drilldown)
with open("table.html", "w") as f: f.write(out) ######################################################################## # Create browsing context model = cubes.load_model("vvo_model.json") cube = model.cube("contracts") workspace = cubes.create_workspace("sql", model, url="sqlite:///vvo_data.sqlite", dimension_prefix="dm_", fact_prefix="ft_") browser = workspace.browser(cube) cell = cubes.Cell(cube) ######################################################################## # Create cross-table (written to table.html in current directory) result = browser.aggregate(cell, drilldown=["process_type", "date"]) rows = ["process_type.description"] columns = ["date.year"] table = result.cross_table(rows, columns, ["contract_amount_sum"]) print_cross_table(rows, columns, table) print "Created file: table.html"
def test_coalesce_drilldown(self): cell = cubes.Cell(self.cube) dim = self.cube.dimension("date") drilldown = {"date": "year"} expected = {"date": [dim.level("year")]} self.assertEqual(expected, coalesce_drilldown(cell, drilldown)) drilldown = ["date"] self.assertEqual(expected, coalesce_drilldown(cell, drilldown)) drilldown = {"date": "day"} expected = { "date": [dim.level("year"), dim.level("month"), dim.level("day")] } self.assertEqual(expected, coalesce_drilldown(cell, drilldown)) # Try "next level" cut = cubes.PointCut("date", [2010]) cell = cubes.Cell(self.cube, [cut]) drilldown = {"date": "year"} expected = {"date": [dim.level("year")]} self.assertEqual(expected, coalesce_drilldown(cell, drilldown)) drilldown = ["date"] expected = {"date": [dim.level("year"), dim.level("month")]} self.assertEqual(expected, coalesce_drilldown(cell, drilldown)) # Try with range cell cut = cubes.RangeCut("date", [2009], [2010]) cell = cubes.Cell(self.cube, [cut]) drilldown = ["date"] expected = {"date": [dim.level("year"), dim.level("month")]} self.assertEqual(expected, coalesce_drilldown(cell, drilldown)) drilldown = {"date": "year"} expected = {"date": [dim.level("year")]} self.assertEqual(expected, coalesce_drilldown(cell, drilldown)) cut = cubes.RangeCut("date", [2009], [2010, 1]) cell = cubes.Cell(self.cube, [cut]) drilldown = ["date"] expected = { "date": [dim.level("year"), dim.level("month"), dim.level("day")] } self.assertEqual(expected, coalesce_drilldown(cell, drilldown)) # Try "last level" cut = cubes.PointCut("date", [2010, 1, 2]) cell = cubes.Cell(self.cube, [cut]) drilldown = {"date": "day"} expected = { "date": [dim.level("year"), dim.level("month"), dim.level("day")] } self.assertEqual(expected, coalesce_drilldown(cell, drilldown)) drilldown = ["date"] expected = {"date": [dim.level("year"), dim.level("month")]} self.assertRaises(ArgumentError, coalesce_drilldown, cell, drilldown)
def report(dim_name=None): cube = workspace.cube("contracts") browser = workspace.browser(cube) result = browser.aggregate() print 'keys: ', result.levels if not dim_name: return render_template('report.html', dimensions=cube.dimensions) # First we need to get the hierarchy to know the order of levels. Cubes # supports multiple hierarchies internally. dimension = cube.dimension(dim_name) hierarchy = dimension.hierarchy() # Parse the`cut` request parameter and convert it to a list of # actual cube cuts. Think of this as of multi-dimensional path, even that # for this simple example, we are goint to use only one dimension for # browsing. cutstr = request.args.get("cut") cell = cubes.Cell(browser.cube, cubes.cuts_from_string(cube,cutstr)) # Get the cut of actually browsed dimension, so we know "where we are" - # the current dimension path cut = cell.cut_for_dimension(dimension) if cut: path = cut.path else: path = [] # # Do the work, do the aggregation. # print "AGGREGATE %s DD: %s" % (cell, dim_name) result = browser.aggregate(cell, drilldown=[dim_name]) # If we have no path, then there is no cut for the dimension, # therefore # there is no corresponding detail. if path: details = browser.cell_details(cell, dimension)[0] else: details = [] # Find what level we are on and what is going to be the drill-down level # in the hierarchy levels = hierarchy.levels_for_path(path) if levels: next_level = hierarchy.next_level(levels[-1]) else: next_level = hierarchy.next_level(None) # Are we at the very detailed level? is_last = hierarchy.is_last(next_level) # Finally, we render it return render_template('report.html', dimensions=cube.dimensions, dimension=dimension, levels=levels, next_level=next_level, result=result, cell=cell, is_last=is_last, details=details)