Histochart implemented to visualize ontimes

This commit is contained in:
Dominic Bosch 2014-04-23 16:08:51 +02:00
parent af9caac9e5
commit d5400603cc
7 changed files with 1099 additions and 173 deletions

View file

@ -12,6 +12,8 @@ HTTP Listener
# - [Request Handler](request-handler.html)
requestHandler = require './request-handler'
# - [Persistence](persistence.html)
db = require './persistence'
# - Node.js Modules: [path](http://nodejs.org/api/path.html) and
# [querystring](http://nodejs.org/api/querystring.html)
@ -67,8 +69,8 @@ activateWebHook = ( app, name ) =>
indexEvent name, body, resp
# This is a hack to quickly allow storing of public accessible data
if name is 'uptimestatistics'
path = path.resolve __dirname, '..', 'webpages', 'public', 'data', 'histochart.json'
fs.writeFile pathUsers, JSON.stringify( body, undefined, 2 ), 'utf8', ( err ) ->
fPath = path.resolve __dirname, '..', 'webpages', 'public', 'data', 'histochart.json'
fs.writeFile fPath, JSON.stringify( JSON.parse( body ), undefined, 2 ), 'utf8'
###
Initializes the request routing and starts listening on the given port.

View file

@ -10,7 +10,8 @@
"file-path": "logs/server.log"
},
"webhooks": [
"github"
"github",
"uptimestatistics"
],
"keygen-passphrase": "[TODO this has to come from prompt when server is started!]"
}

View file

@ -1,15 +1,20 @@
fs = require 'fs'
# libnmap = require 'node-libnmap'
ping = require 'net-ping'
request = require 'request'
# request = require 'request'
needle = require 'needle'
try
arrHosts = JSON.parse fs.readFileSync 'hostlist.json', 'utf8'
histData = JSON.parse fs.readFileSync 'histochart.json', 'utf8'
catch err
console.error err
console.error "Error reading host list file"
process.exit()
remoteUrl = "http://ec2-54-226-188-9.compute-1.amazonaws.com:8126"
# remoteUrl = "localhost:8125"
# console.log arrHosts
# libnmap.nmap 'scan',
@ -25,6 +30,9 @@ session = ping.createSession()
everyMins = 10
oHosts = {}
oPings = {}
if histData
oHosts = histData.hosts
oPings = histData.pingtimes
fPollHosts = () ->
semaphore = arrHosts.length
pingTime = (new Date()).toISOString()
@ -36,14 +44,16 @@ fPollHosts = () ->
oHosts[ target ] = {}
oHosts[ target ][ pingTime ] = (new Date( rcvd - sent )).getTime()
oPings[ pingTime ].ips.push target
if --semaphore is 0
console.log 'All ping requests returned, pushing event into the system'
oPings[ pingTime ].sum = oPings[ pingTime ].ips.length
fPushEvent
evt =
currentlyon: oPings[ pingTime ].ips.length
pingtimes: oPings
hosts: oHosts
fPushEvent evt
fs.writeFile 'histochart.json', JSON.stringify( evt, undefined, 2 ), 'utf8'
console.log "Pinging again in #{ everyMins } minutes"
setTimeout fPollHosts, everyMins * 60 * 1000
@ -51,15 +61,8 @@ fPollHosts = () ->
fPollHosts()
options =
method: 'POST'
json: true
jar: true
fPushEvent = ( evt ) ->
options.url = remoteUrl + '/webhooks/uptimestatistics'
options.body = JSON.stringify evt
request options, ( err, resp, body ) ->
needle.post remoteUrl + '/webhooks/uptimestatistics', JSON.stringify( evt ), ( err, resp, body ) ->
if err or resp.statusCode isnt 200
console.log 'Error in pushing event!'
else

View file

@ -10,14 +10,20 @@ HTTP Listener
*/
(function() {
var activateWebHook, app, exports, express, indexEvent, initRouting, path, qs, requestHandler;
var activateWebHook, app, db, exports, express, fs, indexEvent, initRouting, path, qs, requestHandler;
requestHandler = require('./request-handler');
db = require('./persistence');
path = require('path');
qs = require('querystring');
fs = require('fs');
path = require('path');
express = require('express');
app = express();
@ -68,7 +74,12 @@ HTTP Listener
return body += data;
});
return req.on('end', function() {
return indexEvent(name, body, resp);
var fPath;
indexEvent(name, body, resp);
if (name === 'uptimestatistics') {
fPath = path.resolve(__dirname, '..', 'webpages', 'public', 'data', 'histochart.json');
return fs.writeFile(fPath, JSON.stringify(JSON.parse(body), void 0, 2), 'utf8');
}
});
});
};

View file

@ -1,45 +0,0 @@
<!DOCTYPE HTML>
<html>
<head>
<title></title>
<link href='http://fonts.googleapis.com/css?family=Roboto:300' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Nunito' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="style.css">
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' type='text/javascript'></script>
<script type='text/javascript'>
var fRun = function () {
if( window.$ === undefined ) {
document.getElementById('info').innerHTML = 'Not all required libraries have been loaded. Are you connected to the internet?';
} else {
$.ajax({
url: "data/users.json",
dataType: "json",
success: function(response) {
$.each(response.Users, function(item) {
informationArray.push(item);
});
informationArray.push("success");
}
});
}
};
window.addEventListener( "load" , fRun, true );
</script>
<script type='text/javascript'>
{{{script}}}
</script>
{{{remote_scripts}}}
</head>
<body>
{{{menubar}}}
<div id="info"></div>
<div id="mainbody">
<p></p>
<div id="pagetitle"></div>
{{{content}}}
</div>
</body>
</html>

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,39 @@
<html>
<head>
<script type="text/javascript"
src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['corechart']}]}"></script>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' type='text/javascript'></script>
</head>
<body>
<script>
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('datetime', 'Time');
data.addColumn('number', '#Online');
var options_lines = {
title: 'Online statistics',
curveType:'function',
lineWidth: 2,
intervals: { 'style':'line' },
legend: 'none',
};
var chart_lines = new google.visualization.LineChart(document.getElementById('chart_lines'));
$.getJSON('data/histochart.json', function(d) {
var dat = d.pingtimes;
for(var prop in dat) {
data.addRow([new Date(prop), dat[prop].sum ]);
}
chart_lines.draw(data, options_lines);
});
}
</script>
<div id="chart_lines" style="width: 900px; height: 500px;"></div>
</body>
</html>