t-comm
Version:
专业、稳定、纯粹的工具库
202 lines (199 loc) • 5.24 kB
JavaScript
import { getPartRatio } from '../../number/number.mjs';
import { saveJsonToLog } from '../../node/fs-util.mjs';
import 'fs';
import 'path';
import '../../fs/fs.mjs';
import '../../time/time.mjs';
/**
* 处理事件数据
* @ignore
* @param {object} options 配置
* @example
*
* const res = parseEventData({
* data: {
* LAUNCH_GAME_WX_SUC: 2,
* LAUNCH_GAME_WX_FAIL: 1,
* ENTER_GAME_WX_SUC: 2,
* ENTER_GAME_WX_FAIL: 6,
* },
* eventMap: {
* WX_SUC: {
* // 总和
* type: 'SUMMARY',
* target: ['LAUNCH_GAME_WX_SUC', 'ENTER_GAME_WX_SUC'],
* },
* WX_FAIL: {
* // 总和
* type: 'SUMMARY',
* target: ['LAUNCH_GAME_WX_FAIL', 'ENTER_GAME_WX_FAIL'],
* },
* WX_SUMMARY: {
* // 总和
* type: 'SUMMARY',
* target: ['WX_SUC', 'WX_FAIL'],
* },
* WX_SUC_RATIO: {
* // 比例
* type: 'RATIO',
* target: [
* // 分母部分
* ['WX_SUMMARY'],
* // 分子部分
* ['WX_SUC'],
* ],
* },
* },
* });
*
* console.log(res);
*
* // 结果如下:
* {
* WX_SUC: 4,
* WX_FAIL: 7,
* WX_SUMMARY: 11,
* WX_SUC_RATIO: 36.36,
* }
*
*
*/
function parseEventData(_a) {
var data = _a.data,
eventMap = _a.eventMap;
var res = {};
Object.keys(eventMap).forEach(function (event) {
var info = eventMap[event] || {};
var target = info.target,
type = info.type;
if (type === 'SUMMARY') {
res[event] = getEventSummary(target, data, res) || 0;
} else if (type === 'RATIO') {
var _a = target[0],
summary = _a === void 0 ? [] : _a,
_b = target[1],
part = _b === void 0 ? [] : _b;
res[event] = getPartRatio(getEventSummary(summary, data, res), getEventSummary(part, data, res)) || 0;
}
});
res.ALL_SUMMARY = Object.keys(res).filter(function (key) {
return key.endsWith('_SUMMARY');
}).reduce(function (acc, item) {
return acc + res[item];
}, 0);
res.ALL_FAIL = Object.keys(res).filter(function (key) {
return key.endsWith('_FAIL');
}).reduce(function (acc, item) {
return acc + res[item];
}, 0);
res.ALL_FAIL_RATIO = getPartRatio(res.ALL_SUMMARY, res.ALL_FAIL);
return res;
}
/**
* 获取数据综合
* @ignore
* @param keyList keyList
* @param dataMap dataMap
* @param extraDataMap extraDataMap
* @returns 数据综合
*/
function getEventSummary(keyList, dataMap, extraDataMap) {
return keyList.reduce(function (acc, item) {
return acc + (dataMap[item] || extraDataMap[item] || 0);
}, 0);
}
/**
* 解析额外数据
* @ignore
* @param eventDataMap 数据map
* @returns 解析后的数据
* @example
* const eventDataMap = {
* '57706': {
LAUNCH_GAME_FAIL_GP_HELPER: 2,
LAUNCH_GAME_FAIL_QQ: 228,
extraData: {
JUMP_LIVE_ACCOUNT_CONSOLE_SUC: 27,
LAUNCH_GAME_FAIL_MP: 11,
LAUNCH_GAME_SUC_MP: 261,
}
},
'62653': {
* }
*
* const res = parseExtraData(eventDataMap)
* console.log(res)
* {
* 57706: {
* LAUNCH_GAME_FAIL_GP_HELPER: 2,
LAUNCH_GAME_FAIL_QQ: 228,
* EXTRA_DATA_JUMP_LIVE_ACCOUNT_CONSOLE_SUC: 27,
EXTRA_DATA_LAUNCH_GAME_FAIL_MP: 11,
EXTRA_DATA_LAUNCH_GAME_SUC_MP: 261,
* },
62653: {
// ...
}
* }
*/
function parseExtraData(eventDataMap) {
var res = {};
saveJsonToLog(eventDataMap, 'tam.custom-event.event-data-map.json');
Object.keys(eventDataMap).forEach(function (projectId) {
var eventData = eventDataMap[projectId];
if (eventData.extraData) {
Object.keys(eventData.extraData).forEach(function (key) {
eventData["EXTRA_DATA_".concat(key)] = eventData.extraData[key];
});
delete eventData.extraData;
}
res[projectId] = eventData;
});
saveJsonToLog(res, 'tam.custom-event.parsed-extra-data.json');
return res;
}
/**
* 解析自定义事件数据
* @ignore
* @param options 配置
* @returns 解析后的数据
*/
function parseMultiCustomEvent(_a) {
var eventDataMap = _a.eventDataMap,
eventMap = _a.eventMap,
_b = _a.projectIdMap,
projectIdMap = _b === void 0 ? {} : _b,
sortKeyList = _a.sortKeyList;
var parsedEventData = parseExtraData(eventDataMap);
var res = [];
Object.keys(parsedEventData).forEach(function (projectId) {
var _a;
var eventData = eventDataMap[projectId];
var parsedData = parseEventData({
data: eventData,
eventMap: eventMap
});
// 统一格式,类似于:[ { ProjectName: { name: 'ProjectName', value: '脚手架' } } ]
var initial = {
ProjectName: {
name: 'ProjectName',
value: (_a = projectIdMap[projectId]) === null || _a === void 0 ? void 0 : _a.name
}
};
initial = Object.keys(parsedData).filter(function (item) {
return sortKeyList.includes(item);
}).sort(function (a, b) {
return sortKeyList.indexOf(a) - sortKeyList.indexOf(b);
}).reduce(function (acc, key) {
acc[key] = {
name: key,
value: parsedData[key]
};
return acc;
}, initial);
res.push(initial);
});
saveJsonToLog(res, 'tam.custom-event.parsed-multi-custom-event.json');
return res;
}
export { parseMultiCustomEvent };