Beispiel #1
0
    def test_aggregate(self):
        md = {}
        with self.assertRaises(ModelError):
            measure = MeasureAggregate.from_metadata(md)

        measure = MeasureAggregate.from_metadata("amount_sum")
        self.assertIsInstance(measure, MeasureAggregate)
        self.assertEqual("amount_sum", measure.name)
Beispiel #2
0
    def test_aggregate(self):
        md = {}
        with self.assertRaises(ModelError):
            measure = MeasureAggregate.from_metadata(md)

        measure = MeasureAggregate.from_metadata("amount_sum")
        self.assertIsInstance(measure, MeasureAggregate)
        self.assertEqual("amount_sum", measure.name)
Beispiel #3
0
    def test_fact_count(self):
        md = {"name": "count", "function": "count"}
        agg = MeasureAggregate.from_metadata(md)

        self.assertEqual("count", agg.name)
        self.assertIsNone(agg.measure)
        self.assertEqual("count", agg.function)
        self.assertIsNone(agg.formula)
Beispiel #4
0
    def test_fact_count(self):
        md = {"name": "count", "function": "count"}
        agg = MeasureAggregate.from_metadata(md)

        self.assertEqual("count", agg.name)
        self.assertIsNone(agg.measure)
        self.assertEqual("count", agg.function)
        self.assertIsNone(agg.formula)
Beispiel #5
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'])
Beispiel #6
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