forked from tapd8/apig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreportApiCallStats.js
63 lines (58 loc) · 2.07 KB
/
reportApiCallStats.js
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
const {getToken, removeToken} = require('./tapdata');
const requestOfcalls = require('request');
const Conf = require('conf');
const config = new Conf();
const apiStatsRptTimeStamp = new Conf({ configName: "apiStatsRptTimeStamp" });
const reportTaskListOfApiStats = new Conf({ configName: "reportTaskListOfApiStats" });
setInterval(() => {
// let newV = reportApiCallStats.get();
let needReported = [];
for (let o of reportTaskListOfApiStats) {
// console.log(o);
if (!o[1].reporting) {
needReported.push(o[1]);
}
if (needReported.length >= config.get("apiStatsBatchReport._maxApiStatsBatchSizePerReportPost")) {
break; // closes iterator, triggers return
}
}
// console.log(needReported);
reportApiCallStats(needReported);
}, config.get("apiStatsBatchReport._reportIntervals"));
function reportApiCallStats(apiAuditLogs) {
if (apiAuditLogs && Array.isArray(apiAuditLogs) && apiAuditLogs.length > 0) {
apiAuditLogs.forEach((apicall) => {
apicall.report_time = new Date().getTime();
reportTaskListOfApiStats.set(`${apicall.call_id}.reporting`, true);
});
getToken(function (token) {
let url = config.get("tapDataServer.url") + '/api/ApiCalls?access_token=' + token;
// console.log(url);
requestOfcalls.post({
url: url,
json: true,
body: apiAuditLogs,
}, (err, resp, body) => {
// console.log("[email protected]:45:", body);
if (err || body.error) {
console.error('[email protected]:47:', err, body);
apiAuditLogs.forEach(errCall => {
reportTaskListOfApiStats.delete(`${errCall.call_id}.reporting`);
});
} else if (resp.statusCode === 401 || resp.statusCode === 403) {
console.error('Access token Expired');
removeToken();
} else {
apiStatsRptTimeStamp.set("lastReport", new Date());
// console.log("resp:\n", resp);
// console.log("body:\n", body);
let reported = Array.isArray(body) ? body : [body];
reported.forEach((call) => {
reportTaskListOfApiStats.delete(call.call_id);
});
}
});
});
}
}
// exports.reportApiCallStats = reportApiCallStats;