Ejemplo n.º 1
0
    def test_template_info(self):
        md = {
            "name": "template",
            "levels": [
                { "name": "one", "info": {"units":"$", "format": "foo"}}
            ]
        }
        tempdim = Dimension.from_metadata(md)

        md = {
            "name": "dim",
            "levels": [
                { "name": "one", "info": {"units":"USD"}}
            ],
            "template": "template"
        }

        templates = {"template": tempdim}
        dim = Dimension.from_metadata(md, templates)

        level = dim.level("one")
        self.assertIn("units", level.info)
        self.assertIn("format", level.info)
        self.assertEqual(level.info["units"], "USD")
        self.assertEqual(level.info["format"], "foo")
Ejemplo n.º 2
0
    def test_template_info(self):
        md = {
            "name": "template",
            "levels": [{
                "name": "one",
                "info": {
                    "units": "$",
                    "format": "foo"
                }
            }]
        }
        tempdim = Dimension.from_metadata(md)

        md = {
            "name": "dim",
            "levels": [{
                "name": "one",
                "info": {
                    "units": "USD"
                }
            }],
            "template": "template"
        }

        templates = {"template": tempdim}
        dim = Dimension.from_metadata(md, templates)

        level = dim.level("one")
        self.assertIn("units", level.info)
        self.assertIn("format", level.info)
        self.assertEqual(level.info["units"], "USD")
        self.assertEqual(level.info["format"], "foo")
Ejemplo n.º 3
0
    def test_template(self):
        dims = {"date": self.dimension}
        desc = {"template": "date", "name": "date"}

        dim = Dimension.from_metadata(desc, dims)
        self.assertEqual(self.dimension, dim)
        hier = dim.hierarchy()
        self.assertEqual(4, len(hier.levels))

        desc["hierarchy"] = ["year", "month"]
        dim = Dimension.from_metadata(desc, dims)
        self.assertEqual(1, len(dim.hierarchies))
        hier = dim.hierarchy()
        self.assertEqual(2, len(hier.levels))

        template = self.dimension.to_dict()
        template["hierarchies"] = [{
            "name": "ym",
            "levels": ["year", "month"]
        }, {
            "name": "ymd",
            "levels": ["year", "month", "day"]
        }]

        template["default_hierarchy_name"] = "ym"
        template = Dimension.from_metadata(template)
        dims = {"date": template}
        desc = {"template": "date", "name": "another_date"}
        dim = Dimension.from_metadata(desc, dims)
        self.assertEqual(2, len(dim.hierarchies))
        self.assertEqual(["ym", "ymd"],
                         [hier.name for hier in dim.hierarchies])
Ejemplo n.º 4
0
    def test_template(self):
        dims = {"date": self.dimension}
        desc = {"template": "date", "name": "date"}

        dim = Dimension.from_metadata(desc, dims)
        self.assertEqual(self.dimension, dim)
        hier = dim.hierarchy()
        self.assertEqual(4, len(hier.levels))

        desc["hierarchy"] = ["year", "month"]
        dim = Dimension.from_metadata(desc, dims)
        self.assertEqual(1, len(dim.hierarchies))
        hier = dim.hierarchy()
        self.assertEqual(2, len(hier.levels))

        template = self.dimension.to_dict()
        template["hierarchies"] = [
            {"name": "ym", "levels": ["year", "month"]},
            {"name": "ymd", "levels": ["year", "month", "day"]}
        ]

        template["default_hierarchy_name"] = "ym"
        template = Dimension.from_metadata(template)
        dims = {"date": template}
        desc = {"template": "date", "name":"another_date"}
        dim = Dimension.from_metadata(desc, dims)
        self.assertEqual(2, len(dim.hierarchies))
        self.assertEqual(["ym", "ymd"],
                         [hier.name for hier in dim.hierarchies])
