UNPKG

@alifd/fusion-collector

Version:

Collect info for fusion based project

229 lines (187 loc) 5.81 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); module.exports = function (wpo, undef) { var startTime, scriptStart; if (wpo.startTime) { startTime = wpo.startTime; } else { try { startTime = window.performance.timing.responseStart; scriptStart = new Date(); } catch (e) { scriptStart = startTime = new Date() - 0; } } var send = function send(params, sampling) { sampling = sampling || wpo.config.sample; // // 双十一当天统计数据抽样率降低 // // if ((curDate.getUTCDate() == 10 && curDate.getUTCMonth() == 10 && curDate.getUTCHours() >= 16) || // (curDate.getUTCDate() == 11 && curDate.getUTCMonth() == 10)) { // sampling *= 10; // } if (wpo.sampling(sampling) == (wpo.config.modVal || 1)) { params.sampling = sampling; wpo.send(params); } }; /** * [custom description] * @param {[int/string]} category [0/'time',1/'count'] * @param {[string]} key [自定义值] * @param {[any]} value [自定义值,如果type为count,自动忽略该值] * @return {[void]} */ wpo.custom = function (category, key, value) { var customParam = { type: 'custom' }, arr = ['time', 'count']; category = arr[category] || category; if (category == 'time' || category == 'count') { customParam['category'] = category; } if (customParam.type) { customParam['key'] = key; customParam['value'] = category == 'time' ? value : undef; send(customParam); } }; /** * [error description] * @param {[str]} category [可选参,错误类型,默认为sys] * @param {[str]} msg [自定义错误信息] * @return {[void]} */ wpo.error = function (category, msg, file, line, col, stack) { var errorParam = { type: 'jserror' }; if (arguments.length === 1) { msg = category; category = undefined; } // 有错误信息才上报 if (msg) { errorParam['category'] = category || 'sys'; if ((0, _typeof2["default"])(msg) == 'object' && msg.message) { //event处理https://developer.mozilla.org/en-US/docs/Web/API/ErrorEvent var msgEvent = msg; try { msg = msgEvent.message; file = file || msgEvent.filename; line = line || msgEvent.lineno; col = col || msgEvent.colno; } catch (e) {} } else { if ((0, _typeof2["default"])(msg) === 'object') { try { msg = JSON.stringify(msg); } catch (e) {} } } errorParam['msg'] = msg; // separate msg file name if (file) { errorParam['file'] = file; } if (line) { errorParam['line'] = line; } if (col) { errorParam['col'] = col; } if (stack) { errorParam['stack'] = stack; } send(errorParam, 1); } }; /** * [performance description] * @param {[obj]} params [性能相关信息] * @return {[void]} */ wpo.performance = function (params) { var perParam = { type: 'per' }; send(wpo.extend(perParam, params)); }; /** * [retCode description] * @param {[str]} api [所调用的api] * @param {[boolean]} issucess [是否成功,不成功会100%发送,成功按照抽样发送] * @param {[type]} delay [调用时间] * @param {[type]} code [错误码] * @param {[type]} msg [错误详情] * @return {[void]} */ wpo.retCode = function (api, issucess, delay, code, msg) { var retParam = { type: 'retcode', api: api, issucess: issucess, delay: typeof delay == 'number' ? parseInt(delay, 10) : new Date() - startTime, msg: code || (issucess ? 'success' : 'fail'), detail: msg || '', sampling: this.config.retCode[api] }; if (typeof retParam.delay !== 'undefined') { send(retParam, issucess ? retParam.sampling : 1); } }; var sendSpeed = function sendSpeed() { var perParam = { type: 'speed' }, val; for (var i = 0, len = wpo.speed.points.length; i < len; i++) { val = wpo.speed.points[i]; if (val) { perParam['s' + i] = val; wpo.speed.points[i] = null; } } send(perParam); }; /** * [speed description] * @param {[int/str]} pos [0/'s0',1/'s1',2/'s2'....10/'s10'] * @param {[int]} delay [耗时,如果没有定义,这按照当前时间减去页面起始时间] * @param {[boolean]} _immediately [内部使用,是否强制发送,不强制发送会尽量收集3s内的所有点的数据一次性发送] * @return {[void]} */ wpo.speed = function (pos, delay, _immediately) { var sArr; if (typeof pos == 'string') { pos = parseInt(pos.slice(1), 10); } if (typeof pos == 'number') { sArr = wpo.speed.points || new Array(11); sArr[pos] = typeof delay == 'number' ? delay : new Date() - startTime; if (sArr[pos] < 0) { sArr[pos] = new Date() - scriptStart; } wpo.speed.points = sArr; } clearTimeout(wpo.speed.timer); if (!_immediately) { wpo.speed.timer = setTimeout(sendSpeed, 3000); } else { sendSpeed(); } }; /** * [log 日志统计] * @param {[string]} msg [发送的内容] * @param {[int]} sampling [可以自定义发送的抽样] * @return {[void]} */ wpo.log = function (msg, sampling) { var param = { type: 'log', msg: msg }; send(param, sampling); }; };