From db915edefc6cbd8ffe0d10a79360407360ec02cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Tue, 22 Oct 2013 14:01:50 +0200 Subject: [PATCH] fixed storing user count in redis (thx badboy_) made user count api working and added data to graph --- node/ip-poll.js | 21 +++++++++++++++------ node/public/js/graph.js | 13 ++++++++----- node/statusbot.js | 17 +++++++---------- node/test.js | 30 ++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 21 deletions(-) create mode 100644 node/test.js diff --git a/node/ip-poll.js b/node/ip-poll.js index fd24ba0..fe51830 100644 --- a/node/ip-poll.js +++ b/node/ip-poll.js @@ -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); + }); }; }; diff --git a/node/public/js/graph.js b/node/public/js/graph.js index 16a5c5b..a870d5a 100644 --- a/node/public/js/graph.js +++ b/node/public/js/graph.js @@ -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(); diff --git a/node/statusbot.js b/node/statusbot.js index 5c6934c..70efa66 100644 --- a/node/statusbot.js +++ b/node/statusbot.js @@ -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); diff --git a/node/test.js b/node/test.js new file mode 100644 index 0000000..2cc8cfd --- /dev/null +++ b/node/test.js @@ -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(); + }); + + +}); \ No newline at end of file