Ejemplo n.º 5
0
    def test_template_hierarchies(self):
        md = {
            "name": "time",
            "levels": ["year", "month", "day", "hour"],
            "hierarchies": [
                {"name": "full", "levels": ["year", "month", "day", "hour"]},
                {"name": "ymd", "levels": ["year", "month", "day"]},
                {"name": "ym", "levels": ["year", "month"]},
                {"name": "y", "levels": ["year"]},
            ]
        }
        dim_time = Dimension.from_metadata(md)
        templates = {"time": dim_time}
        md = {
            "name": "date",
            "template": "time",
            "hierarchies": [
                "ymd", "ym", "y"
            ]
        }

        dim_date = Dimension.from_metadata(md, templates)

        self.assertEqual(dim_date.name, "date")
        self.assertEqual(len(dim_date.hierarchies), 3)
        names = [h.name for h in dim_date.hierarchies]
        self.assertEqual(["ymd", "ym", "y"], names)
Ejemplo n.º 6
0
    def test_comparisons(self):
        """Comparison of dimension instances"""

        dim1 = Dimension.from_metadata(DIM_DATE_DESC)
        dim2 = Dimension.from_metadata(DIM_DATE_DESC)

        self.assertListEqual(dim1.levels, dim2.levels)
        self.assertListEqual(dim1.hierarchies, dim2.hierarchies)

        self.assertEqual(dim1, dim2)
Ejemplo n.º 7
0
    def test_comparisons(self):
        """Comparison of dimension instances"""

        dim1 = Dimension.from_metadata(DIM_DATE_DESC)
        dim2 = Dimension.from_metadata(DIM_DATE_DESC)

        self.assertListEqual(dim1.levels, dim2.levels)
        self.assertListEqual(dim1.hierarchies, dim2.hierarchies)

        self.assertEqual(dim1, dim2)
Ejemplo n.º 8
0
    def setUp(self):
        self.levels = [
            Level("year", attributes=create_list_of(Attribute, ["year"])),
            Level("month",
                  attributes=create_list_of(
                      Attribute, ["month", "month_name", "month_sname"])),
            Level("day", attributes=create_list_of(Attribute, ["day"])),
            Level("week", attributes=create_list_of(Attribute, ["week"]))
        ]
        self.level_names = [level.name for level in self.levels]
        self.dimension = Dimension("date", levels=self.levels)

        levels = [self.levels[0], self.levels[1], self.levels[2]]
        self.hierarchy = Hierarchy("default", levels)
Ejemplo n.º 9
0
    def test_to_dict(self):
        desc = self.dimension.to_dict()
        dim = Dimension.from_metadata(desc)

        self.assertEqual(self.dimension.hierarchies, dim.hierarchies)
        self.assertEqual(self.dimension.levels, dim.levels)
        self.assertEqual(self.dimension, dim)
Ejemplo n.º 10
0
    def test_to_dict(self):
        desc = self.dimension.to_dict()
        dim = Dimension.from_metadata(desc)

        self.assertEqual(self.dimension.hierarchies, dim.hierarchies)
        self.assertEqual(self.dimension.levels, dim.levels)
        self.assertEqual(self.dimension, dim)
Ejemplo n.º 11
0
    def test_simplify(self):
        """Simplification of attribute reference (with and without details)"""

        level = Level("name", attributes=[Attribute("name")])
        dim = Dimension("group", levels=[level])
        attr = dim.attribute("name")
        self.assertEqual("name", attr.name)

        # Simplified -> dimension name
        self.assertEqual("group", str(attr))
        self.assertEqual("group", attr.ref)

        level = Level("name", attributes=[Attribute("key"), Attribute("name")])
        dim = Dimension("group", levels=[level])
        attr = dim.attribute("name")
        self.assertEqual("name", attr.name)
        self.assertEqual("group.name", str(attr))
        self.assertEqual("group.name", attr.ref)
Ejemplo n.º 12
0
 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)
Ejemplo n.º 13
0
 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)
