corexxx 1.0.134 → 1.0.136
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +3 -0
- package/dist/danalytics.d.ts +36 -0
- package/dist/danalytics.js +113 -0
- package/dist/danalytics.js.map +1 -0
- package/dist/dlog.d.ts +2 -12
- package/dist/dlog.js +30 -47
- package/dist/dlog.js.map +1 -1
- package/package.json +1 -1
- package/src/danalytics.ts +112 -0
- package/src/dlog.ts +31 -56
- package/test/danalytics.test.ts +29 -0
- package/test/dlog.test.ts +12 -7
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DAnalytics - Simple Analytics/Event Tracking Utility
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* const analytics = new DAnalytics({ appName: "myApp", debug: true });
|
|
6
|
+
* analytics.trackEvent("user_login", { userId: 123 });
|
|
7
|
+
* analytics.trackPageView("/home");
|
|
8
|
+
*/
|
|
9
|
+
export interface TConfig {
|
|
10
|
+
server: string;
|
|
11
|
+
app_id: string;
|
|
12
|
+
app_version?: string;
|
|
13
|
+
device_os?: string;
|
|
14
|
+
device_id?: string;
|
|
15
|
+
device_api?: string;
|
|
16
|
+
debug?: boolean;
|
|
17
|
+
}
|
|
18
|
+
export declare class DAnalytics {
|
|
19
|
+
private config;
|
|
20
|
+
private sessionKey;
|
|
21
|
+
private eventQueue;
|
|
22
|
+
constructor(config: TConfig);
|
|
23
|
+
/**
|
|
24
|
+
* Initialize analytics session by calling the launch API.
|
|
25
|
+
*/
|
|
26
|
+
private initSession;
|
|
27
|
+
/**
|
|
28
|
+
* Track a specific user action by calling the /api/analytics/action endpoint.
|
|
29
|
+
* @param type The type of action (e.g., "click")
|
|
30
|
+
* @param target_id The target element's ID (e.g., "btn1")
|
|
31
|
+
* @param tag A tag describing the action (e.g., "btn_click")
|
|
32
|
+
* @returns The response from the server
|
|
33
|
+
*/
|
|
34
|
+
trackAction(type: string, target_id: string, tag: string): Promise<any>;
|
|
35
|
+
trackConsoleLog(): Promise<any>;
|
|
36
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* DAnalytics - Simple Analytics/Event Tracking Utility
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* const analytics = new DAnalytics({ appName: "myApp", debug: true });
|
|
7
|
+
* analytics.trackEvent("user_login", { userId: 123 });
|
|
8
|
+
* analytics.trackPageView("/home");
|
|
9
|
+
*/
|
|
10
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
11
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
12
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
13
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
14
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
15
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
16
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.DAnalytics = void 0;
|
|
21
|
+
const dlog_1 = require("./dlog");
|
|
22
|
+
const dnetwork_1 = require("./dnetwork");
|
|
23
|
+
class DAnalytics {
|
|
24
|
+
constructor(config) {
|
|
25
|
+
this.sessionKey = null;
|
|
26
|
+
this.eventQueue = [];
|
|
27
|
+
this.config = config;
|
|
28
|
+
this.initSession();
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Initialize analytics session by calling the launch API.
|
|
32
|
+
*/
|
|
33
|
+
initSession() {
|
|
34
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
+
const url = `http://${this.config.server}/api/analytics/launch`;
|
|
36
|
+
const payload = {
|
|
37
|
+
app_id: this.config.app_id,
|
|
38
|
+
app_version: this.config.app_version || "0.0",
|
|
39
|
+
device_os: this.config.device_os || "unknown",
|
|
40
|
+
device_id: this.config.device_id || "unknown",
|
|
41
|
+
device_api: this.config.device_api || "unknown",
|
|
42
|
+
};
|
|
43
|
+
try {
|
|
44
|
+
const resp = yield dnetwork_1.dnetwork.postSimpleStore(url, payload);
|
|
45
|
+
this.sessionKey = resp.out.session;
|
|
46
|
+
if (this.config.debug) {
|
|
47
|
+
dlog_1.dlog.d("[DAnalytics] Session initialized:", this.sessionKey);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
if (this.config.debug) {
|
|
52
|
+
// eslint-disable-next-line no-console
|
|
53
|
+
dlog_1.dlog.d("[DAnalytics] Failed to initialize session", err);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Track a specific user action by calling the /api/analytics/action endpoint.
|
|
60
|
+
* @param type The type of action (e.g., "click")
|
|
61
|
+
* @param target_id The target element's ID (e.g., "btn1")
|
|
62
|
+
* @param tag A tag describing the action (e.g., "btn_click")
|
|
63
|
+
* @returns The response from the server
|
|
64
|
+
*/
|
|
65
|
+
trackAction(type, target_id, tag) {
|
|
66
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
if (!this.sessionKey) {
|
|
68
|
+
if (this.config.debug) {
|
|
69
|
+
// eslint-disable-next-line no-console
|
|
70
|
+
console.warn("[DAnalytics] Session not initialized, cannot track action.");
|
|
71
|
+
}
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const url = `http://${this.config.server}/api/analytics/action`;
|
|
75
|
+
const payload = {
|
|
76
|
+
app_id: this.config.app_id,
|
|
77
|
+
session: this.sessionKey,
|
|
78
|
+
type,
|
|
79
|
+
action_id: target_id,
|
|
80
|
+
action_tag: tag,
|
|
81
|
+
};
|
|
82
|
+
try {
|
|
83
|
+
const data = yield dnetwork_1.dnetwork.postSimpleStore(url, payload);
|
|
84
|
+
if (this.config.debug) {
|
|
85
|
+
// eslint-disable-next-line no-console
|
|
86
|
+
console.log("[DAnalytics] Tracked action:", data);
|
|
87
|
+
}
|
|
88
|
+
return data;
|
|
89
|
+
}
|
|
90
|
+
catch (err) {
|
|
91
|
+
if (this.config.debug) {
|
|
92
|
+
// eslint-disable-next-line no-console
|
|
93
|
+
console.error("[DAnalytics] Failed to track action", err);
|
|
94
|
+
}
|
|
95
|
+
throw err;
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
// This will log anything in the console.
|
|
100
|
+
// This will flash all log history
|
|
101
|
+
trackConsoleLog() {
|
|
102
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
103
|
+
const url = `http://${this.config.server}/api/analytics/adhoc`;
|
|
104
|
+
return yield dnetwork_1.dnetwork.postSimpleStore(url, {
|
|
105
|
+
app_id: this.config.app_id,
|
|
106
|
+
session: this.sessionKey,
|
|
107
|
+
console_log: dlog_1.dlog.getLogHistoryAndFlash(),
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
exports.DAnalytics = DAnalytics;
|
|
113
|
+
//# sourceMappingURL=danalytics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"danalytics.js","sourceRoot":"","sources":["../src/danalytics.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;;;;;;;;;;AAEH,iCAA8B;AAC9B,yCAAsC;AAYtC,MAAa,UAAU;IAKrB,YAAY,MAAe;QAHnB,eAAU,GAAkB,IAAI,CAAC;QACjC,eAAU,GAAmD,EAAE,CAAC;QAGtE,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,WAAW,EAAE,CAAC;IACrB,CAAC;IAED;;OAEG;IACW,WAAW;;YACvB,MAAM,GAAG,GAAG,UAAU,IAAI,CAAC,MAAM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uB,CAAC;YAChE,MAAM,OAAO,GAAG;gBACd,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,MAAM;gBAC1B,OAAO,EAAE,IAAI,CAAC,UAAU;gBACxB,IAAI;gBACJ,SAAS,EAAE,SAAS;gBACpB,UAAU,EAAE,GAAG;aAChB,CAAC;YACF,IAAI,CAAC;gBACH,MAAM,IAAI,GAAG,MAAM,mBAAQ,CAAC,eAAe,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;gBAC1D,IAAI,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;oBACtB,sCAAsC;oBACtC,OAAO,CAAC,GAAG,CAAC,8BAA8B,EAAE,IAAI,CAAC,CAAC;gBACpD,CAAC;gBACD,OAAO,IAAI,CAAC;YACd,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,IAAI,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;oBACtB,sCAAsC;oBACtC,OAAO,CAAC,KAAK,CAAC,qCAAqC,EAAE,GAAG,CAAC,CAAC;gBAC5D,CAAC;gBACD,MAAM,GAAG,CAAC;YACZ,CAAC;QACH,CAAC;KAAA;IACD,yCAAyC;IACzC,kCAAkC;IAC5B,eAAe;;YACnB,MAAM,GAAG,GAAG,UAAU,IAAI,CAAC,MAAM,CAAC,MAAM,sBAAsB,CAAC;YAC/D,OAAO,MAAM,mBAAQ,CAAC,eAAe,CAAC,GAAG,EAAE;gBACzC,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,MAAM;gBAC1B,OAAO,EAAE,IAAI,CAAC,UAAU;gBACxB,WAAW,EAAE,WAAI,CAAC,qBAAqB,EAAE;aAC1C,CAAC,CAAC;QACL,CAAC;KAAA;CACF;AAzFD,gCAyFC"}
|
package/dist/dlog.d.ts
CHANGED
|
@@ -49,18 +49,8 @@ export declare namespace dlog {
|
|
|
49
49
|
*/
|
|
50
50
|
export function ex(error: Error, ignore?: boolean, msg?: string): void;
|
|
51
51
|
/**
|
|
52
|
-
*
|
|
53
|
-
* Redacts sensitive keys.
|
|
52
|
+
* Returns the log history and clears it.
|
|
54
53
|
*/
|
|
55
|
-
export function
|
|
56
|
-
/**
|
|
57
|
-
* Logs all entries of a Map, redacting sensitive keys.
|
|
58
|
-
*/
|
|
59
|
-
export function map(map: Map<string, unknown>): void;
|
|
60
|
-
/**
|
|
61
|
-
* Placeholder for trace logging.
|
|
62
|
-
* To be implemented if needed.
|
|
63
|
-
*/
|
|
64
|
-
export function trace(): void;
|
|
54
|
+
export function getLogHistoryAndFlash(): string[];
|
|
65
55
|
export {};
|
|
66
56
|
}
|
package/dist/dlog.js
CHANGED
|
@@ -22,6 +22,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
22
22
|
exports.dlog = void 0;
|
|
23
23
|
var dlog;
|
|
24
24
|
(function (dlog) {
|
|
25
|
+
// This will store any data other than debug
|
|
26
|
+
const logs_history = [];
|
|
25
27
|
let config = { isDev: true, enablePII: false };
|
|
26
28
|
// Unicode icons for log levels
|
|
27
29
|
const ICONS = {
|
|
@@ -88,19 +90,24 @@ var dlog;
|
|
|
88
90
|
*/
|
|
89
91
|
function getCallerFunctionName() {
|
|
90
92
|
var _a;
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
93
|
+
try {
|
|
94
|
+
const err = new Error();
|
|
95
|
+
const stack = (_a = err.stack) === null || _a === void 0 ? void 0 : _a.split("\n");
|
|
96
|
+
// stack[0] is 'Error', stack[1] is this function, stack[2] is the caller
|
|
97
|
+
if (stack && stack.length >= 5) {
|
|
98
|
+
const match = stack[4].match(/\((.*):(\d+):(\d+)\)$/);
|
|
99
|
+
if (match) {
|
|
100
|
+
const filePath = match[1];
|
|
101
|
+
const line = match[2];
|
|
102
|
+
// Optionally, you can trim the file path to just the filename:
|
|
103
|
+
const fileName = filePath.split(/[\\/]/).pop();
|
|
104
|
+
return `${fileName}:${line}`;
|
|
105
|
+
}
|
|
102
106
|
}
|
|
103
107
|
}
|
|
108
|
+
catch (e) {
|
|
109
|
+
// pass
|
|
110
|
+
}
|
|
104
111
|
return "anonymous";
|
|
105
112
|
}
|
|
106
113
|
/**
|
|
@@ -124,6 +131,9 @@ var dlog;
|
|
|
124
131
|
}
|
|
125
132
|
}
|
|
126
133
|
const message = `[${icon}${level}] [${now}][${caller}] ${convertMsg(msg)}${msg2Str}`;
|
|
134
|
+
if (level != "DEBUG") {
|
|
135
|
+
logs_history.push(message);
|
|
136
|
+
}
|
|
127
137
|
console.log(message);
|
|
128
138
|
}
|
|
129
139
|
/**
|
|
@@ -165,46 +175,19 @@ var dlog;
|
|
|
165
175
|
const caller = getCallerFunctionName();
|
|
166
176
|
const icon = ICONS.EXCEPTION;
|
|
167
177
|
const stack = !ignore && error.stack ? `\n${error.stack}` : "";
|
|
168
|
-
|
|
178
|
+
let message = `[${now}][${icon} EXCEPTION][${caller}] ${msg !== null && msg !== void 0 ? msg : ""} ${error.message}${stack}`;
|
|
179
|
+
console.log(message);
|
|
180
|
+
logs_history.push(message);
|
|
169
181
|
}
|
|
170
182
|
dlog.ex = ex;
|
|
171
183
|
/**
|
|
172
|
-
*
|
|
173
|
-
* Redacts sensitive keys.
|
|
174
|
-
*/
|
|
175
|
-
function obj(obj, msg = "DEBUG OBJECT") {
|
|
176
|
-
if (!config.isDev)
|
|
177
|
-
return;
|
|
178
|
-
const now = new Date().toLocaleString();
|
|
179
|
-
const caller = getCallerFunctionName();
|
|
180
|
-
console.log(`[${now}][${ICONS.DEBUG} DEBUG][${caller}] ===== ${msg} START =====`);
|
|
181
|
-
console.log(convertMsg(obj));
|
|
182
|
-
console.log(`[${now}][${ICONS.DEBUG} DEBUG][${caller}] ===== ${msg} END =====`);
|
|
183
|
-
}
|
|
184
|
-
dlog.obj = obj;
|
|
185
|
-
/**
|
|
186
|
-
* Logs all entries of a Map, redacting sensitive keys.
|
|
187
|
-
*/
|
|
188
|
-
function map(map) {
|
|
189
|
-
if (!config.isDev)
|
|
190
|
-
return;
|
|
191
|
-
const now = new Date().toLocaleString();
|
|
192
|
-
const caller = getCallerFunctionName();
|
|
193
|
-
console.log(`[${now}][${ICONS.DEBUG} DEBUG][${caller}] ===== MAP START =====`);
|
|
194
|
-
for (const [key, value] of map.entries()) {
|
|
195
|
-
const displayValue = convertMsg({ [key]: value });
|
|
196
|
-
console.log(`${JSON.stringify(key)} ==> ${displayValue}`);
|
|
197
|
-
}
|
|
198
|
-
console.log(`[${now}][${ICONS.DEBUG} DEBUG][${caller}] ===== MAP END =====`);
|
|
199
|
-
}
|
|
200
|
-
dlog.map = map;
|
|
201
|
-
/**
|
|
202
|
-
* Placeholder for trace logging.
|
|
203
|
-
* To be implemented if needed.
|
|
184
|
+
* Returns the log history and clears it.
|
|
204
185
|
*/
|
|
205
|
-
function
|
|
206
|
-
|
|
186
|
+
function getLogHistoryAndFlash() {
|
|
187
|
+
const history = [...logs_history];
|
|
188
|
+
logs_history.length = 0;
|
|
189
|
+
return history;
|
|
207
190
|
}
|
|
208
|
-
dlog.
|
|
191
|
+
dlog.getLogHistoryAndFlash = getLogHistoryAndFlash;
|
|
209
192
|
})(dlog || (exports.dlog = dlog = {}));
|
|
210
193
|
//# sourceMappingURL=dlog.js.map
|
package/dist/dlog.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dlog.js","sourceRoot":"","sources":["../src/dlog.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;GAkBG;;;AAEH,IAAiB,IAAI,
|
|
1
|
+
{"version":3,"file":"dlog.js","sourceRoot":"","sources":["../src/dlog.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;GAkBG;;;AAEH,IAAiB,IAAI,CA+KpB;AA/KD,WAAiB,IAAI;IAMnB,4CAA4C;IAC5C,MAAM,YAAY,GAAa,EAAE,CAAC;IAElC,IAAI,MAA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AC;IAC7B,CAAC;IAXe,OAAE,KAWjB,CAAA;IAED;;OAEG;IACH,SAAgB,qBAAqB;QACnC,MAAM,OAAO,GAAG,CAAC,GAAG,YAAY,CAAC,CAAC;QAClC,YAAY,CAAC,MAAM,GAAG,CAAC,CAAC;QACxB,OAAO,OAAO,CAAC;IACjB,CAAC;IAJe,0BAAqB,wBAIpC,CAAA;AACH,CAAC,EA/KgB,IAAI,oBAAJ,IAAI,QA+KpB"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DAnalytics - Simple Analytics/Event Tracking Utility
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* const analytics = new DAnalytics({ appName: "myApp", debug: true });
|
|
6
|
+
* analytics.trackEvent("user_login", { userId: 123 });
|
|
7
|
+
* analytics.trackPageView("/home");
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { dlog } from "./dlog";
|
|
11
|
+
import { dnetwork } from "./dnetwork";
|
|
12
|
+
|
|
13
|
+
export interface TConfig {
|
|
14
|
+
server: string;
|
|
15
|
+
app_id: string;
|
|
16
|
+
app_version?: string;
|
|
17
|
+
device_os?: string;
|
|
18
|
+
device_id?: string;
|
|
19
|
+
device_api?: string;
|
|
20
|
+
debug?: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export class DAnalytics {
|
|
24
|
+
private config: TConfig;
|
|
25
|
+
private sessionKey: string | null = null;
|
|
26
|
+
private eventQueue: Array<{ type: string; data: any; ts: number }> = [];
|
|
27
|
+
|
|
28
|
+
constructor(config: TConfig) {
|
|
29
|
+
this.config = config;
|
|
30
|
+
this.initSession();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Initialize analytics session by calling the launch API.
|
|
35
|
+
*/
|
|
36
|
+
private async initSession() {
|
|
37
|
+
const url = `http://${this.config.server}/api/analytics/launch`;
|
|
38
|
+
const payload = {
|
|
39
|
+
app_id: this.config.app_id,
|
|
40
|
+
app_version: this.config.app_version || "0.0",
|
|
41
|
+
device_os: this.config.device_os || "unknown",
|
|
42
|
+
device_id: this.config.device_id || "unknown",
|
|
43
|
+
device_api: this.config.device_api || "unknown",
|
|
44
|
+
};
|
|
45
|
+
try {
|
|
46
|
+
const resp = await dnetwork.postSimpleStore(url, payload);
|
|
47
|
+
this.sessionKey = resp.out.session;
|
|
48
|
+
if (this.config.debug) {
|
|
49
|
+
dlog.d("[DAnalytics] Session initialized:", this.sessionKey);
|
|
50
|
+
}
|
|
51
|
+
} catch (err) {
|
|
52
|
+
if (this.config.debug) {
|
|
53
|
+
// eslint-disable-next-line no-console
|
|
54
|
+
dlog.d("[DAnalytics] Failed to initialize session", err);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Track a specific user action by calling the /api/analytics/action endpoint.
|
|
60
|
+
* @param type The type of action (e.g., "click")
|
|
61
|
+
* @param target_id The target element's ID (e.g., "btn1")
|
|
62
|
+
* @param tag A tag describing the action (e.g., "btn_click")
|
|
63
|
+
* @returns The response from the server
|
|
64
|
+
*/
|
|
65
|
+
async trackAction(
|
|
66
|
+
type: string,
|
|
67
|
+
target_id: string,
|
|
68
|
+
tag: string
|
|
69
|
+
): Promise<any> {
|
|
70
|
+
if (!this.sessionKey) {
|
|
71
|
+
if (this.config.debug) {
|
|
72
|
+
// eslint-disable-next-line no-console
|
|
73
|
+
console.warn(
|
|
74
|
+
"[DAnalytics] Session not initialized, cannot track action."
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const url = `http://${this.config.server}/api/analytics/action`;
|
|
80
|
+
const payload = {
|
|
81
|
+
app_id: this.config.app_id,
|
|
82
|
+
session: this.sessionKey,
|
|
83
|
+
type,
|
|
84
|
+
action_id: target_id,
|
|
85
|
+
action_tag: tag,
|
|
86
|
+
};
|
|
87
|
+
try {
|
|
88
|
+
const data = await dnetwork.postSimpleStore(url, payload);
|
|
89
|
+
if (this.config.debug) {
|
|
90
|
+
// eslint-disable-next-line no-console
|
|
91
|
+
console.log("[DAnalytics] Tracked action:", data);
|
|
92
|
+
}
|
|
93
|
+
return data;
|
|
94
|
+
} catch (err) {
|
|
95
|
+
if (this.config.debug) {
|
|
96
|
+
// eslint-disable-next-line no-console
|
|
97
|
+
console.error("[DAnalytics] Failed to track action", err);
|
|
98
|
+
}
|
|
99
|
+
throw err;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// This will log anything in the console.
|
|
103
|
+
// This will flash all log history
|
|
104
|
+
async trackConsoleLog() {
|
|
105
|
+
const url = `http://${this.config.server}/api/analytics/adhoc`;
|
|
106
|
+
return await dnetwork.postSimpleStore(url, {
|
|
107
|
+
app_id: this.config.app_id,
|
|
108
|
+
session: this.sessionKey,
|
|
109
|
+
console_log: dlog.getLogHistoryAndFlash(),
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
package/src/dlog.ts
CHANGED
|
@@ -24,6 +24,9 @@ export namespace dlog {
|
|
|
24
24
|
enablePII?: boolean;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
// This will store any data other than debug
|
|
28
|
+
const logs_history: string[] = [];
|
|
29
|
+
|
|
27
30
|
let config: DlogConfig = { isDev: true, enablePII: false };
|
|
28
31
|
|
|
29
32
|
// Unicode icons for log levels
|
|
@@ -90,18 +93,22 @@ export namespace dlog {
|
|
|
90
93
|
* Gets the name of the caller function from the stack trace.
|
|
91
94
|
*/
|
|
92
95
|
function getCallerFunctionName(): string {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
96
|
+
try {
|
|
97
|
+
const err = new Error();
|
|
98
|
+
const stack = err.stack?.split("\n");
|
|
99
|
+
// stack[0] is 'Error', stack[1] is this function, stack[2] is the caller
|
|
100
|
+
if (stack && stack.length >= 5) {
|
|
101
|
+
const match = stack[4].match(/\((.*):(\d+):(\d+)\)$/);
|
|
102
|
+
if (match) {
|
|
103
|
+
const filePath = match[1];
|
|
104
|
+
const line = match[2];
|
|
105
|
+
// Optionally, you can trim the file path to just the filename:
|
|
106
|
+
const fileName = filePath.split(/[\\/]/).pop();
|
|
107
|
+
return `${fileName}:${line}`;
|
|
108
|
+
}
|
|
104
109
|
}
|
|
110
|
+
} catch (e) {
|
|
111
|
+
// pass
|
|
105
112
|
}
|
|
106
113
|
return "anonymous";
|
|
107
114
|
}
|
|
@@ -127,6 +134,9 @@ export namespace dlog {
|
|
|
127
134
|
const message = `[${icon}${level}] [${now}][${caller}] ${convertMsg(
|
|
128
135
|
msg
|
|
129
136
|
)}${msg2Str}`;
|
|
137
|
+
if (level != "DEBUG") {
|
|
138
|
+
logs_history.push(message);
|
|
139
|
+
}
|
|
130
140
|
console.log(message);
|
|
131
141
|
}
|
|
132
142
|
|
|
@@ -168,54 +178,19 @@ export namespace dlog {
|
|
|
168
178
|
const caller = getCallerFunctionName();
|
|
169
179
|
const icon = ICONS.EXCEPTION;
|
|
170
180
|
const stack = !ignore && error.stack ? `\n${error.stack}` : "";
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Logs an object with a custom message.
|
|
180
|
-
* Redacts sensitive keys.
|
|
181
|
-
*/
|
|
182
|
-
export function obj(obj?: object, msg = "DEBUG OBJECT") {
|
|
183
|
-
if (!config.isDev) return;
|
|
184
|
-
const now = new Date().toLocaleString();
|
|
185
|
-
const caller = getCallerFunctionName();
|
|
186
|
-
console.log(
|
|
187
|
-
`[${now}][${ICONS.DEBUG} DEBUG][${caller}] ===== ${msg} START =====`
|
|
188
|
-
);
|
|
189
|
-
console.log(convertMsg(obj));
|
|
190
|
-
console.log(
|
|
191
|
-
`[${now}][${ICONS.DEBUG} DEBUG][${caller}] ===== ${msg} END =====`
|
|
192
|
-
);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Logs all entries of a Map, redacting sensitive keys.
|
|
197
|
-
*/
|
|
198
|
-
export function map(map: Map<string, unknown>) {
|
|
199
|
-
if (!config.isDev) return;
|
|
200
|
-
const now = new Date().toLocaleString();
|
|
201
|
-
const caller = getCallerFunctionName();
|
|
202
|
-
console.log(
|
|
203
|
-
`[${now}][${ICONS.DEBUG} DEBUG][${caller}] ===== MAP START =====`
|
|
204
|
-
);
|
|
205
|
-
for (const [key, value] of map.entries()) {
|
|
206
|
-
const displayValue = convertMsg({ [key]: value });
|
|
207
|
-
console.log(`${JSON.stringify(key)} ==> ${displayValue}`);
|
|
208
|
-
}
|
|
209
|
-
console.log(
|
|
210
|
-
`[${now}][${ICONS.DEBUG} DEBUG][${caller}] ===== MAP END =====`
|
|
211
|
-
);
|
|
181
|
+
let message = `[${now}][${icon} EXCEPTION][${caller}] ${msg ?? ""} ${
|
|
182
|
+
error.message
|
|
183
|
+
}${stack}`;
|
|
184
|
+
console.log(message);
|
|
185
|
+
logs_history.push(message);
|
|
212
186
|
}
|
|
213
187
|
|
|
214
188
|
/**
|
|
215
|
-
*
|
|
216
|
-
* To be implemented if needed.
|
|
189
|
+
* Returns the log history and clears it.
|
|
217
190
|
*/
|
|
218
|
-
export function
|
|
219
|
-
|
|
191
|
+
export function getLogHistoryAndFlash(): string[] {
|
|
192
|
+
const history = [...logs_history];
|
|
193
|
+
logs_history.length = 0;
|
|
194
|
+
return history;
|
|
220
195
|
}
|
|
221
196
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { DAnalytics, TConfig } from "../src/danalytics";
|
|
2
|
+
import { dlog } from "../src/dlog";
|
|
3
|
+
|
|
4
|
+
describe("DAnalytics initSession", () => {
|
|
5
|
+
beforeEach(() => {});
|
|
6
|
+
|
|
7
|
+
it("should call the launch API and store the session key", async () => {
|
|
8
|
+
const config: TConfig = {
|
|
9
|
+
server: "simplestore.dipankar.co.in",
|
|
10
|
+
app_id: "corexxx",
|
|
11
|
+
debug: false,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// Create instance and wait for session to initialize
|
|
15
|
+
const analytics = new DAnalytics(config);
|
|
16
|
+
// Wait for the async initSession to complete
|
|
17
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
18
|
+
expect(analytics["sessionKey"]).toBeDefined();
|
|
19
|
+
|
|
20
|
+
// test action:
|
|
21
|
+
const result = await analytics.trackAction("click", "btn1", "btn_click");
|
|
22
|
+
expect(result).toMatchObject({ status: "success" });
|
|
23
|
+
|
|
24
|
+
// adhoc action
|
|
25
|
+
dlog.i("This is a sample test console log", { Hello: 100 });
|
|
26
|
+
const result1 = await analytics.trackConsoleLog();
|
|
27
|
+
expect(result1).toMatchObject({ status: "success" });
|
|
28
|
+
});
|
|
29
|
+
});
|
package/test/dlog.test.ts
CHANGED
|
@@ -25,12 +25,17 @@ describe("dlog utility", () => {
|
|
|
25
25
|
}
|
|
26
26
|
});
|
|
27
27
|
|
|
28
|
-
it("should
|
|
29
|
-
dlog.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
28
|
+
it("should return and clear the log history", () => {
|
|
29
|
+
dlog.d("Test debug log");
|
|
30
|
+
dlog.i("Test info log");
|
|
31
|
+
const history = dlog.getLogHistoryAndFlash();
|
|
32
|
+
expect(Array.isArray(history)).toBe(true);
|
|
33
|
+
expect(history.length).toBeGreaterThanOrEqual(2);
|
|
34
|
+
expect(history.some((line) => line.includes("Test debug log"))).toBe(true);
|
|
35
|
+
expect(history.some((line) => line.includes("Test info log"))).toBe(true);
|
|
36
|
+
|
|
37
|
+
// After flash, history should be empty
|
|
38
|
+
const afterFlash = dlog.getLogHistoryAndFlash();
|
|
39
|
+
expect(afterFlash.length).toBe(0);
|
|
35
40
|
});
|
|
36
41
|
});
|