import { Pipe } from './index'; import { SpeedLog, NormalLog, LOG_TYPE, Config } from '@tencent/aegis-core'; // 限制每条测速只上报一次 export const createSpeedRepeatLimitPipe = function (config: Config): Pipe { const logMap: { [key: string]: boolean } = {}; return (log: SpeedLog | SpeedLog[], resolve) => { if (!config.speedSample) { resolve(log); return; } if (Array.isArray(log)) { const filterLog = log.filter((log) => { const through = !logMap[log.url]; logMap[log.url] = true; return through; }); filterLog.length && resolve(filterLog); } else { !logMap[log.url] && resolve(log); logMap[log.url] = true; } }; }; export const createErrorLogLimitPipe = function (config: Config): Pipe { const logMap: { [key: string]: number } = {}; return (logs: NormalLog[], resolve) => { const maxNum = typeof config.repeat === 'number' ? config.repeat : 5; if (maxNum === 0) { return resolve(logs); } resolve(logs.filter((log) => { // 数量限制只针对错误日志 if ( log.level === LOG_TYPE.ERROR || log.level === LOG_TYPE.PROMISE_ERROR || log.level === LOG_TYPE.AJAX_ERROR || log.level === LOG_TYPE.SCRIPT_ERROR || log.level === LOG_TYPE.IMAGE_ERROR || log.level === LOG_TYPE.CSS_ERROR || log.level === LOG_TYPE.MEDIA_ERROR ) { logMap[log.msg] = logMap[log.msg] || 0; logMap[log.msg] += 1; if (logMap[log.msg] > maxNum) { return false; } return true; } return true; })); }; };