miniprogram-logger
Version:
It is used to log and gather statistics of users' behavior.
92 lines (91 loc) • 3.3 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var reporter_1 = require("../common/reporter");
var ReportAnalytics = /** @class */ (function (_super) {
__extends(ReportAnalytics, _super);
function ReportAnalytics(tableName, fields, context) {
var _this = _super.call(this, tableName, fields || ["action", "duration"], context) || this;
_this.Id = 0;
_this.Stopwatch = new Map();
_this.TransformFunction = reporter_1.logTransformFunction;
return _this;
}
ReportAnalytics.prototype.log = function () {
//@ts-ignore
this.report.apply(this, arguments);
};
/**
*
* @param label - 唯一标签,结束时使用同样标签,
* @param context - 记录上下文信息,如果未设置action则使用label作为action
*/
ReportAnalytics.prototype.time = function (label, context) {
var _a;
this.Stopwatch.set(label, [Date.now(), Object.assign((_a = {}, _a[this.Fields[0]] = arguments[0], _a), context)]);
};
/**
*
* @param label - 唯一标签,与开始对应
* @param context - 记录上下文信息,如果未设置action则使用label作为action
*/
ReportAnalytics.prototype.timeEnd = function (label, context) {
if (!this.end(label, context)) {
//@ts-ignore
console.error("timer label not found", label);
}
};
ReportAnalytics.prototype.start = function () {
var _a;
if (arguments.length === 1 && typeof arguments[0] === "object") {
//对象参数
this.Stopwatch.set(this.Id, [Date.now(), arguments[0]]);
}
else {
//多参数
this.Stopwatch.set(this.Id, [
Date.now(),
Object.assign(arguments[1] || {}, (_a = {}, _a[this.Fields[0]] = arguments[0], _a)),
]);
}
return this.Id++;
};
/**
* 完成计时,并清除计时ID
* @param id
* @param context
*/
ReportAnalytics.prototype.end = function (id, context) {
var _a;
var info = this.Stopwatch.get(id);
if (!info) {
return false;
}
var start = info[0], data = info[1];
Object.assign(data, context, (_a = {}, _a[this.Fields[1]] = Date.now() - start, _a));
this.report(data);
return this.remove(id);
};
/**
* 清除计时
* @param id
* @returns 不存在返回false
*/
ReportAnalytics.prototype.remove = function (id) {
return this.Stopwatch.delete(id);
};
return ReportAnalytics;
}(reporter_1.Reporter));
exports.ReportAnalytics = ReportAnalytics;