Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generating cube () from configuration data of database #141

Closed
610278470 opened this issue Jun 20, 2019 · 14 comments
Closed

Generating cube () from configuration data of database #141

610278470 opened this issue Jun 20, 2019 · 14 comments
Labels
enhancement New feature proposal
Milestone

Comments

@610278470
Copy link

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

@paveltiunov
Copy link
Member

@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

@610278470
Copy link
Author

@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
Copy link
Author

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

@610278470 610278470 reopened this Jun 20, 2019
@paveltiunov
Copy link
Member

@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 paveltiunov added the enhancement New feature proposal label Jun 20, 2019
@paveltiunov
Copy link
Member

paveltiunov commented Jun 21, 2019

@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
Copy link
Member

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

@paveltiunov paveltiunov added this to the v0.10.0 milestone Jun 21, 2019
@610278470
Copy link
Author

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.

@paveltiunov
Copy link
Member

@610278470 Did you upgrade to 0.10.0?

@610278470
Copy link
Author

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

@paveltiunov
Copy link
Member

@610278470 Does it work?

@610278470
Copy link
Author

@paveltiunov Yes, it's great.

@VP007-py
Copy link

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

@610278470
Copy link
Author

@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.

@manickaws
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature proposal
Projects
None yet
Development

No branches or pull requests

4 participants