-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAnalysysNodeJsSdk.js
More file actions
490 lines (478 loc) · 17.4 KB
/
AnalysysNodeJsSdk.js
File metadata and controls
490 lines (478 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// import request from 'request'; //请求的公共模块
import request from 'sync-request'; //请求的公共模块 同步
import baseConfig from './lib/AnalysysBase'; //基本配置文件 包括事件名称,成功码、错误码等
import { successLog, errorLog } from './lib/AnalysysLog'; //成功日志 和 错误日志
import check from './lib/AnalysysCheck'; // 校验模块
import Util from './lib/AnalysysCommon'; // 公共方法模块
import zlib from 'zlib'; // 数据压缩的公共模块
import fs from 'fs';
import LogCollector from './lib/LogCollecter';
class AnalysysAgent {
constructor({ appId, uploadURL, debugMode = 0, postNumber = 0, postTime = 0, logCollector = null }) {
baseConfig.base.$debug = debugMode;
this.appId = appId;
if (Util.paramType(logCollector) == 'Null' || Util.paramType(logCollector) != 'Object') {
var status = check.checkBase(appId, uploadURL);
if (status) {
this.uploadURL = Util.checkUrlLast(uploadURL) + 'up?appid=' + appId;
}
}
this.baseProperties = {
'$lib': 'Node',
'$lib_version': '4.0.5',
'$debug': debugMode
};
this.superProperty = {};
this.postData = [];
this.postNumber = postNumber;
this.upPostDataTime = postTime;
this.postFun = null;
this.timer = null;
this.code = 400;
this.logCollector = logCollector;
}
/**
* 重置状态,每次打印前的重置
*/
resetCode () {
this.code = 400;
baseConfig.status = {
'code': 200,
'FnName': baseConfig.status.FnName || '',
'key': '',
'value': '',
'errorCode': '',
'successCode': ''
};
}
/**
* 注册超级属性
* @param params key
*/
registerSuperProperty (key, value) {
this.resetCode();
baseConfig.status.FnName = '$registerSuperProperty';
var obj = Util.toObj(key, value);
obj = check.checkProperty(obj);
var superProperty = this.superProperty;
baseConfig.status.value = JSON.stringify(obj);
// if (check.checkKey(key) && check.checkValue(value)) {
this.superProperty = Util.objMerge(superProperty, obj);
baseConfig.status.successCode = 20002;
this.code = 200;
successLog();
return true;
// }
}
/**
* 注册超级属性,注册后每次发送的消息体中都包含该属性值
* @param params 属性
*/
registerSuperProperties (property) {
this.resetCode();
var superProperty = this.superProperty;
baseConfig.status.FnName = '$registerSuperProperties';
if (check.checkProperty(property)) {
this.superProperty = Util.objMerge(superProperty, property);
baseConfig.status.value = JSON.stringify(property);
baseConfig.status.successCode = 20002;
this.code = 200;
successLog();
return true;
}
errorLog();
return false;
}
/**
* 获取超级属性
* @param key 属性KEY
* @return 该KEY的超级属性值
*/
getSuperProperty (key) {
this.resetCode();
baseConfig.status.FnName = '$getSuperProperty';
baseConfig.status.key = key;
check.checkKey(key);
if (this.superProperty.hasOwnProperty(key)) {
var value = this.superProperty[key];
baseConfig.status.successCode = 20010;
baseConfig.status.value = value;
this.code = 200;
successLog();
return value;
}
baseConfig.status.successCode = 20009;
successLog();
return null;
}
/**
* 获取超级属性
* @return 所有超级属性
*/
getSuperProperties () {
this.resetCode();
baseConfig.status.FnName = '$getSuperProperties';
baseConfig.status.successCode = '20010';
baseConfig.status.value = JSON.stringify(this.superProperty);
this.code = 200;
successLog();
return this.superProperty;
}
/**
* 移除超级属性
* @param key 属性key
*/
unRegisterSuperProperty (key) {
this.resetCode();
baseConfig.status.value = key;
baseConfig.status.FnName = '$unRegisterSuperProperty';
//删除 空值 键 空 0
check.checkKey(key);
if (this.superProperty.hasOwnProperty(key)) {
delete this.superProperty[key];
baseConfig.status.successCode = '20003';
this.code = 200;
successLog();
return true;
}
baseConfig.status.successCode = '20011';
successLog();
return false;
}
/**
* 清除超级属性
*/
clearSuperProperties () {
this.resetCode();
baseConfig.status.FnName = '$clearSuperProperties';
baseConfig.status.successCode = '20004';
this.code = 200;
successLog();
this.superProperty = {};
return true;
}
/**
* 立即发送所有收集的信息到服务器
*/
flush () {
if (this.postData.length > 0) {
return this.send();
}
return false;
}
/**
* 立即终止发送的请求
*/
// shutdown () {
// if (this.postFun) {
// this.postFun.abort();
// baseConfig.status.successCode = '20013';
// successLog();
// return true;
// }
// return false
// }
/**
* 设置用户的属性
* @param distinctId 用户ID
* @param isLogin 用户ID是否是登录 ID
* @param properties 用户属性
*/
profileSet (distinctId, isLogin, properties, platform, upLoadTime) {
this.resetCode();
baseConfig.status.FnName = '$profile_set';
if (!check.checkUploadTime(upLoadTime)) {
return false;
}
// properties = check.checkProperty(properties);
if (check.checkDistinctId(distinctId) && check.checkBoolean(isLogin) && check.checkProperty(properties)) {
return this.upLoad(distinctId, isLogin, '$profile_set', properties, platform, upLoadTime);
}
errorLog();
return false;
}
/**
* 首次设置用户的属性,该属性只在首次设置时有效
* @param distinctId 用户ID
* @param isLogin 用户ID是否是登录 ID
* @param properties 用户属性
*/
profileSetOnce (distinctId, isLogin, properties, platform, upLoadTime) {
this.resetCode();
baseConfig.status.FnName = '$profile_set_once';
if (!check.checkUploadTime(upLoadTime)) {
return false;
}
// properties = check.checkProperty(properties);
if (check.checkDistinctId(distinctId) && check.checkBoolean(isLogin) && check.checkProperty(properties)) {
return this.upLoad(distinctId, isLogin, '$profile_set_once', properties, platform, upLoadTime);
}
errorLog();
return false;
}
/**
* 为用户的一个或多个数值类型的属性累加一个数值
* @param distinctId 用户ID
* @param isLogin 用户ID是否是登录 ID
* @param properties 用户属性
*/
profileIncrement (distinctId, isLogin, properties, platform, upLoadTime) {
this.resetCode();
baseConfig.status.FnName = '$profile_increment';
if (!check.checkUploadTime(upLoadTime)) {
return false;
}
if (check.checkDistinctId(distinctId) && check.checkBoolean(isLogin) && Util.paramType(properties) === 'Object') {
if (Object.keys(properties).length > 0) {
for (var i in properties) {
check.checkKey(i);
check.checkIncrementValue(properties[i]);
// if (check.checkIncrementKey(i) && check.checkIncrementValue(properties[i])) {
// return;
// }
}
} else {
baseConfig.status.errorCode = '600026';
baseConfig.status.key = 'properties';
errorLog();
}
return this.upLoad(distinctId, isLogin, '$profile_increment', properties, platform, upLoadTime);
}
if (!Util.paramType(properties) !== 'Object') {
baseConfig.status.key = properties;
baseConfig.status.value = properties;
baseConfig.status.errorCode = '600016';
}
errorLog();
return false;
}
/**
* 追加用户列表类型的属性
* @param distinctId 用户ID
* @param isLogin 用户ID是否是登录 ID
* @param properties 用户属性
*/
profileAppend (distinctId, isLogin, properties, platform, upLoadTime) {
this.resetCode();
baseConfig.status.FnName = '$profile_append';
if (!check.checkUploadTime(upLoadTime)) {
return false;
}
// properties = check.checkProperty(properties);
if (check.checkDistinctId(distinctId) && check.checkBoolean(isLogin) && check.checkProperty(properties)) {
return this.upLoad(distinctId, isLogin, '$profile_append', properties, platform, upLoadTime);
}
errorLog();
return false;
}
/**
* 删除用户某一个属性
* @param distinctId 用户ID
* @param isLogin 用户ID是否是登录 ID
* @param property 用户属性名称
* @throws AnalysysException
*/
profileUnSet (distinctId, isLogin, property, platform, upLoadTime) {
this.resetCode();
baseConfig.status.FnName = '$profile_unset';
if (!check.checkUploadTime(upLoadTime)) {
return false;
}
check.checkUnsetKey(property);
if (check.checkDistinctId(distinctId) && check.checkBoolean(isLogin)) {
var properties = {};
properties[property] = '';
return this.upLoad(distinctId, isLogin, '$profile_unset', properties, platform, upLoadTime);
}
errorLog();
return false;
}
/**
* 删除用户所有属性
* @param distinctId 用户ID
* @param isLogin 用户ID是否是登录 ID
* @throws AnalysysException
*/
profileDelete (distinctId, isLogin, platform, upLoadTime) {
this.resetCode();
baseConfig.status.FnName = '$profile_delete';
if (!check.checkUploadTime(upLoadTime)) {
return false;
}
if (check.checkDistinctId(distinctId) && check.checkBoolean(isLogin)) {
return this.upLoad(distinctId, isLogin, '$profile_delete', {}, platform, upLoadTime);
}
errorLog();
return false;
}
/**
* 关联用户匿名ID和登录ID
* @param aliasId 用户登录ID
* @param distinctId 用户匿名ID
* @throws AnalysysException
*/
alias (aliasId, distinctId, platform, upLoadTime) {
this.resetCode();
baseConfig.status.FnName = '$alias';
if (!check.checkUploadTime(upLoadTime)) {
return false;
}
if (check.checkAliasId(aliasId) && check.checkDistinctId(distinctId)) {
var param = {};
param.$original_id = distinctId;
return this.upLoad(aliasId, true, '$alias', param, platform, upLoadTime);
}
errorLog();
return false;
}
/**
* 追踪用户多个属性的事件
* @param distinctId 用户ID
* @param isLogin 用户ID是否是登录 ID
* @param eventName 事件名称
* @param properties 事件属性
* @throws AnalysysException
*/
track (distinctId, isLogin, eventName, properties, platform, upLoadTime) {
this.resetCode();
baseConfig.status.FnName = eventName;
// baseConfig.status.FnName = eventName || "$track";
if (!check.checkUploadTime(upLoadTime)) {
return false;
}
upLoadTime = upLoadTime || +new Date();
check.checkEventName(eventName);
// track 没有 properties 也要上报
if (properties == undefined) {
properties = {};
if (check.checkDistinctId(distinctId) && check.checkBoolean(isLogin)) {
return this.upLoad(distinctId, isLogin, eventName, properties, platform, upLoadTime, true);
}
} else {
// properties = check.checkProperty(properties);
if (check.checkDistinctId(distinctId) && check.checkBoolean(isLogin) && check.checkProperty(properties)) {
return this.upLoad(distinctId, isLogin, eventName, properties, platform, upLoadTime, true);
}
}
errorLog();
return false;
}
/**
* 上传数据,首先校验相关KEY和VALUE,符合规则才可以上传
* @param distinctId 用户标识
* @param eventName 事件名称
* @param properties 属性
* @throws AnalysysException
*/
upLoad (distinctId, isLogin, eventName, properties, platform, upLoadTime, merFlag) {
//API 方法校验了参数,上传就不校验了
var eventMap = {};
eventMap.appid = this.appId;
eventMap.xwho = distinctId;
eventMap.xwhen = +upLoadTime || +new Date();
eventMap.xwhat = eventName;
// 平台校验放在upload 里面公共
if (platform == undefined) {
check.checkPlatform(platform);
return false;
}
platform = check.checkPlatform(platform);
this.baseProperties.$is_login = isLogin;
this.baseProperties.$platform = platform;
//属性合并
properties = Util.objMerge(properties, this.baseProperties); //合并基本属性
if (merFlag == true) { // 只有 track 需要合并超级属性
properties = Util.objMerge(this.superProperty, properties); //合并超级属性
}
eventMap.xcontext = properties;
this.postData.push(eventMap);
//数据上传 满足 一定的条数上传( 数据可设置 ),立即上传;
if (this.postData.length >= this.postNumber) {
return this.send();
}
//没有设置上传条数,上传间隔时间,立即上传。
if (!this.postNumber && !this.upPostDataTime) {
return this.send();
}
//设置了上传间隔时间(可以为0。设置0 或者 不设置 默认为 0), 上传条数大于 0 的情况下,会进入到时间条件的上传,到达时间就上传。
if (this.upPostDataTime >= 0 && this.postData.length > 0) {
var _this = this;
if (this.timer) {
return true;
} else {
this.timer = setTimeout(function () {
_this.send();
}, this.upPostDataTime);
return true;
}
}
}
/**
* 上传方式
*/
send () {
var _this = this;
this.resetCode();
// 对appid 进行校验
if (!check.checkAppid(this.appId)) return false;
// 没有 落文件 是 会对上报数据进项校验的。有落文件 就忽略这一步
if (Util.paramType(this.logCollector) == 'Null' || Util.paramType(this.logCollector) != 'Object') {
var flag = check.checkBase(this.appId, this.uploadURL);
if (!flag) return false;
}
var postData = this.postData;
// 来到这里 说明满足条件 写入文件
if (Util.paramType(this.logCollector) == 'Object') {
var logC = this.logCollector;
return logC.LogCol(_this.postData, function (err) {
if (!err) {
_this.postData = [];
}
}, this.baseProperties.$debug);
}
baseConfig.status.key = this.uploadURL;
baseConfig.status.value = JSON.stringify(postData);
baseConfig.status.successCode = '20012';
successLog();
//发送请求时间 内产生的数据操作。
this.postData = []; //此时清空缓存 ,下次进入到 send的 是一个 可发送的新数据。
clearTimeout(this.timer);
this.timer = null;
let resRequest = request("POST", this.uploadURL, {
json: true,
json: postData
});
if (resRequest.statusCode == 200) {
if (Util.paramType(resRequest.body) === 'Object') {
//成功 打印成功日志 清空定时器 失败 打印失败日志 数据存储下来,下次上报
if (body.code == 200) {
baseConfig.status.successCode = '20001';
this.code = 200;
successLog();
return true;
}
baseConfig.status.errorCode = '60008';
errorLog();
return false;
} else {
//先 base64 解密 之后 gzip 解压。
var buffer = resRequest.body.toString();
if (buffer == "H4sIAAAAAAAAAKtWSs5PSVWyMjIwqAUAVAOW6gwAAAA=" || "{\"code\":200}") {
baseConfig.status.successCode = '20001';
this.code = 200;
successLog();
return true;
}
baseConfig.status.errorCode = '60008';
errorLog();
return false;
}
} else {
baseConfig.status.errorCode = '60008';
errorLog();
return false;
}
}
}
// 之前版本直接抛出
export default AnalysysAgent;