Ejemplo n.º 14
0
    def test_create(self):
        """Dimension from a dictionary"""
        dim = Dimension.from_metadata("year")
        self.assertIsInstance(dim, Dimension)
        self.assertEqual("year", dim.name)
        self.assertEqual(["year"], [str(a) for a in dim.attributes])

        # Test default: explicit level attributes
        desc = {"name": "date", "levels": ["year"]}
        dim = Dimension.from_metadata(desc)
        self.assertTrue(dim.is_flat)
        self.assertFalse(dim.has_details)
        self.assertIsInstance(dim, Dimension)
        self.assertEqual("date", dim.name)

        # Dimension string is a reference and is equal to dimension name if
        # dimension is flat
        self.assertEqual(["date"], [str(a) for a in dim.attributes])

        desc = {"name": "date", "levels": ["year", "month", "day"]}
        dim = Dimension.from_metadata(desc)
        self.assertIsInstance(dim, Dimension)
        self.assertEqual("date", dim.name)
        refs = [str(a) for a in dim.attributes]
        self.assertEqual(["date.year", "date.month", "date.day"], refs)
        self.assertFalse(dim.is_flat)
        self.assertFalse(dim.has_details)
        self.assertEqual(3, len(dim.levels))
        for level in dim.levels:
            self.assertIsInstance(level, Level)
        self.assertEqual(1, len(dim.hierarchies))
        self.assertEqual(3, len(dim.hierarchy()))

        # Test default: implicit single level attributes
        desc = {"name": "product", "attributes": ["code", "name"]}
        dim = Dimension.from_metadata(desc)
        refs = [str(a) for a in dim.attributes]
        self.assertEqual(["product.code", "product.name"], refs)
        self.assertEqual(1, len(dim.levels))
        self.assertEqual(1, len(dim.hierarchies))
Ejemplo n.º 15
0
    def test_create(self):
        """Dimension from a dictionary"""
        dim = Dimension.from_metadata("year")
        self.assertIsInstance(dim, Dimension)
        self.assertEqual("year", dim.name)
        self.assertEqual(["year"], [str(a) for a in dim.attributes])

        # Test default: explicit level attributes
        desc = {"name": "date", "levels": ["year"]}
        dim = Dimension.from_metadata(desc)
        self.assertTrue(dim.is_flat)
        self.assertFalse(dim.has_details)
        self.assertIsInstance(dim, Dimension)
        self.assertEqual("date", dim.name)

        # Dimension string is a reference and is equal to dimension name if
        # dimension is flat
        self.assertEqual(["date"], [str(a) for a in dim.attributes])

        desc = {"name": "date", "levels": ["year", "month", "day"]}
        dim = Dimension.from_metadata(desc)
        self.assertIsInstance(dim, Dimension)
        self.assertEqual("date", dim.name)
        refs = [str(a) for a in dim.attributes]
        self.assertEqual(["date.year", "date.month", "date.day"], refs)
        self.assertFalse(dim.is_flat)
        self.assertFalse(dim.has_details)
        self.assertEqual(3, len(dim.levels))
        for level in dim.levels:
            self.assertIsInstance(level, Level)
        self.assertEqual(1, len(dim.hierarchies))
        self.assertEqual(3, len(dim.hierarchy()))

        # Test default: implicit single level attributes
        desc = {"name": "product", "attributes": ["code", "name"]}
        dim = Dimension.from_metadata(desc)
        refs = [str(a) for a in dim.attributes]
        self.assertEqual(["product.code", "product.name"], refs)
        self.assertEqual(1, len(dim.levels))
        self.assertEqual(1, len(dim.hierarchies))
