fixed storing user count in redis (thx badboy_)
made user count api working and added data to graph
This commit is contained in:
parent
82f8867161
commit
db915edefc
|
@ -9,12 +9,9 @@ var IpPoll = function(switchaddr, hostsaddr) {
|
|||
var self = this;
|
||||
|
||||
var redisClient = redis.createClient();
|
||||
|
||||
var regexp = /\(([0-9]+) hosts* up\)/;
|
||||
// var nmap = "nmap -n -sP -T5 --host-timeout 10ms ";
|
||||
var nmap = "nmap -n -sP -T5 ";
|
||||
|
||||
|
||||
redisClient.on("connect", function () {
|
||||
console.log("connected to redis");
|
||||
self.emit('ready');
|
||||
|
@ -25,9 +22,13 @@ var IpPoll = function(switchaddr, hostsaddr) {
|
|||
if(error == null) {
|
||||
var matches = regexp.exec(stdout);
|
||||
if(matches != null && matches.length == 2) {
|
||||
var num = parseInt(matches[1]);
|
||||
var time = Date.now();
|
||||
|
||||
redisClient.zadd('onlinecount', Date.now(), num, function() {
|
||||
redisClient.zremrangebyscore('onlinecount', "-inf", time - 7*24*60*1000);
|
||||
|
||||
var num = matches[1];
|
||||
|
||||
redisClient.zadd('onlinecount', time, time + "|" + num, function() {
|
||||
self.emit('doneCount', num);
|
||||
});
|
||||
}
|
||||
|
@ -50,8 +51,16 @@ var IpPoll = function(switchaddr, hostsaddr) {
|
|||
|
||||
this.getHistory = function(start, end, callback) {
|
||||
|
||||
redisClient.zrangebyscore('onlinecount', "-inf", "+inf", function(err, replies) {
|
||||
var data = [];
|
||||
|
||||
callback(data);
|
||||
replies.forEach(function (reply, i) {
|
||||
var line = reply.split('|');
|
||||
data.push( { at: moment(parseInt(line[0])).format(), value: line[1] });
|
||||
});
|
||||
|
||||
callback(data);
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
@ -2,20 +2,25 @@
|
|||
|
||||
$(function() {
|
||||
|
||||
// TODO: we could try Rickshaw.Graph.JSONP instead of using jQuery getJSON
|
||||
// http://code.shutterstock.com/rickshaw/
|
||||
|
||||
$.getJSON("/api/usercount", function(data, textStatus, jqXHR) {
|
||||
|
||||
var offset = new Date().getTimezoneOffset();
|
||||
var newData = [];
|
||||
|
||||
// TODO: we could change the API to directly give us { x: , y: } objects
|
||||
|
||||
for (var i = 0; i < data.datapoints.length; i++) {
|
||||
var date = moment(data.datapoints[i].at).unix() - offset * 60;
|
||||
var value = parseFloat(data.datapoints[i].value);
|
||||
var value = parseInt(data.datapoints[i].value);
|
||||
newData.push({x: date, y: value});
|
||||
}
|
||||
|
||||
var graph = new Rickshaw.Graph( {
|
||||
element: document.querySelector("#graph"),
|
||||
renderer: 'bar',
|
||||
renderer: 'line',
|
||||
series: [ {
|
||||
data: newData,
|
||||
name: 'Benutzer',
|
||||
|
@ -23,11 +28,9 @@ $(function() {
|
|||
} ]
|
||||
} );
|
||||
|
||||
|
||||
|
||||
new Rickshaw.Graph.Axis.Time({graph: graph}).render();
|
||||
new Rickshaw.Graph.Axis.Y({graph: graph}).render();
|
||||
|
||||
new Rickshaw.Graph.HoverDetail({ graph: graph, yFormatter: function (y) { return y.toFixed(0) } });
|
||||
|
||||
graph.render();
|
||||
|
||||
|
|
|
@ -54,7 +54,6 @@ ippoll.on('doneState', function (state) {
|
|||
spaceanswer.state.lastchange = new Date();
|
||||
simpleanswer.state = state;
|
||||
simpleanswer.lastchange = spaceanswer.state.lastchange;
|
||||
|
||||
io.sockets.emit('sdata', { data: simpleanswer });
|
||||
});
|
||||
|
||||
|
@ -104,20 +103,18 @@ app.get('/api/usercount', function (req, res) {
|
|||
//TODO: respect query params "start", "end", "interval" (s) and "limit" (like Xively)
|
||||
// maybe skip "interval" if code gets too complex :)
|
||||
|
||||
usercountanswer.datapoints.length = 0;
|
||||
usercountanswer.at = simpleanswer.lastchange;
|
||||
usercountanswer.current_value = simpleanswer.count;
|
||||
ippoll.getHistory("-inf","+inf", function(data) {
|
||||
usercountanswer.datapoints.length = 0;
|
||||
usercountanswer.at = simpleanswer.lastchange;
|
||||
usercountanswer.current_value = simpleanswer.count;
|
||||
usercountanswer.datapoints = data;
|
||||
res.send(usercountanswer);
|
||||
});
|
||||
|
||||
for(var i=100; i > 0;i--) {
|
||||
usercountanswer.datapoints.push( { at: moment().subtract("minute", i), value: parseInt(Math.random()*20) } );
|
||||
}
|
||||
|
||||
res.send(usercountanswer);
|
||||
});
|
||||
|
||||
app.get('/db', routes.db);
|
||||
app.post('/form', routes.form);
|
||||
|
||||
app.get('/', routes.index);
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
var moment = require("moment");
|
||||
var redis = require("redis");
|
||||
var redisClient = redis.createClient();
|
||||
|
||||
redisClient.on("connect", function () {
|
||||
console.log("connected to redis");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
redisClient.zrangebyscore('onlinecount', "-inf", "+inf", function(err, replies) {
|
||||
|
||||
var data = [];
|
||||
|
||||
replies.forEach(function (reply, i) {
|
||||
|
||||
var line = reply.split('|');
|
||||
data.push( { at: moment(parseInt(line[0])).format(), value: line[1] });
|
||||
|
||||
|
||||
});
|
||||
|
||||
console.log(data);
|
||||
|
||||
redisClient.quit();
|
||||
});
|
||||
|
||||
|
||||
});
|
Loading…
Reference in New Issue