def setUp(self): a = [DIM_DATE_DESC, DIM_PRODUCT_DESC, DIM_FLAG_DESC] self.measures = create_list_of(Measure, ["amount", "discount"]) self.details = create_list_of(Attribute, ["detail"]) self.dimensions = [Dimension.from_metadata(desc) for desc in a] self.cube = Cube("contracts", dimensions=self.dimensions, measures=self.measures, details=self.details)
def test_to_dict(self): desc = self.cube.to_dict() dims = dict((dim.name, dim) for dim in self.dimensions) cube = Cube.from_metadata(desc, dims) self.assertEqual(self.cube.dimensions, cube.dimensions) self.assertEqual(self.cube.measures, cube.measures) self.assertEqual(self.cube, cube)
def test_links(self): # TODO: test link alias! dims = dict((d.name, d) for d in self.dimensions) links = [{"name": "date"}] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) self.assertEqual(len(cube.dimensions), 1) dim = cube.dimension("date") self.assertEqual(len(dim.hierarchies), 2) links = [{"name": "date"}, "product", "flag"] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) self.assertEqual(len(cube.dimensions), 3) self.assertIsInstance(cube.dimension("flag"), Dimension)
def get_complex_cube(star_name, cubes): #skipping authorization without "authorized_cube" func #the workspace.cube function will raiseerror if cube is not found star_cube_raw = current_app.cubes_workspace.cube(star_name, locale=g.locale, metaonly=True) star_cube = add_table_identifier(star_cube_raw, seperator="__") cubes_meta = [] for cube_name in cubes: if cube_name: cube_joiner_meta_raw = current_app.cubes_workspace.cube( cube_name, locale=g.locale, metaonly=True) cube_joiner_meta = add_table_identifier(cube_joiner_meta_raw, seperator="__") cubes_meta.append(cube_joiner_meta) star_cube = coalesce_cubes(star_cube, cubes_meta) return Cube(name=star_cube['name'], fact=star_cube['fact'], aggregates=star_cube['aggregates'], measures=star_cube['measures'], label=star_cube['label'], description=star_cube['description'], dimensions=star_cube['dimensions'], store=star_cube['store'], mappings=star_cube['mappings'], joins=star_cube['joins'])
def test_create_cube(self): cube = { "name": "cube", "dimensions": ["date"], "aggregates": ["record_count"], "details": ["some_detail", "another_detail"] } cube = Cube.from_metadata(cube) self.assertEqual(cube.name, "cube") self.assertEqual(len(cube.aggregates), 1) self.assertEqual(len(cube.details), 2)
def cube(self, name, locale=None): """Create a GA cube: * cube is a GA group * GA metric is cube aggregate * GA dimension is cube dimension """ # TODO: preliminary implementation try: metadata = self.cube_metadata(name) except NoSuchCubeError: metadata = {} group = self.cube_to_group[name] # Gather aggregates metrics = self.ga_group_metrics[group] aggregates = [] for metric in metrics: aggregate = { "name": ga_id_to_identifier(metric["id"]), "label": metric["uiName"], "description": metric.get("description") } mtype = self._measurement_type_for(metric.get('dataType')) if mtype: aggregate['info'] = {'measurement_type': mtype} aggregates.append(aggregate) aggregates = aggregate_list(aggregates) dims = self.ga_group_dims[group] dims = [ga_id_to_identifier(d["id"]) for d in dims] dims = ["time"] + dims cube = Cube(name=name, label=metadata.get("label", group), aggregates=aggregates, category=metadata.get("category", self.store.category), info=metadata.get("info"), dimension_links=dims, store=self.store) return cube
def test_inherit_nonadditive(self): dims = [DIM_DATE_DESC, DIM_PRODUCT_DESC, DIM_FLAG_DESC] cube = { "name": "contracts", "dimensions": ["date", "product"], "nonadditive": "time", "measures": ["amount", "discount"] } dims = [Dimension.from_metadata(md) for md in dims] dims = dict((dim.name, dim) for dim in dims) cube = Cube.from_metadata(cube) measures = cube.measures self.assertEqual(measures[0].nonadditive, "time")
def cube(self, name, locale=None): """Creates a mixpanel cube with following variables: * `name` – cube name * `measures` – cube measures: `total` and `uniques` * `dimension_links` – list of linked dimension names * `mappings` – mapping of corrected dimension names Dimensions are Mixpanel's properties where ``$`` character is replaced by the underscore ``_`` character. """ params = { "event": self.cube_to_event.get(name, name), "limit": DIMENSION_COUNT_LIMIT } result = self.store.request(["events", "properties", "top"], params) if not result: raise NoSuchCubeError("Unknown Mixpanel cube %s" % name, name) try: metadata = self.cube_metadata(name) except NoSuchCubeError: metadata = {} options = self.cube_options(name) allowed_dims = options.get("allowed_dimensions", []) denied_dims = options.get("denied_dimensions", []) dims = ["time"] mappings = {} for prop in result.keys(): try: dim_name = self.property_to_dimension[prop] except KeyError: dim_name = _mangle_dimension_name(prop) # Skip not allowed dimensions if (allowed_dims and dim_name not in allowed_dims) or \ (denied_dims and dim_name in denied_dims): continue if dim_name != prop: mappings[dim_name] = prop dims.append(dim_name) aggregates = aggregate_list(MXP_AGGREGATES_METADATA) label = metadata.get("label", capwords(name.replace("_", " "))) category = metadata.get("category", self.store.category) cube = Cube(name=name, aggregates=aggregates, label=label, description=category, info=metadata.get("info"), dimension_links=dims, store=self.store, mappings=mappings, category=category) cube.info["required_drilldowns"] = ["time"] return cube
def test_link_hierarchies(self): dims = dict((d.name, d) for d in self.dimensions) links = [{"name": "date"}] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) dim = cube.dimension("date") self.assertEqual(len(dim.hierarchies), 2) self.assertEqual(dim.hierarchy().name, "ymd") links = [{"name": "date", "nonadditive": None}] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) dim = cube.dimension("date") self.assertEqual(len(dim.hierarchies), 2) self.assertEqual(dim.hierarchy().name, "ymd") links = [{"name": "date", "hierarchies": ["ym"]}] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) dim = cube.dimension("date") self.assertEqual(len(dim.hierarchies), 1) self.assertEqual(dim.hierarchy().name, "ym")
def getGeomCube(provider, metaonly): basemeta = { "name": "geometry", "info": {}, "label": "Base Geometry", 'fact_table': "geometry__time", "description": "The Base Geometry Table", "aggregates": [], "measures": [], "details": [] } dim_metas = [{ "name": "time", "info": {}, "label": "Year", "default_hierarchy_name": "time", "levels": [{ "name": "time", "info": {}, "label": "Year", "key": "time", "label_attribute": "time", "order_attribute": "time", "attributes": [{ "name": "time", "info": {}, "label": "Year", "ref": "geometry__time.time", "locales": [] }] }], "hierarchies": [ { "name": "time", "info": {}, "label": "Year", "levels": ["time"] }, ], "is_flat": False, "has_details": False }, { "name": "country_level0", "info": {}, "label": "Country Name (lower case)", "default_hierarchy_name": "name", "levels": [{ "name": "name", "info": {}, "label": "Country Name", "key": "name", "label_attribute": "name", "order_attribute": "name", "attributes": [{ "name": "name", "info": {}, "label": "Country Name", "ref": "geometry__country_level0.name", "locales": [] }] }, { "name": "continent", "info": {}, "label": "Continents Countries", "key": "continent", "label_attribute": "continent", "order_attribute": "continent", "attributes": [{ "name": "continent", "info": {}, "label": "Continents Country", "ref": "geometry__country_level0.continent", "locales": [] }] }, { "name": "dos_region", "info": {}, "label": "Department of State Countries", "key": "dos_region", "label_attribute": "dos_region", "order_attribute": "dos_region", "attributes": [{ "name": "dos_region", "info": {}, "label": "Department of State Country", "ref": "geometry__country_level0.dos_region", "locales": [] }] }, { "name": "dod_cmd", "info": {}, "label": "Department of Defense Country", "key": "dod_cmd", "label_attribute": "dod_cmd", "order_attribute": "dod_cmd", "attributes": [{ "name": "dod_cmd", "info": {}, "label": "Department of Defense Country", "ref": "geometry__country_level0.dod_cmd", "locales": [] }] }, { "name": "usaid_reg", "info": {}, "label": "USAID Countries", "key": "usaid_reg", "label_attribute": "usaid_reg", "order_attribute": "usaid_reg", "attributes": [{ "name": "usaid_reg", "info": {}, "label": "USAID Countries", "ref": "geometry__country_level0.usaid_reg", "locales": [] }] }, { "name": "feed_the_f", "info": {}, "label": "Feed the Future Countries", "key": "feed_the_f", "label_attribute": "feed_the_f", "order_attribute": "feed_the_f", "attributes": [{ "name": "feed_the_f", "info": {}, "label": "Feed the Future Countries", "ref": "geometry__country_level0.feed_the_f", "locales": [] }] }, { "name": "pepfar", "info": {}, "label": "PEPFAR Countries", "key": "pepfar", "label_attribute": "pepfar", "order_attribute": "pepfar", "attributes": [{ "name": "pepfar", "info": {}, "label": "PEPFAR Countries", "ref": "geometry__country_level0.pepfar", "locales": [] }] }, { "name": "paf", "info": {}, "label": "PAF Countries", "key": "paf", "label_attribute": "paf", "order_attribute": "paf", "attributes": [{ "name": "paf", "info": {}, "label": "PAF Countries", "ref": "geometry__country_level0.paf", "locales": [] }] }, { "name": "oecd", "info": {}, "label": "OECD Countries", "key": "oecd", "label_attribute": "oecd", "order_attribute": "oecd", "attributes": [{ "name": "oecd", "info": {}, "label": "OECD Countries", "ref": "geometry__country_level0.oecd", "locales": [] }] }, { "name": "region_un", "info": {}, "label": "UN Region Countries", "key": "region_un", "label_attribute": "region_un", "order_attribute": "region_un", "attributes": [{ "name": "region_un", "info": {}, "label": "UN Region Countries", "ref": "geometry__country_level0.region_un", "locales": [] }] }, { "name": "subregion", "info": {}, "label": "Subregion Countries", "key": "subregion", "label_attribute": "subregion", "order_attribute": "subregion", "attributes": [{ "name": "subregion", "info": {}, "label": "Subregion Countries", "ref": "geometry__country_level0.subregion", "locales": [] }] }, { "name": "region_wb", "info": {}, "label": "World Bank Region Countries", "key": "region_wb", "label_attribute": "region_wb", "order_attribute": "region_wb", "attributes": [{ "name": "region_wb", "info": {}, "label": "World Bank Region Countries", "ref": "geometry__country_level0.region_wb", "locales": [] }] }, { "name": "wb_inc_lvl", "info": {}, "label": "World Bank Income Level Countries", "key": "wb_inc_lvl", "label_attribute": "wb_inc_lvl", "order_attribute": "wb_inc_lvl", "attributes": [{ "name": "wb_inc_lvl", "info": {}, "label": "World Bank Income Level Countries", "ref": "geometry__country_level0.wb_inc_lvl", "locales": [] }] }, { "name": "sovereignt", "info": {}, "label": "Sovereignty", "key": "sovereignt", "label_attribute": "sovereignt", "order_attribute": "sovereignt", "attributes": [{ "name": "sovereignt", "info": {}, "label": "Sovereignty", "ref": "geometry__country_level0.sovereignt", "locales": [] }] }], "hierarchies": [{ "name": "name", "info": {}, "label": "Country Name", "levels": ["name"] }, { "name": "sovereignt", "info": {}, "label": "Sovereignty", "levels": ["sovereignt", "name"] }, { "name": "dos_region", "info": {}, "label": "Department of State Regions", "levels": ["dos_region", "sovereignt", "name"] }, { "name": "usaid_reg", "info": {}, "label": "USAID Regions", "levels": ["usaid_reg", "sovereignt", "name"] }, { "name": "dod_cmd", "info": {}, "label": "Department of Defense Regions", "levels": ["dod_cmd", "sovereignt", "name"] }, { "name": "feed_the_f", "info": {}, "label": "Feed the Future Regions", "levels": ["feed_the_f", "sovereignt", "name"] }, { "name": "pepfar", "info": {}, "label": "PEPFAR Regions", "levels": ["pepfar", "sovereignt", "name"] }, { "name": "paf", "info": {}, "label": "PAF Regions", "levels": ["paf", "sovereignt", "name"] }, { "name": "oecd", "info": {}, "label": "OECD Regions", "levels": ["oecd", "sovereignt", "name"] }, { "name": "region_un", "info": {}, "label": "United Nation Regions", "levels": ["region_un", "sovereignt", "name"] }, { "name": "subregion", "info": {}, "label": "Subregions", "levels": ["subregion", "sovereignt", "name"] }, { "name": "region_wb", "info": {}, "label": "World Bank Regions", "levels": ["region_wb", "sovereignt", "name"] }, { "name": "wb_inc_lvl", "info": {}, "label": "World Bank Income Level Regions", "levels": ["wb_inc_lvl", "sovereignt", "name"] }, { "name": "continent", "info": {}, "label": "Continents", "levels": ["continent", "sovereignt", "name"] }], "is_flat": False, "has_details": False }] joins = [{ "master": u"geometry__time.gid", "detail": u"geometry__country_level0.gid" }] #joins = [] mappings = { u'time.year': u"geometry__time.year", u'geometry__year': u"geometry__time.year", u"time.id": u"geometry__time.id", u"time.gid": u"geometry__time.gid", u"country_level0.continent": u"geometry__country_level0.continent", u"country_level0.dos_region": u"geometry__country_level0.dos_region", u"country_level0.usaid_reg": u"geometry__country_level0.usaid_reg", u"country_level0.dod_cmd": u"geometry__country_level0.dod_cmd", u"country_level0.feed_the_f": u"geometry__country_level0.feed_the_f", u"country_level0.pepfar": u"geometry__country_level0.pepfar", u"country_level0.paf": u"geometry__country_level0.paf", u"country_level0.oecd": u"geometry__country_level0.oecd", u"country_level0.region_un": u"geometry__country_level0.region_un", u"country_level0.subregion": u"geometry__country_level0.subregion", u"country_level0.region_wb": u"geometry__country_level0.region_wb", u"country_level0.wb_inc_lvl": u"geometry__country_level0.wb_inc_lvl", u"country_level0.id": u"geometry__country_level0.id", u"country_level0.name": u"geometry__country_level0.name", u"country_level0.label": u"geometry__country_level0.label" } dimensions = [] for dim in dim_metas: dimensions.append(Dimension.from_metadata(dim)) cube_meta = { "name": basemeta['name'], "fact": basemeta['fact_table'], "aggregates": basemeta['aggregates'], "measures": basemeta['measures'], "label": basemeta['label'], "description": basemeta['description'], "dimensions": dimensions, "store": provider.store, "mappings": mappings, "joins": joins } if metaonly: return cube_meta else: return Cube(name=cube_meta['name'], fact=cube_meta['fact'], aggregates=cube_meta['aggregates'], measures=cube_meta['measures'], label=cube_meta['label'], description=cube_meta['description'], dimensions=cube_meta['dimensions'], store=cube_meta['store'], mappings=cube_meta['mappings'], joins=cube_meta['joins'])
def cube(self, name): """Create a cube object `name` from measures test model.""" return Cube.from_metadata(self.cubes_md[name])
class CubeTestCase(unittest.TestCase): def setUp(self): a = [DIM_DATE_DESC, DIM_PRODUCT_DESC, DIM_FLAG_DESC] self.measures = create_list_of(Measure, ["amount", "discount"]) self.details = create_list_of(Attribute, ["detail"]) self.dimensions = [Dimension.from_metadata(desc) for desc in a] self.cube = Cube("contracts", dimensions=self.dimensions, measures=self.measures, details=self.details) def test_create_cube(self): cube = { "name": "cube", "dimensions": ["date"], "aggregates": ["record_count"], "details": ["some_detail", "another_detail"] } cube = Cube.from_metadata(cube) self.assertEqual(cube.name, "cube") self.assertEqual(len(cube.aggregates), 1) self.assertEqual(len(cube.details), 2) def test_get_dimension(self): self.assertListEqual(self.dimensions, self.cube.dimensions) self.assertEqual("date", self.cube.dimension("date").name) self.assertEqual("product", self.cube.dimension("product").name) self.assertEqual("flag", self.cube.dimension("flag").name) self.assertRaises(NoSuchDimensionError, self.cube.dimension, "xxx") def test_get_measure(self): self.assertListEqual(self.measures, self.cube.measures) self.assertEqual("amount", self.cube.measure("amount").name) self.assertEqual("discount", self.cube.measure("discount").name) self.assertRaises(NoSuchAttributeError, self.cube.measure, "xxx") def test_attributes(self): all_attributes = self.cube.all_attributes refs = [a.ref for a in all_attributes] expected = [ 'date.year', 'date.month', 'date.month_name', 'date.day', 'product.key', 'product.name', 'product.description', 'flag', 'detail', 'amount', 'discount'] self.assertSequenceEqual(expected, refs) attributes = self.cube.get_attributes(["date.year", "product.name"]) refs = [a.ref for a in attributes] expected = ['date.year', 'product.name'] self.assertSequenceEqual(expected, refs) attributes = self.cube.get_attributes(["amount"]) refs = [a.ref for a in attributes] self.assertSequenceEqual(["amount"], refs) with self.assertRaises(NoSuchAttributeError): self.cube.get_attributes(["UNKNOWN"]) @unittest.skip("deferred (needs workspace)") def test_to_dict(self): desc = self.cube.to_dict() dims = dict((dim.name, dim) for dim in self.dimensions) cube = Cube.from_metadata(desc, dims) self.assertEqual(self.cube.dimensions, cube.dimensions) self.assertEqual(self.cube.measures, cube.measures) self.assertEqual(self.cube, cube) @unittest.skip("requires revision") def test_links(self): # TODO: test link alias! dims = dict((d.name, d) for d in self.dimensions) links = [{"name": "date"}] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) self.assertEqual(len(cube.dimensions), 1) dim = cube.dimension("date") self.assertEqual(len(dim.hierarchies), 2) links = [{"name": "date"}, "product", "flag"] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) self.assertEqual(len(cube.dimensions), 3) self.assertIsInstance(cube.dimension("flag"), Dimension) @unittest.skip("requires revision") def test_link_hierarchies(self): dims = dict((d.name, d) for d in self.dimensions) links = [{"name": "date"}] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) dim = cube.dimension("date") self.assertEqual(len(dim.hierarchies), 2) self.assertEqual(dim.hierarchy().name, "ymd") links = [{"name": "date", "nonadditive":None}] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) dim = cube.dimension("date") self.assertEqual(len(dim.hierarchies), 2) self.assertEqual(dim.hierarchy().name, "ymd") links = [{"name": "date", "hierarchies": ["ym"]}] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) dim = cube.dimension("date") self.assertEqual(len(dim.hierarchies), 1) self.assertEqual(dim.hierarchy().name, "ym") def test_inherit_nonadditive(self): dims = [DIM_DATE_DESC, DIM_PRODUCT_DESC, DIM_FLAG_DESC] cube = { "name": "contracts", "dimensions": ["date", "product"], "nonadditive": "time", "measures": ["amount", "discount"] } dims = [Dimension.from_metadata(md) for md in dims] dims = dict((dim.name, dim) for dim in dims) cube = Cube.from_metadata(cube) measures = cube.measures self.assertEqual(measures[0].nonadditive, "time")
class CubeTestCase(unittest.TestCase): def setUp(self): a = [DIM_DATE_DESC, DIM_PRODUCT_DESC, DIM_FLAG_DESC] self.measures = create_list_of(Measure, ["amount", "discount"]) self.details = create_list_of(Attribute, ["detail"]) self.dimensions = [Dimension.from_metadata(desc) for desc in a] self.cube = Cube("contracts", dimensions=self.dimensions, measures=self.measures, details=self.details) def test_create_cube(self): cube = { "name": "cube", "dimensions": ["date"], "aggregates": ["record_count"], "details": ["some_detail", "another_detail"] } cube = Cube.from_metadata(cube) self.assertEqual(cube.name, "cube") self.assertEqual(len(cube.aggregates), 1) self.assertEqual(len(cube.details), 2) def test_get_dimension(self): self.assertListEqual(self.dimensions, self.cube.dimensions) self.assertEqual("date", self.cube.dimension("date").name) self.assertEqual("product", self.cube.dimension("product").name) self.assertEqual("flag", self.cube.dimension("flag").name) self.assertRaises(NoSuchDimensionError, self.cube.dimension, "xxx") def test_get_measure(self): self.assertListEqual(self.measures, self.cube.measures) self.assertEqual("amount", self.cube.measure("amount").name) self.assertEqual("discount", self.cube.measure("discount").name) self.assertRaises(NoSuchAttributeError, self.cube.measure, "xxx") def test_attributes(self): all_attributes = self.cube.all_attributes refs = [a.ref for a in all_attributes] expected = [ 'date.year', 'date.month', 'date.month_name', 'date.day', 'product.key', 'product.name', 'product.description', 'flag', 'detail', 'amount', 'discount' ] self.assertSequenceEqual(expected, refs) attributes = self.cube.get_attributes(["date.year", "product.name"]) refs = [a.ref for a in attributes] expected = ['date.year', 'product.name'] self.assertSequenceEqual(expected, refs) attributes = self.cube.get_attributes(["amount"]) refs = [a.ref for a in attributes] self.assertSequenceEqual(["amount"], refs) with self.assertRaises(NoSuchAttributeError): self.cube.get_attributes(["UNKNOWN"]) @unittest.skip("deferred (needs workspace)") def test_to_dict(self): desc = self.cube.to_dict() dims = dict((dim.name, dim) for dim in self.dimensions) cube = Cube.from_metadata(desc, dims) self.assertEqual(self.cube.dimensions, cube.dimensions) self.assertEqual(self.cube.measures, cube.measures) self.assertEqual(self.cube, cube) @unittest.skip("requires revision") def test_links(self): # TODO: test link alias! dims = dict((d.name, d) for d in self.dimensions) links = [{"name": "date"}] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) self.assertEqual(len(cube.dimensions), 1) dim = cube.dimension("date") self.assertEqual(len(dim.hierarchies), 2) links = [{"name": "date"}, "product", "flag"] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) self.assertEqual(len(cube.dimensions), 3) self.assertIsInstance(cube.dimension("flag"), Dimension) @unittest.skip("requires revision") def test_link_hierarchies(self): dims = dict((d.name, d) for d in self.dimensions) links = [{"name": "date"}] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) dim = cube.dimension("date") self.assertEqual(len(dim.hierarchies), 2) self.assertEqual(dim.hierarchy().name, "ymd") links = [{"name": "date", "nonadditive": None}] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) dim = cube.dimension("date") self.assertEqual(len(dim.hierarchies), 2) self.assertEqual(dim.hierarchy().name, "ymd") links = [{"name": "date", "hierarchies": ["ym"]}] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) dim = cube.dimension("date") self.assertEqual(len(dim.hierarchies), 1) self.assertEqual(dim.hierarchy().name, "ym") def test_inherit_nonadditive(self): dims = [DIM_DATE_DESC, DIM_PRODUCT_DESC, DIM_FLAG_DESC] cube = { "name": "contracts", "dimensions": ["date", "product"], "nonadditive": "time", "measures": ["amount", "discount"] } dims = [Dimension.from_metadata(md) for md in dims] dims = dict((dim.name, dim) for dim in dims) cube = Cube.from_metadata(cube) measures = cube.measures self.assertEqual(measures[0].nonadditive, "time")
def cube(self, name, locale=None, namespace=None, metaonly=None): if name == "geometry": return getGeomCube(self, metaonly) dataset = get_dataset(name) if name is None: raise NoSuchCubeError("Unknown dataset %s" % name, name) mappings = {} joins = [] fact_table = dataset.source.model.table.name aggregates = [ MeasureAggregate('num_entries', label='Number of entries', function='count') ] measures = [] # "wma": partial(_window_function_factory, window_function=weighted_moving_average, label='Weighted Moving Avg. of {measure}'), # "sma": partial(_window_function_factory, window_function=simple_moving_average, label='Simple Moving Avg. of {measure}'), # "sms": partial(_window_function_factory, window_function=simple_moving_sum, label='Simple Moving Sum of {measure}'), aggregation_funcs = ["sum", "min", "max", "avg"] for measure in dataset.source.model.measures: cubes_measure = Measure(measure.name, label=measure.label) measures.append(cubes_measure) for agg_func in aggregation_funcs: aggregate = MeasureAggregate(measure.name + "_" + agg_func, label=measure.label + agg_func, measure=measure.name, function=agg_func) aggregates.append(aggregate) dimensions = [] for dim in dataset.source.model.dimensions: meta = dim.to_cubes(mappings, joins) meta.update({'name': dim.name, 'label': dim.label}) dimensions.append(Dimension.from_metadata(meta)) cube_meta = { "name": dataset.name, "fact": fact_table, "aggregates": aggregates, "measures": measures, #change these when they get addeed to the model "label": dataset.label, "description": "non null description", "dimensions": dimensions, "store": self.store, "mappings": mappings, "joins": joins } if metaonly: return cube_meta else: return Cube(name=cube_meta['name'], fact=cube_meta['fact'], aggregates=cube_meta['aggregates'], measures=cube_meta['measures'], label=cube_meta['label'], description=cube_meta['description'], dimensions=cube_meta['dimensions'], store=cube_meta['store'], mappings=cube_meta['mappings'], joins=cube_meta['joins'])
def cube(self, name, locale=None, namespace=None): dataset = Dataset.by_name(name) if name is None: raise NoSuchCubeError("Unknown dataset %s" % name, name) measures, dimensions, mappings = [], [], {} aggregates = [ MeasureAggregate('fact_count', label='Number of entries', function='count') ] for measure in dataset.model.measures: cubes_measure = Measure(measure.name, label=measure.label) measures.append(cubes_measure) aggregate = MeasureAggregate(measure.name + '_sum', label=measure.label, measure=measure.name, function='sum') aggregates.append(aggregate) mappings[measure.name] = measure.column for dimension in dataset.model.dimensions: attributes, last_col = [], None for attr in dimension.attributes: attributes.append({'name': attr.name, 'label': attr.label}) mappings[attr.path] = last_col = attr.column # Workaround because the cubes mapper shortens references # for single-attribute dimensions to just the dimension name. if len(attributes) == 1: mappings[dimension.name] = last_col # Translate into cubes' categories cardinality = 'high' if dimension.cardinality: if dimension.cardinality < 6: cardinality = 'tiny' elif dimension.cardinality < 51: cardinality = 'low' elif dimension.cardinality < 1001: cardinality = 'medium' meta = { 'label': dimension.label, 'name': dimension.name, 'cardinality': cardinality, 'levels': [{ 'name': dimension.name, 'label': dimension.label, 'cardinality': cardinality, 'attributes': attributes }] } if dimension.key_attribute: meta['levels'][0]['key'] = dimension.key_attribute.name if dimension.label_attribute: meta['levels'][0]['label_attribute'] = \ dimension.label_attribute.name meta['levels'][0]['order_attribute'] = \ dimension.label_attribute.name dimensions.append(Dimension.from_metadata(meta)) cube = Cube(name=dataset.name, fact=dataset.fact_table.table.name, aggregates=aggregates, measures=measures, label=dataset.label, description=dataset.description, dimensions=dimensions, store=self.store, mappings=mappings) link_cube(cube, locale, provider=self, namespace=namespace) return cube
def test_link_hierarchies(self): dims = dict((d.name, d) for d in self.dimensions) links = [{"name": "date"}] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) dim = cube.dimension("date") self.assertEqual(len(dim.hierarchies), 2) self.assertEqual(dim.hierarchy().name, "ymd") links = [{"name": "date", "nonadditive":None}] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) dim = cube.dimension("date") self.assertEqual(len(dim.hierarchies), 2) self.assertEqual(dim.hierarchy().name, "ymd") links = [{"name": "date", "hierarchies": ["ym"]}] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) dim = cube.dimension("date") self.assertEqual(len(dim.hierarchies), 1) self.assertEqual(dim.hierarchy().name, "ym")