Ejemplo n.º 16
0
    def test_create_attribute(self):
        """Coalesce attribute object (string or Attribute instance)"""

        level = Level("name", attributes=[Attribute("key"), Attribute("name")])
        dim = Dimension("group", levels=[level])

        obj = Attribute.from_metadata("name")
        self.assertIsInstance(obj, Attribute)
        self.assertEqual("name", obj.name)

        obj = Attribute.from_metadata({"name": "key"})
        obj.dimension = dim
        self.assertIsInstance(obj, Attribute)
        self.assertEqual("key", obj.name)
        self.assertEqual(dim, obj.dimension)

        attr = dim.attribute("key")
        obj = Attribute.from_metadata(attr)
        obj.dimension = dim
        self.assertIsInstance(obj, Attribute)
        self.assertEqual("key", obj.name)
        self.assertEqual(obj, attr)
Ejemplo n.º 17
0
    def setUp(self):
        self.levels = [
            Level("year", attributes=create_list_of(Attribute, ["year"])),
            Level("month", attributes=create_list_of(Attribute, ["month", "month_name",
                                             "month_sname"])),
            Level("day", attributes=create_list_of(Attribute, ["day"])),
            Level("week", attributes=create_list_of(Attribute, ["week"]))
        ]
        self.level_names = [level.name for level in self.levels]
        self.dimension = Dimension("date", levels=self.levels)

        levels = [self.levels[0], self.levels[1], self.levels[2]]
        self.hierarchy = Hierarchy("default", levels)
Ejemplo n.º 18
0
    def test_create_attribute(self):
        """Coalesce attribute object (string or Attribute instance)"""

        level = Level("name", attributes=[Attribute("key"), Attribute("name")])
        dim = Dimension("group", levels=[level])

        obj = Attribute.from_metadata("name")
        self.assertIsInstance(obj, Attribute)
        self.assertEqual("name", obj.name)

        obj = Attribute.from_metadata({"name": "key"})
        obj.dimension = dim
        self.assertIsInstance(obj, Attribute)
        self.assertEqual("key", obj.name)
        self.assertEqual(dim, obj.dimension)

        attr = dim.attribute("key")
        obj = Attribute.from_metadata(attr)
        obj.dimension = dim
        self.assertIsInstance(obj, Attribute)
        self.assertEqual("key", obj.name)
        self.assertEqual(obj, attr)
Ejemplo n.º 19
0
    def test_template_hierarchies(self):
        md = {
            "name":
            "time",
            "levels": ["year", "month", "day", "hour"],
            "hierarchies": [
                {
                    "name": "full",
                    "levels": ["year", "month", "day", "hour"]
                },
                {
                    "name": "ymd",
                    "levels": ["year", "month", "day"]
                },
                {
                    "name": "ym",
                    "levels": ["year", "month"]
                },
                {
                    "name": "y",
                    "levels": ["year"]
                },
            ]
        }
        dim_time = Dimension.from_metadata(md)
        templates = {"time": dim_time}
        md = {
            "name": "date",
            "template": "time",
            "hierarchies": ["ymd", "ym", "y"]
        }

        dim_date = Dimension.from_metadata(md, templates)

        self.assertEqual(dim_date.name, "date")
        self.assertEqual(len(dim_date.hierarchies), 3)
        names = [h.name for h in dim_date.hierarchies]
        self.assertEqual(["ymd", "ym", "y"], names)
Ejemplo n.º 20
0
 def setUp(self):
     self.levels = [
         Level("year", attributes=[Attribute("year")]),
         Level("month",
               attributes=[
                   Attribute("month"),
                   Attribute("month_name"),
                   Attribute("month_sname")
               ]),
         Level("day", attributes=[Attribute("day")]),
         Level("week", attributes=[Attribute("week")])
     ]
     self.level_names = [level.name for level in self.levels]
     self.dimension = Dimension("date", levels=self.levels)
     levels = [self.levels[0], self.levels[1], self.levels[2]]
     self.hierarchy = Hierarchy("default", levels)
