cube icon indicating copy to clipboard operation
cube copied to clipboard

Generating cube () from configuration data of database

Open 610278470 opened this issue 7 years ago • 14 comments

Funnel query I want to write the parameters needed for query in the database, and then generate cube () based on these parameters.Thanks.

610278470 avatar Jun 20 '19 12:06 610278470

@610278470 Heya! Could you please provide some more info on the problem you're trying to solve? Any example charts you want to build and data structure you have will be very helpful. Here's some info on Funnel support: https://cube.dev/docs/funnels

paveltiunov avatar Jun 20 '19 16:06 paveltiunov

@paveltiunov Now funnel is configured in the back end and queried through the front end. The query format of funnel is fixed. Now I want to realize that the front end can generate funnel queries at will. For example, the front end selects several events that need to be filtered, and then dynamically generates funnels to query data through the back end.

610278470 avatar Jun 20 '19 16:06 610278470

Is this requirement achievable? I don't see any relevant information in the documents and examples. Thank you.

610278470 avatar Jun 20 '19 16:06 610278470

@610278470 Ah. Gotcha! Yep. Right now what you can do is generate Funnel definitions on the fly if you persist those in some way. Here's an example: https://github.com/statsbotco/cube.js/blob/master/examples/event-analytics/backend/schema/Funnels.js. You can use regular javascript to access database or API to get these descriptions. We still need to remove restrictions on require() within schema so all node_modules can be used. So title for the issue is totally correct. Let's use it to track status of this require() from node_modules implementation within cube.js schema.

paveltiunov avatar Jun 20 '19 22:06 paveltiunov

@610278470 Just released it in 0.10.0. So here's an example of how you can use it in case you have some API for retrieving funnel definitions:

const fetch = require('node-fetch');
const Funnels = require('Funnels');

asyncModule(async () => {
  const funnels = await (await fetch('http://your-api-endpoint/funnels')).json();

  class Funnel {
    constructor({ title, steps }) {
      this.title = title;
      this.steps = steps;
    }

    get transformedSteps() {
      return Object.keys(this.steps).map((key, index) => {
        const value = this.steps[key];
        let where = null
        if (value[0] === PAGE_VIEW_EVENT) {
          if (value.length === 1) {
            where = `event = '${value[0]}'`
          } else {
            where = `event = '${value[0]}' AND page_title = '${value[1]}'`
          }
        } else {
          where = `event = 'se' AND se_category = '${value[0]}' AND se_action = '${value[1]}'`
        }

        return {
          name: key,
          eventsView: {
            sql: () => `select * from (${eventsSQl}) WHERE ${where}`
          },
          timeToConvert: index > 0 ? '30 day' : null
        }
      });
    }

    get config() {
      return {
        userId: {
          sql: () => `user_id`
        },
        time: {
          sql: () => `time`
        },
        steps: this.transformedSteps
      }
    }
  }

  funnels.forEach((funnel) => {
    const funnelObject = new Funnel(funnel);
    cube(funnelObject.title, {
      extends: Funnels.eventFunnel(funnelObject.config),
      preAggregations: {
        main: {
          type: `originalSql`,
        }
      }
    });
  });
})

cc @keydunov

paveltiunov avatar Jun 21 '19 03:06 paveltiunov

@610278470 Could you please try and let me know if it works for you?

paveltiunov avatar Jun 21 '19 05:06 paveltiunov

Error: Compile errors: Package 'node-fetch' not found at ErrorReporter.throwIfAny (/Users/wanzi/Desktop/work/SkyAnalytics/node_modules/@cubejs-backend/schema-compiler/compiler/DataSchemaCompiler.js:42:13) at DataSchemaCompiler.throwIfAnyErrors (/Users/wanzi/Desktop/work/SkyAnalytics/node_modules/@cubejs-backend/schema-compiler/compiler/DataSchemaCompiler.js:149:23) at repository.dataSchemaFiles.then.then (/Users/wanzi/Desktop/work/SkyAnalytics/node_modules/@cubejs-backend/schema-compiler/compiler/DataSchemaCompiler.js:99:14)

@paveltiunov I also introduced the node module in Funnel.js before, and reported this error. This is perfect if the funnel query can be generated based on the query conditions of the front end.

610278470 avatar Jun 21 '19 07:06 610278470

@610278470 Did you upgrade to 0.10.0?

paveltiunov avatar Jun 21 '19 16:06 paveltiunov

@paveltiunov Well, embarrassment. Forget the updated version.Thanks.

610278470 avatar Jun 21 '19 17:06 610278470

@610278470 Does it work?

paveltiunov avatar Jun 21 '19 17:06 paveltiunov

@paveltiunov Yes, it's great.

610278470 avatar Jun 22 '19 12:06 610278470

@610278470 on which database did you try out? Can you elaborate the procedure

ghostaccount737 avatar Jun 27 '19 13:06 ghostaccount737

@PVinay737 Mongodb. There is sample code in the above answer, the actual is to write the title and step parameters required by the custom funnel in the database, and then request the server to get the defined parameters to generate a funnel query.

610278470 avatar Jun 27 '19 14:06 610278470

how do i get data from this dynamic async module? in the dashboard its not listed

manickaws avatar Dec 17 '20 18:12 manickaws