Minetrack/lib/util.js

151 lines
3.3 KiB
JavaScript
Raw Normal View History

2016-02-01 10:49:54 +00:00
var logger = require('./logger');
var config = require('../config.json');
var servers = require('../servers.json');
var serverNameLookup = [];
var bootTime;
2016-02-01 10:49:54 +00:00
2016-02-01 11:14:33 +00:00
// Finds a server in servers.json with a matching IP.
// If it finds one, it caches the result for faster future lookups.
2016-02-01 10:49:54 +00:00
function getServerNameByIp(ip) {
var lookupName = serverNameLookup[ip];
if (lookupName) {
return lookupName;
}
for (var i = 0; i < servers.length; i++) {
var entry = servers[i];
if (entry.ip === ip) {
2016-02-01 10:53:40 +00:00
serverNameLookup[entry.ip] = entry.name;
2016-02-01 10:49:54 +00:00
return entry.name;
}
}
}
2016-02-01 11:14:33 +00:00
// Returns a list of configured server IPs from servers.json
2016-02-01 10:55:37 +00:00
function getServerIps() {
var ips = [];
for (var i = 0; i < servers.length; i++) {
ips.push(servers[i].ip);
}
return ips;
}
2015-12-18 07:45:38 +00:00
// This method is a monstrosity.
// Since we loaded ALL pings from the database, we need to filter out the pings so each entry is a minute apart.
// This is done by iterating over the list, since the time between each ping can be completely arbitrary.
function trimUselessPings(data) {
var keys = Object.keys(data);
for (var i = 0; i < keys.length; i++) {
var listing = data[keys[i]];
var lastTimestamp = 0;
var filteredListing = [];
for (var x = 0; x < listing.length; x++) {
var entry = listing[x];
// 0 is the index of the timestamp.
// See the convertPingsToGraph method.
if (entry[0] - lastTimestamp >= 60 * 1000) {
2015-12-18 08:17:39 +00:00
// This second check tries to smooth out randomly dropped pings.
// By default we only want entries that are online (playerCount > 0).
// This way we'll keep looking forward until we find one that is online.
// However if we can't find one within a reasonable timeframe, select the sucky one.
if (entry[0] - lastTimestamp >= 120 * 1000 || entry[1] > 0) {
filteredListing.push(entry);
lastTimestamp = entry[0];
}
2015-12-18 07:45:38 +00:00
}
}
data[keys[i]] = filteredListing;
}
}
2015-12-19 03:23:24 +00:00
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);
}
}
}
2015-11-09 00:34:17 +00:00
exports.getCurrentTimeMs = function() {
return new Date().getTime();
2015-11-24 23:24:17 +00:00
};
exports.setIntervalNoDelay = function(func, delay) {
var task = setInterval(func, delay);
func();
return task;
2015-12-18 07:45:38 +00:00
};
exports.convertPingsToGraph = function(sqlData) {
2016-02-01 10:55:37 +00:00
var serverIps = getServerIps();
2015-12-18 07:45:38 +00:00
var graphData = {};
2016-02-01 11:44:07 +00:00
var startTime = exports.getCurrentTimeMs();
2016-02-01 11:43:23 +00:00
2015-12-18 07:45:38 +00:00
for (var i = 0; i < sqlData.length; i++) {
var entry = sqlData[i];
2016-02-01 10:55:37 +00:00
if (serverIps.indexOf(entry.ip) === -1) {
continue;
}
2016-02-01 10:49:54 +00:00
var name = getServerNameByIp(entry.ip);
if (!graphData[name]) {
graphData[name] = [];
2015-12-18 07:45:38 +00:00
}
2016-02-01 10:49:54 +00:00
graphData[name].push([entry.timestamp, entry.playerCount]);
2015-12-18 07:45:38 +00:00
}
// Break it into minutes.
trimUselessPings(graphData);
2015-12-19 03:23:24 +00:00
// Drop old data.
exports.trimOldPings(graphData);
2016-02-01 11:44:56 +00:00
logger.info('Converted data structure in ' + (exports.getCurrentTimeMs() - startTime) + 'ms');
2016-02-01 11:43:23 +00:00
2015-12-18 07:45:38 +00:00
return graphData;
};
exports.getBootTime = function() {
if (!bootTime) {
bootTime = exports.getCurrentTimeMs();
logger.info('Selected %d as boot time.', bootTime);
}
return bootTime;
2015-11-09 00:34:17 +00:00
};