forked from tapd8/apig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.js
More file actions
64 lines (51 loc) · 1.6 KB
/
report.js
File metadata and controls
64 lines (51 loc) · 1.6 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
const log = require('./dist').log.app;
const request = require('request');
const path = require('path');
const appConfig = require('./config');
const getToken = require('./tapdata').getToken;
const hostname = require('os').hostname();
const startTime = new Date().getTime();
const apiServerStatus = {
worker_status: {}
};
const report = function (data, token) {
const configPath = path.join(__dirname, 'config.json');
const reportServerUrl = appConfig.tapDataServer.reportUrl + '?access_token=' + token;
if (!reportServerUrl || !reportServerUrl)
return;
data = Object.assign(data || {}, appConfig.reportData);
data['start_time'] = startTime;
// data['ping_time'] = new Date().getTime();
//data['worker_ip'] = hostname;
data['hostname'] = hostname;
data['port'] = appConfig.port;
data['total_thread'] = 2;
data['running_thread'] = apiServerStatus.worker_status.status === 'running' ? 2 : 1;
data['version'] = appConfig.version;
Object.assign(data, apiServerStatus);
try {
// log.debug('report data', data);
request.post({
url: reportServerUrl + encodeURI(`&[where][process_id]=${appConfig.reportData.process_id}&[where][worker_type]=${appConfig.reportData.worker_type}`),
json: true,
body: data
}, (err, resp, body) => {
if (err) {
log.error('report fail', err);
} else {
// log.debug(`report complete:`, body);
}
});
} catch (e) {
log.error('report fail', e);
}
};
setInterval(() => {
getToken(token => {
if (token)
report(null, token)
})
}, appConfig.reportIntervals || 1000);
exports.setStatus = function (status) {
Object.assign(apiServerStatus, status);
};