/**
* @param params 调用参数,HTTP 请求下为请求体
* @param context 调用上下文
*
* @return 函数的返回数据,HTTP 场景下会作为 Response Body
*
* 完整信息可参考:
* https://inspirecloud.bytedance.net/docs/cloud-function/basic.html
*/
const axios = require('axios')
const dayjs = require('dayjs')
const plimit = require('p-limit');
const { domain } = require('./CONSTANT')
const limit = plimit(10);
const buids = ['电商_货架', '电商_服务体验', '电商_营销', '电商_联盟生态']
const totalPage = buids.length;
const sleep = (time) => new Promise(resolve => setTimeout(resolve, time))
module.exports = async function (params, context) {
const { date: _date, page = 1 } = params;
const date = _date ?
dayjs(params.date).subtract(1, 'd') :
dayjs().subtract(1, 'd');
const dateStr = date.format('YYYY-MM-DD');
const _buid = buids[page - 1];
if (!_buid) return {
hasMore: false,
results: []
};
const projects = await axios.get(`${domain}/api/project/getAllProject`).then(res => {
const { projList } = res.data.data;
return projList.filter(item => item.business_id === _buid)
})
if (!projects.length) return {
hasMore: false,
results: []
};
console.log(dateStr, '============', projects.length)
const promiseList = [];
for (const project of projects) {
const { bid, virtual, status } = project;
// 项目已删除
if (status === 0 || virtual) continue;
const getIndicatorsData = () => axios.post(`${domain}/api/open/blank-screen/data/indicators/detail`, {
bid,
date,
granularity: '1d'
}, {
headers: {
'x-tt-env': 'ppe_test',
'x-use-ppe': '1'
}
}).then(res => {
const indicatorsData = res.data.data || {};
const {
businessId,
groupId,
siteType,
containerTypes,
boundaryerror,
pageview,
blankscreen
} = indicatorsData;
if (!boundaryerror || !blankscreen) return;
return {
bid,
business_id: businessId ? businessId.split('_')[1] : '其它',
group_id: groupId ? groupId.split('_')[2] : '其它',
virtual,
siteType,
containerTypes: containerTypes.join(','),
date: dateStr,
status,
...Object.keys(boundaryerror).reduce((target, key) => {
target[`be_${key}`] = boundaryerror[key]
return target
}, {}),
...Object.keys(pageview).reduce((target, key) => {
target[`pv_${key}`] = pageview[key]
return target
}, {}),
...Object.keys(blankscreen).reduce((target, key) => {
target[`bs_${key}`] = blankscreen[key]
return target
}, {}),
}
})
promiseList.push(limit(getIndicatorsData))
}
const results = await Promise.all(promiseList)
return {
hasMore: Number(totalPage - page) > 0,
results: results.filter(Boolean),
};
}
console