Ejemplo n.º 21
0
    def test_flat_dimension(self):
        """Flat dimension and 'has details' flags"""
        dim = Dimension.from_metadata("foo")
        self.assertTrue(dim.is_flat)
        self.assertFalse(dim.has_details)
        self.assertEqual(1, len(dim.levels))

        level = dim.level("foo")
        self.assertIsInstance(level, Level)
        self.assertEqual("foo", level.name)
        self.assertEqual(1, len(level.attributes))
        self.assertEqual("foo", str(level.key))

        attr = level.attributes[0]
        self.assertIsInstance(attr, Attribute)
        self.assertEqual("foo", attr.name)
Ejemplo n.º 22
0
    def test_flat_dimension(self):
        """Flat dimension and 'has details' flags"""
        dim = Dimension.from_metadata("foo")
        self.assertTrue(dim.is_flat)
        self.assertFalse(dim.has_details)
        self.assertEqual(1, len(dim.levels))

        level = dim.level("foo")
        self.assertIsInstance(level, Level)
        self.assertEqual("foo", level.name)
        self.assertEqual(1, len(level.attributes))
        self.assertEqual("foo", str(level.key))

        attr = level.attributes[0]
        self.assertIsInstance(attr, Attribute)
        self.assertEqual("foo", attr.name)
Ejemplo n.º 23
0
    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")
Ejemplo n.º 24
0
    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")
Ejemplo n.º 25
0
    def test_simplify(self):
        """Simplification of attribute reference (with and without details)"""

        level = Level("name", attributes=[Attribute("name")])
        dim = Dimension("group", levels=[level])
        attr = dim.attribute("name")
        self.assertEqual("name", attr.name)

        # Simplified -> dimension name
        self.assertEqual("group", str(attr))
        self.assertEqual("group", attr.ref)

        level = Level("name", attributes=[Attribute("key"), Attribute("name")])
        dim = Dimension("group", levels=[level])
        attr = dim.attribute("name")
        self.assertEqual("name", attr.name)
        self.assertEqual("group.name", str(attr))
        self.assertEqual("group.name", attr.ref)
Ejemplo n.º 26
0
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'])
Ejemplo n.º 27
0
    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
