-
Notifications
You must be signed in to change notification settings - Fork 2
/
centralDashClient.js
62 lines (56 loc) · 1.62 KB
/
centralDashClient.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
var app = require('express')();
var http = require('http').Server(app);
var urlib = require('http');
var zmq = require('zmq');
var redis = require("redis");
// setup
var cur_second;
var update_interval = 1000;
var central_server_connection_string = "tcp://10.0.1.1:5555";
var stat_data_options = {
host: 'localhost',
port: 80,
path: '/log/'
};
// zmq client
var requester = zmq.socket('req');
requester.connect(central_server_connection_string);
requester.on("message", function(reply) {});
///// redis client /////
var client = redis.createClient();
function replacer(key, value) {
if (typeof value === "number") {
return value.toString();
}
return value;
}
// update server loop
setInterval(function(){
cur_second = Date.now()/1000|0;
tmp = [];
// for redis based updates
/*
client.hgetall(cur_second-1, function (err, obj) {
var data_object = {"timestamp":cur_second, "interval":update_interval, "data":JSON.stringify(obj)}
requester.send(JSON.stringify(data_object));
});
*/
// for 'curl' based updates
urlib.get(stat_data_options, function(res) {
res.on('data', function(chunk) {
tmp.push(chunk);
});
res.on('end', function(e) {
var data = JSON.parse(tmp.join(''));
var data_object = {"timestamp":cur_second, "interval":update_interval, "data":JSON.stringify(data, replacer)}
requester.send(JSON.stringify(data_object));
});
res.on('error', function(e) {
return;
});
})
}, update_interval);
// end zmq on quit
process.on('SIGINT', function() {
producer.close();
});