Numerous graph fixes

This commit is contained in:
Cryptkeeper 2015-12-18 21:23:24 -06:00
parent 76a4cd1f95
commit 454627b2a9
6 changed files with 82 additions and 22 deletions

10
app.js

@ -93,9 +93,7 @@ function pingAll() {
lastGraphPush[network.ip] = timeMs; lastGraphPush[network.ip] = timeMs;
// Don't have too much data! // Don't have too much data!
if (graphData[network.ip].length >= 24 * 60) { util.trimOldPings(graphData);
graphData[network.ip].shift();
}
graphData[network.ip].push([timeMs, res ? res.players.online : 0]); graphData[network.ip].push([timeMs, res ? res.players.online : 0]);
@ -139,6 +137,8 @@ function startServices() {
logger.log('info', 'Accepted connection: %s, total clients: %d', client.request.connection.remoteAddress, connectedClients); logger.log('info', 'Accepted connection: %s, total clients: %d', client.request.connection.remoteAddress, connectedClients);
setTimeout(function() { setTimeout(function() {
client.emit('setGraphDuration', config.graphDuration);
// Send them our previous data, so they have somewhere to start. // Send them our previous data, so they have somewhere to start.
client.emit('updateMojangServices', mojang.toMessage()); client.emit('updateMojangServices', mojang.toMessage());
@ -178,7 +178,7 @@ if (config.logToDatabase) {
var timestamp = util.getCurrentTimeMs(); var timestamp = util.getCurrentTimeMs();
db.queryPings(24 * 60 * 60 * 1000, function(data) { db.queryPings(config.graphDuration, function(data) {
graphData = util.convertPingsToGraph(data); graphData = util.convertPingsToGraph(data);
logger.log('info', 'Queried and parsed ping history in %sms', util.getCurrentTimeMs() - timestamp); logger.log('info', 'Queried and parsed ping history in %sms', util.getCurrentTimeMs() - timestamp);
@ -186,7 +186,7 @@ if (config.logToDatabase) {
startServices(); startServices();
}); });
} else { } else {
logger.warn('Database logging is not enabled. You can enable it by setting "logToDatabase" to true in config.json. This requires sqlite3 to be installed.'); logger.log('warn', 'Database logging is not enabled. You can enable it by setting "logToDatabase" to true in config.json. This requires sqlite3 to be installed.');
startServices(); startServices();
} }

@ -231,6 +231,8 @@ $(document).ready(function() {
var mojangServicesUpdater; var mojangServicesUpdater;
var sortServersTask; var sortServersTask;
var graphDuration;
socket.on('connect', function() { socket.on('connect', function() {
$('#tagline-text').text('Loading...'); $('#tagline-text').text('Loading...');
@ -262,6 +264,10 @@ $(document).ready(function() {
$('#big-graph-controls').css('display', 'none'); $('#big-graph-controls').css('display', 'none');
}); });
socket.on('setGraphDuration', function(value) {
graphDuration = value;
});
socket.on('historyGraph', function(rawData) { socket.on('historyGraph', function(rawData) {
displayedGraphData = rawData; displayedGraphData = rawData;
@ -295,23 +301,25 @@ $(document).ready(function() {
}); });
socket.on('updateHistoryGraph', function(rawData) { socket.on('updateHistoryGraph', function(rawData) {
var targetGraphData = displayedGraphData[rawData.ip]; // Prevent race conditions.
if (!graphDuration) {
// If it's not in our display group, push it to the hidden group instead so it can be restored and still be up to date. return;
if (!targetGraphData) {
targetGraphData = hiddenGraphData[rawData.ip];
} }
if (targetGraphData.length > 24 * 60) { // If it's not in our display group, use the hidden group instead.
targetGraphData.shift(); var targetGraphData = displayedGraphData[rawData.ip] ? displayedGraphData : hiddenGraphData;
trimOldPings(targetGraphData, graphDuration);
targetGraphData[rawData.ip].push([rawData.timestamp, rawData.players]);
// Redraw if we need to.
if (displayedGraphData[rawData.ip]) {
historyPlot.setData(convertGraphData(displayedGraphData));
historyPlot.setupGrid();
historyPlot.draw();
} }
targetGraphData.push([rawData.timestamp, rawData.players]);
historyPlot.setData(convertGraphData(displayedGraphData));
historyPlot.setupGrid();
historyPlot.draw();
}); });
socket.on('add', function(servers) { socket.on('add', function(servers) {

@ -36,6 +36,30 @@ function isMobileBrowser() {
return check; return check;
} }
function trimOldPings(data, graphDuration) {
var keys = Object.keys(data);
var timeMs = new Date().getTime();
for (var x = 0; x < keys.length; x++) {
var listing = data[keys[x]];
var toSplice = [];
for (var i = 0; i < listing.length; i++) {
var entry = listing[i];
if (timeMs - entry[0] > graphDuration) {
toSplice.push(i);
}
}
for (var i = 0; i < toSplice.length; i++) {
listing.splice(toSplice[i], 1);
}
}
}
function handlePlotHover(event, pos, item) { function handlePlotHover(event, pos, item) {
if (item) { if (item) {
var text = getTimestamp(item.datapoint[0] / 1000) + '\ var text = getTimestamp(item.datapoint[0] / 1000) + '\

@ -188,5 +188,6 @@
"pingAll": 2000, "pingAll": 2000,
"connectTimeout": 1500 "connectTimeout": 1500
}, },
"logToDatabase": true "logToDatabase": true,
"graphDuration": 86400000
} }

@ -27,6 +27,7 @@ exports.start = function(callback) {
if (requestUrl === '/status.json') { if (requestUrl === '/status.json') {
res.setHeader('Content-Type', 'text/plain'); res.setHeader('Content-Type', 'text/plain');
res.write(JSON.stringify({ res.write(JSON.stringify({
error: true, error: true,
message: 'API deprecated.' message: 'API deprecated.'
@ -41,6 +42,7 @@ exports.start = function(callback) {
fs.createReadStream(file).pipe(res); fs.createReadStream(file).pipe(res);
} else { } else {
res.statusCode = 404; res.statusCode = 404;
res.write('404'); res.write('404');
res.end(); res.end();
@ -50,8 +52,7 @@ exports.start = function(callback) {
server.listen(config.site.port, config.site.ip); server.listen(config.site.port, config.site.ip);
// I don't like this. But it works, I think. // I don't like this. But it works, I think.
io = io.listen(server); exports.io = io.listen(server);
exports.io = io;
// Since everything is loaded, do some final prep work. // Since everything is loaded, do some final prep work.
logger.log('info', 'Started on %s:%d', config.site.ip, config.site.port); logger.log('info', 'Started on %s:%d', config.site.ip, config.site.port);

@ -61,6 +61,29 @@ function trimUselessPings(data) {
} }
} }
exports.trimOldPings = function(data) {
var keys = Object.keys(data);
var timeMs = exports.getCurrentTimeMs();
for (var x = 0; x < keys.length; x++) {
var listing = data[keys[x]];
var toSplice = [];
for (var i = 0; i < listing.length; i++) {
var entry = listing[i];
if (timeMs - entry[0] > config.graphDuration) {
toSplice.push(i);
}
}
for (var i = 0; i < toSplice.length; i++) {
listing.splice(toSplice[i], 1);
}
}
}
exports.getCurrentTimeMs = function() { exports.getCurrentTimeMs = function() {
return new Date().getTime(); return new Date().getTime();
}; };
@ -89,5 +112,8 @@ exports.convertPingsToGraph = function(sqlData) {
// Break it into minutes. // Break it into minutes.
trimUselessPings(graphData); trimUselessPings(graphData);
// Drop old data.
exports.trimOldPings(graphData);
return graphData; return graphData;
}; };