Ejemplo n.º 28
0
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'])
Ejemplo n.º 29
0
class DimensionTestCase(unittest.TestCase):
    def setUp(self):
        self.levels = [
            Level("year", attributes=create_list_of(Attribute, ["year"])),
            Level("month", attributes=create_list_of(Attribute, ["month", "month_name",
                                             "month_sname"])),
            Level("day", attributes=create_list_of(Attribute, ["day"])),
            Level("week", attributes=create_list_of(Attribute, ["week"]))
        ]
        self.level_names = [level.name for level in self.levels]
        self.dimension = Dimension("date", levels=self.levels)

        levels = [self.levels[0], self.levels[1], self.levels[2]]
        self.hierarchy = Hierarchy("default", levels)

    def test_create(self):
        """Dimension from a dictionary"""
        dim = Dimension.from_metadata("year")
        self.assertIsInstance(dim, Dimension)
        self.assertEqual("year", dim.name)
        self.assertEqual(["year"], [str(a) for a in dim.attributes])

        # Test default: explicit level attributes
        desc = {"name": "date", "levels": ["year"]}
        dim = Dimension.from_metadata(desc)
        self.assertTrue(dim.is_flat)
        self.assertFalse(dim.has_details)
        self.assertIsInstance(dim, Dimension)
        self.assertEqual("date", dim.name)

        # Dimension string is a reference and is equal to dimension name if
        # dimension is flat
        self.assertEqual(["date"], [str(a) for a in dim.attributes])

        desc = {"name": "date", "levels": ["year", "month", "day"]}
        dim = Dimension.from_metadata(desc)
        self.assertIsInstance(dim, Dimension)
        self.assertEqual("date", dim.name)
        refs = [str(a) for a in dim.attributes]
        self.assertEqual(["date.year", "date.month", "date.day"], refs)
        self.assertFalse(dim.is_flat)
        self.assertFalse(dim.has_details)
        self.assertEqual(3, len(dim.levels))
        for level in dim.levels:
            self.assertIsInstance(level, Level)
        self.assertEqual(1, len(dim.hierarchies))
        self.assertEqual(3, len(dim.hierarchy()))

        # Test default: implicit single level attributes
        desc = {"name": "product", "attributes": ["code", "name"]}
        dim = Dimension.from_metadata(desc)
        refs = [str(a) for a in dim.attributes]
        self.assertEqual(["product.code", "product.name"], refs)
        self.assertEqual(1, len(dim.levels))
        self.assertEqual(1, len(dim.hierarchies))

    def test_flat_dimension(self):
        """Flat dimension and 'has details' flags"""
        dim = Dimension.from_metadata("foo")
        self.assertTrue(dim.is_flat)
        self.assertFalse(dim.has_details)
        self.assertEqual(1, len(dim.levels))

        level = dim.level("foo")
        self.assertIsInstance(level, Level)
        self.assertEqual("foo", level.name)
        self.assertEqual(1, len(level.attributes))
        self.assertEqual("foo", str(level.key))

        attr = level.attributes[0]
        self.assertIsInstance(attr, Attribute)
        self.assertEqual("foo", attr.name)

    def test_comparisons(self):
        """Comparison of dimension instances"""

        dim1 = Dimension.from_metadata(DIM_DATE_DESC)
        dim2 = Dimension.from_metadata(DIM_DATE_DESC)

        self.assertListEqual(dim1.levels, dim2.levels)
        self.assertListEqual(dim1.hierarchies, dim2.hierarchies)

        self.assertEqual(dim1, dim2)

    def test_to_dict(self):
        desc = self.dimension.to_dict()
        dim = Dimension.from_metadata(desc)

        self.assertEqual(self.dimension.hierarchies, dim.hierarchies)
        self.assertEqual(self.dimension.levels, dim.levels)
        self.assertEqual(self.dimension, dim)

    def test_template(self):
        dims = {"date": self.dimension}
        desc = {"template": "date", "name": "date"}

        dim = Dimension.from_metadata(desc, dims)
        self.assertEqual(self.dimension, dim)
        hier = dim.hierarchy()
        self.assertEqual(4, len(hier.levels))

        desc["hierarchy"] = ["year", "month"]
        dim = Dimension.from_metadata(desc, dims)
        self.assertEqual(1, len(dim.hierarchies))
        hier = dim.hierarchy()
        self.assertEqual(2, len(hier.levels))

        template = self.dimension.to_dict()
        template["hierarchies"] = [
            {"name": "ym", "levels": ["year", "month"]},
            {"name": "ymd", "levels": ["year", "month", "day"]}
        ]

        template["default_hierarchy_name"] = "ym"
        template = Dimension.from_metadata(template)
        dims = {"date": template}
        desc = {"template": "date", "name":"another_date"}
        dim = Dimension.from_metadata(desc, dims)
        self.assertEqual(2, len(dim.hierarchies))
        self.assertEqual(["ym", "ymd"],
                         [hier.name for hier in dim.hierarchies])

    def test_template_hierarchies(self):
        md = {
            "name": "time",
            "levels": ["year", "month", "day", "hour"],
            "hierarchies": [
                {"name": "full", "levels": ["year", "month", "day", "hour"]},
                {"name": "ymd", "levels": ["year", "month", "day"]},
                {"name": "ym", "levels": ["year", "month"]},
                {"name": "y", "levels": ["year"]},
            ]
        }
        dim_time = Dimension.from_metadata(md)
        templates = {"time": dim_time}
        md = {
            "name": "date",
            "template": "time",
            "hierarchies": [
                "ymd", "ym", "y"
            ]
        }

        dim_date = Dimension.from_metadata(md, templates)

        self.assertEqual(dim_date.name, "date")
        self.assertEqual(len(dim_date.hierarchies), 3)
        names = [h.name for h in dim_date.hierarchies]
        self.assertEqual(["ymd", "ym", "y"], names)

    def test_template_info(self):
        md = {
            "name": "template",
            "levels": [
                { "name": "one", "info": {"units":"$", "format": "foo"}}
            ]
        }
        tempdim = Dimension.from_metadata(md)

        md = {
            "name": "dim",
            "levels": [
                { "name": "one", "info": {"units":"USD"}}
            ],
            "template": "template"
        }

        templates = {"template": tempdim}
        dim = Dimension.from_metadata(md, templates)

        level = dim.level("one")
        self.assertIn("units", level.info)
        self.assertIn("format", level.info)
        self.assertEqual(level.info["units"], "USD")
        self.assertEqual(level.info["format"], "foo")
