首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为cube.js动态生成模式?

如何为cube.js动态生成模式?
EN

Stack Overflow用户
提问于 2019-12-06 20:33:52
回答 1查看 983关注 0票数 3

我一直致力于一个项目,以生成可配置的仪表板。所以我必须根据api请求动态地生成模式。有没有办法做到这一点?

这将是非常有帮助的,如果有任何工作的例子!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-07 02:30:19

对于这个场景,有一个asyncModule函数。您可以查看下面的示例:

代码语言:javascript
复制
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`,
        }
      }
    });
  });
})

更多信息:https://cube.dev/docs/schema-execution-environment#async-module

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59213264

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档