cube-js / cube

📊 Cube — The Semantic Layer for Building Data Applications
https://cube.dev
Other
17.76k stars 1.76k forks source link

How to use Pre-aggregation when creating dynamic schema #6422

Open wkyhit opened 1 year ago

wkyhit commented 1 year ago

Problem

I create dynamic cubes according to this tutorial: [https://cube.dev/docs/schema/advanced/dynamic-schema-creation] And I have question about how to set pre-aggregation field as the measures and the dimensions in cubes are not known in advance. If I just simply fill the pre-aggregation with a string field, the pre-aggregation will not take effect. Here is the example code below:

Related Cube.js schema



asyncModule(async () => {
    const {
        securityContext: {cubeId},
    } = COMPILE_CONTEXT;

    const dynamicCubes = await (
        await fetch(`http://` + serverAddress + `/api/v1/cubes/${cubeId}`)
    ).json();

    if (dynamicCubes.forEach) {

        dynamicCubes.forEach(dynamicCube => {
            dynamicCube.id = 'ns_' + dynamicCube.id.replace(/-/g, '_')
        })

        dynamicCubes?.forEach(dynamicCube => {
            const dimensions = transformDimensions(dynamicCube.dimensions);
            const measures = transformMeasures(dynamicCube.measures);
            cube(dynamicCube.id, {
                name: dynamicCube.name,
                sql: dynamicCube.sql,
                title: dynamicCube.title,
                dataSource: dynamicCube.dataSource,
                dimensions,
                measures,
               preAggregations:{
                    main: {
                        type: 'rollup',
                        measures: Object.keys(measures).map(key => `CUBE.${key}`),
                        dimensions: Object.keys(dimensions).map(key => `CUBE.${key}`),
                        granularity: `day`,
                    }
                }
            });
        });
    } else {
        logger.log("error", "dynamicCubes is null, detail:" + JSON.stringify(dynamicCubes))
    }
})```
spljs commented 1 year ago

Hello, is there any fix to his ? I'm having the same problem with the following error

From members are not found in [] for join {\"join\":{\"relationship\":\"hasMany\"},\"from\":\"mypipeline_test_names\",\"to\":\"mypipeline_users_stats\",\"originalFrom\":\"mypipeline_test_names\",\"originalTo\":\"mypipeline_users_stats\"}. Please make sure join fields are referencing dimensions instead of columns.

Joins are good & rollups are good

KroneCorylus commented 10 months ago

Hello, some insight on this ?

KroneCorylus commented 10 months ago

Hi, i made it work.

1) the measures and dimensions properties of your preaggregations must be of type() => string[] where the strings are formated like so: "cube_name.measure_name" or "cube_name.dimensionname" 2) the timeDimension property of your pre_aggregations must be of type() => string formated in the same way.

so @wkyhit you can iterate over your desired measures and dimensions and generate those strings.