Ejemplo n.º 30
0
    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
Ejemplo n.º 31
0
    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'])
Ejemplo n.º 32
0
class DimensionTestCase(unittest.TestCase):
    def setUp(self):
        self.levels = [
            Level("year", attributes=create_list_of(Attribute, ["year"])),
            Level("month",
                  attributes=create_list_of(
                      Attribute, ["month", "month_name", "month_sname"])),
            Level("day", attributes=create_list_of(Attribute, ["day"])),
            Level("week", attributes=create_list_of(Attribute, ["week"]))
        ]
        self.level_names = [level.name for level in self.levels]
        self.dimension = Dimension("date", levels=self.levels)

        levels = [self.levels[0], self.levels[1], self.levels[2]]
        self.hierarchy = Hierarchy("default", levels)

    def test_create(self):
        """Dimension from a dictionary"""
        dim = Dimension.from_metadata("year")
        self.assertIsInstance(dim, Dimension)
        self.assertEqual("year", dim.name)
        self.assertEqual(["year"], [str(a) for a in dim.attributes])

        # Test default: explicit level attributes
        desc = {"name": "date", "levels": ["year"]}
        dim = Dimension.from_metadata(desc)
        self.assertTrue(dim.is_flat)
        self.assertFalse(dim.has_details)
        self.assertIsInstance(dim, Dimension)
        self.assertEqual("date", dim.name)

        # Dimension string is a reference and is equal to dimension name if
        # dimension is flat
        self.assertEqual(["date"], [str(a) for a in dim.attributes])

        desc = {"name": "date", "levels": ["year", "month", "day"]}
        dim = Dimension.from_metadata(desc)
        self.assertIsInstance(dim, Dimension)
        self.assertEqual("date", dim.name)
        refs = [str(a) for a in dim.attributes]
        self.assertEqual(["date.year", "date.month", "date.day"], refs)
        self.assertFalse(dim.is_flat)
        self.assertFalse(dim.has_details)
        self.assertEqual(3, len(dim.levels))
        for level in dim.levels:
            self.assertIsInstance(level, Level)
        self.assertEqual(1, len(dim.hierarchies))
        self.assertEqual(3, len(dim.hierarchy()))

        # Test default: implicit single level attributes
        desc = {"name": "product", "attributes": ["code", "name"]}
        dim = Dimension.from_metadata(desc)
        refs = [str(a) for a in dim.attributes]
        self.assertEqual(["product.code", "product.name"], refs)
        self.assertEqual(1, len(dim.levels))
        self.assertEqual(1, len(dim.hierarchies))

    def test_flat_dimension(self):
        """Flat dimension and 'has details' flags"""
        dim = Dimension.from_metadata("foo")
        self.assertTrue(dim.is_flat)
        self.assertFalse(dim.has_details)
        self.assertEqual(1, len(dim.levels))

        level = dim.level("foo")
        self.assertIsInstance(level, Level)
        self.assertEqual("foo", level.name)
        self.assertEqual(1, len(level.attributes))
        self.assertEqual("foo", str(level.key))

        attr = level.attributes[0]
        self.assertIsInstance(attr, Attribute)
        self.assertEqual("foo", attr.name)

    def test_comparisons(self):
        """Comparison of dimension instances"""

        dim1 = Dimension.from_metadata(DIM_DATE_DESC)
        dim2 = Dimension.from_metadata(DIM_DATE_DESC)

        self.assertListEqual(dim1.levels, dim2.levels)
        self.assertListEqual(dim1.hierarchies, dim2.hierarchies)

        self.assertEqual(dim1, dim2)

    def test_to_dict(self):
        desc = self.dimension.to_dict()
        dim = Dimension.from_metadata(desc)

        self.assertEqual(self.dimension.hierarchies, dim.hierarchies)
        self.assertEqual(self.dimension.levels, dim.levels)
        self.assertEqual(self.dimension, dim)

    def test_template(self):
        dims = {"date": self.dimension}
        desc = {"template": "date", "name": "date"}

        dim = Dimension.from_metadata(desc, dims)
        self.assertEqual(self.dimension, dim)
        hier = dim.hierarchy()
        self.assertEqual(4, len(hier.levels))

        desc["hierarchy"] = ["year", "month"]
        dim = Dimension.from_metadata(desc, dims)
        self.assertEqual(1, len(dim.hierarchies))
        hier = dim.hierarchy()
        self.assertEqual(2, len(hier.levels))

        template = self.dimension.to_dict()
        template["hierarchies"] = [{
            "name": "ym",
            "levels": ["year", "month"]
        }, {
            "name": "ymd",
            "levels": ["year", "month", "day"]
        }]

        template["default_hierarchy_name"] = "ym"
        template = Dimension.from_metadata(template)
        dims = {"date": template}
        desc = {"template": "date", "name": "another_date"}
        dim = Dimension.from_metadata(desc, dims)
        self.assertEqual(2, len(dim.hierarchies))
        self.assertEqual(["ym", "ymd"],
                         [hier.name for hier in dim.hierarchies])

    def test_template_hierarchies(self):
        md = {
            "name":
            "time",
            "levels": ["year", "month", "day", "hour"],
            "hierarchies": [
                {
                    "name": "full",
                    "levels": ["year", "month", "day", "hour"]
                },
                {
                    "name": "ymd",
                    "levels": ["year", "month", "day"]
                },
                {
                    "name": "ym",
                    "levels": ["year", "month"]
                },
                {
                    "name": "y",
                    "levels": ["year"]
                },
            ]
        }
        dim_time = Dimension.from_metadata(md)
        templates = {"time": dim_time}
        md = {
            "name": "date",
            "template": "time",
            "hierarchies": ["ymd", "ym", "y"]
        }

        dim_date = Dimension.from_metadata(md, templates)

        self.assertEqual(dim_date.name, "date")
        self.assertEqual(len(dim_date.hierarchies), 3)
        names = [h.name for h in dim_date.hierarchies]
        self.assertEqual(["ymd", "ym", "y"], names)

    def test_template_info(self):
        md = {
            "name": "template",
            "levels": [{
                "name": "one",
                "info": {
                    "units": "$",
                    "format": "foo"
                }
            }]
        }
        tempdim = Dimension.from_metadata(md)

        md = {
            "name": "dim",
            "levels": [{
                "name": "one",
                "info": {
                    "units": "USD"
                }
            }],
            "template": "template"
        }

        templates = {"template": tempdim}
        dim = Dimension.from_metadata(md, templates)

        level = dim.level("one")
        self.assertIn("units", level.info)
        self.assertIn("format", level.info)
        self.assertEqual(level.info["units"], "USD")
        self.assertEqual(level.info["format"], "foo")
Ejemplo n.º 33
0
    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'])
Ejemplo n.º 34
0
    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_name

        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.ref] = last_col = attr.column_name

            # 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

            meta = {
                'label': dimension.label,
                'name': dimension.name,
                'cardinality': dimension.cardinality_class,
                'levels': [{
                    'name': dimension.name,
                    'label': dimension.label,
                    'cardinality': dimension.cardinality_class,
                    '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