Example #1
0
    def test_basic(self):
        md = {}
        with self.assertRaises(ModelError):
            measure = Measure.from_metadata(md)

        measure = Measure.from_metadata("amount")
        self.assertIsInstance(measure, Measure)
        self.assertEqual("amount", measure.name)

        md = {"name": "amount"}
        measure = Measure.from_metadata(md)
        self.assertEqual("amount", measure.name)
Example #2
0
    def test_basic(self):
        md = {}
        with self.assertRaises(ModelError):
            measure = Measure.from_metadata(md)

        measure = Measure.from_metadata("amount")
        self.assertIsInstance(measure, Measure)
        self.assertEqual("amount", measure.name)

        md = {"name": "amount"}
        measure = Measure.from_metadata(md)
        self.assertEqual("amount", measure.name)
Example #3
0
    def test_create_default_aggregates(self):
        measure = Measure.from_metadata("amount")
        aggs = measure.default_aggregates()
        self.assertEqual(1, len(aggs))
        agg = aggs[0]
        self.assertEqual("amount_sum", agg.name)
        self.assertEqual("amount", agg.measure)
        self.assertEqual("sum", agg.function)
        self.assertIsNone(agg.formula)

        md = {"name": "amount", "aggregates": ["sum", "min"]}
        measure = Measure.from_metadata(md)
        aggs = measure.default_aggregates()
        self.assertEqual(2, len(aggs))
        self.assertEqual("amount_sum", aggs[0].name)
        self.assertEqual("amount", aggs[0].measure)
        self.assertEqual("sum", aggs[0].function)
        self.assertIsNone(aggs[0].formula)

        self.assertEqual("amount_min", aggs[1].name)
        self.assertEqual("amount", aggs[1].measure)
        self.assertEqual("min", aggs[1].function)
        self.assertIsNone(aggs[1].formula)
Example #4
0
    def test_create_default_aggregates(self):
        measure = Measure.from_metadata("amount")
        aggs = measure.default_aggregates()
        self.assertEqual(1, len(aggs))
        agg = aggs[0]
        self.assertEqual("amount_sum", agg.name)
        self.assertEqual("amount", agg.measure)
        self.assertEqual("sum", agg.function)
        self.assertIsNone(agg.formula)

        md = {"name": "amount", "aggregates": ["sum", "min"]}
        measure = Measure.from_metadata(md)
        aggs = measure.default_aggregates()
        self.assertEqual(2, len(aggs))
        self.assertEqual("amount_sum", aggs[0].name)
        self.assertEqual("amount", aggs[0].measure)
        self.assertEqual("sum", aggs[0].function)
        self.assertIsNone(aggs[0].formula)

        self.assertEqual("amount_min", aggs[1].name)
        self.assertEqual("amount", aggs[1].measure)
        self.assertEqual("min", aggs[1].function)
        self.assertIsNone(aggs[1].formula)
Example #5
0
 def test_copy(self):
     md = {"name": "amount"}
     measure = Measure.from_metadata(md)
     measure2 = copy.deepcopy(measure)
     self.assertEqual(measure, measure2)
Example #6
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'])
Example #7
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
Example #8
0
 def test_copy(self):
     md = {"name": "amount"}
     measure = Measure.from_metadata(md)
     measure2 = copy.deepcopy(measure)
     self.assertEqual(measure, measure2)