forked from project-sunbird/sunbird-telemetry-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelemetry-service.js
More file actions
106 lines (103 loc) · 4.55 KB
/
telemetry-service.js
File metadata and controls
106 lines (103 loc) · 4.55 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
const uuidv1 = require('uuid/v1'),
request = require('request'),
DispatcherClass = require('../dispatcher/dispatcher').Dispatcher;
config = require('../envVariables')
// TODO: Make this efficient. Implementation to be similar to typesafe config. Right now one configuration holds
// together all supported transport configurations
class TelemetryService {
constructor(Dispatcher, config) {
this.config = config;
this.dispatcher = this.config.localStorageEnabled === 'true' ? new Dispatcher(config) : undefined;
}
dispatch(req, res) {
const message = req.body;
message.did = req.get('x-device-id');
message.channel = req.get('x-channel-id');
message.pid = req.get('x-app-id');
if (!message.mid) message.mid = uuidv1();
message.syncts = new Date().getTime();
let data;
try {
data = JSON.stringify(message);
} catch(err){
console.log('error while signifying', err);
}
if(!data){
this.sendError(res, { id: 'api.telemetry', params: { err: 'INVALID_DATA' }});
return;
}
if (this.config.localStorageEnabled === 'true' || this.config.telemetryProxyEnabled === 'true') {
if (this.config.localStorageEnabled === 'true' && this.config.telemetryProxyEnabled !== 'true') {
// Store locally and respond back with proper status code
this.dispatcher.dispatch(message.mid, data, this.getRequestCallBack(req, res));
} else if (this.config.localStorageEnabled === 'true' && this.config.telemetryProxyEnabled === 'true') {
// Store locally and proxy to the specified URL. If the proxy fails ignore the error as the local storage is successful. Do a sync later
const options = this.getProxyRequestObj(req, data);
request.post(options, (err, data) => {
if (err) console.error('Proxy failed:', err);
else console.log('Proxy successful! Server responded with:', data.body);
});
this.dispatcher.dispatch(message.mid, data, this.getRequestCallBack(req, res));
} else if (this.config.localStorageEnabled !== 'true' && this.config.telemetryProxyEnabled === 'true') {
// Just proxy
const options = this.getProxyRequestObj(req, data);
request.post(options, this.getRequestCallBack(req, res));
}
} else {
this.sendError(res, { id: 'api.telemetry', params: { err: 'Configuration error' }});
}
}
health(req, res) {
if (this.config.localStorageEnabled === 'true') {
this.dispatcher.health((healthy) => {
if (healthy)
this.sendSuccess(res, { id: 'api.health' });
else
this.sendError(res, { id: 'api.health', params: { err: 'Telemetry API is unhealthy' } });
})
} else if (this.config.telemetryProxyEnabled === 'true') {
this.sendSuccess(res, { id: 'api.health' });
} else {
this.sendError(res, { id: 'api.health', params: { err: 'Configuration error' } });
}
}
getRequestCallBack(req, res) {
return (err, data) => {
if (err) this.sendError(res, { id: 'api.telemetry', params: { err: err } });
else this.sendSuccess(res, { id: 'api.telemetry' });
}
}
sendError(res, options) {
const resObj = {
id: options.id,
ver: options.ver || '1.0',
ets: new Date().getTime(),
params: options.params || {},
responseCode: options.responseCode || 'SERVER_ERROR'
}
res.status(500);
res.json(resObj);
}
sendSuccess(res, options) {
const resObj = {
id: options.id,
ver: options.ver || '1.0',
ets: new Date().getTime(),
params: options.params || {},
responseCode: options.responseCode || 'SUCCESS'
}
res.status(200);
res.json(resObj);
}
getProxyRequestObj(req, data) {
const headers = { 'authorization': 'Bearer ' + config.proxyAuthKey };
if (req.get('content-type')) headers['content-type'] = req.get('content-type');
if (req.get('content-encoding')) headers['content-encoding'] = req.get('content-encoding');
return {
url: this.config.proxyURL,
headers: headers,
body: data
};
}
}
module.exports = new TelemetryService(DispatcherClass, config);