import Magix from 'magix' import View from 'dmp-new/view' import CONSTANT from '../constant' Magix.applyStyle('@./crowd-distribute.less') const BLOCK_HEIGHT = 80 const BLOCK_GAP = 10 export default View.extend({ tmpl: '@crowd-distribute.html', init(options) { this.options = options }, async render() { const computeTypes = [ { text: '按曝光看效果', value: '1' }, { text: '按点击看效果', value: '2' } ] const { mountedCallback, module_setting, query } = this.options // 根据 bpdefine 的 module_setting 过滤出当前可用的指标 const metrics = this.filterMetrics( CONSTANT.metrics, module_setting.find( module => module.moduleId == CONSTANT.DISTRIBUTE_MODULE_ID ) ) this.updater.set({ metrics }) // 自定义指标相关,默认取前 6 个 const defaults = [ ...metrics.filter((metric, i) => i < 6).map(metric => metric.key) ] const { computeType = computeTypes[0].value, // 效果 custom = false, // 是否自定义指标 customs = defaults // 用户选择的指标 } = this.updater.get() const selectedMetrics = this.getSelectedMetrics(customs) // 指标 // render this.updater.digest({ metrics, selectedMetrics, custom, customs, defaults, CONSTANT, query, loading: true, computeType, computeTypes, BLOCK_HEIGHT, BLOCK_GAP, error: null }) try { const data = await this.getData() const distributeData = this.formatData(data) this.updater.digest({ data, distributeData, loading: false }) } catch (error) { this.updater.digest({ loading: false, error: error.msg ?? error.messgae }) } //模块请求完毕回调通知外面,加载下一个模块 mountedCallback && mountedCallback() }, // 根据 bpdefine 的 module_setting 过滤出当前可用的指标 filterMetrics(allMetrics, moduleSetting) { const result = allMetrics.filter(metric => moduleSetting.targets.find(target => target === metric.key) ) return result }, // formatData(data) { const headersKey = 'DEEPLINK开始状态' const { post_imp = [], pre = [], all_pre = [], all_post_imp = [] } = data const statusListDistribute = $.extend( true, [], CONSTANT.statusListDistribute ) const frontMetrics = [...pre, ...all_pre] //前链路指标 const endMetrics = [...post_imp, ...all_post_imp] //后链路指标 // 只有 all 数据的情况 if ( frontMetrics.length === 1 && frontMetrics[0].headers[headersKey] === 'all' ) { statusListDistribute.length = 1 // } // 以分层人群为基准,从接口里获取对应的前后链路数据 for (const group of statusListDistribute) { for (const dist of group.subs) { const preTarget = frontMetrics.find( item => item?.headers[headersKey] === dist.key ) const postImpTarget = endMetrics.find( item => item?.headers[headersKey] === dist.key ) dist.targets = { ...preTarget?.targets, ...postImpTarget?.targets } } } return statusListDistribute }, async getData() { const { query, bp } = this.options const { computeType } = this.updater.get() const params = { query, bp, moduleId: CONSTANT.DISTRIBUTE_MODULE_ID, extQuery: { computeType } } const models = [ { name: 'api_quickcase_moduledatav2_post', isJson: true, params } ] const [model] = await this.fetch(models) const data = model.get('data.rpt', {}) return data }, getSelectedMetrics(customs = []) { const { metrics } = this.updater.get() const result = [] for (const metric of customs) { result.push(metrics.find(item => item.key === metric)) } return result }, // 切换计算模式 'changeComputeType'(e) { this.render() }, // 切换指标 'changeInds'(e) { const customs = e.custom ? e.customs : e.defaults const selectedMetrics = this.getSelectedMetrics(customs) this.updater.digest({ selectedMetrics }) } })