From 01a11263ce51be52fcb3091a7a1d443c8d2b73a7 Mon Sep 17 00:00:00 2001 From: Cryptkeeper Date: Mon, 1 Feb 2016 04:49:54 -0600 Subject: [PATCH] Use names instead of IPs --- lib/util.js | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/util.js b/lib/util.js index 0e9914b..2f06c02 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,6 +1,10 @@ +var logger = require('./logger'); + var config = require('../config.json'); var servers = require('../servers.json'); +var serverNameLookup = {}; + // Checks if we have a server in config.json with the IP. function serverWithIpExists(ip) { for (var i = 0; i < servers.length; i++) { @@ -14,6 +18,24 @@ function serverWithIpExists(ip) { return false; } +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) { + lookupName[entry.ip] = entry.name; + + return entry.name; + } + } +} + // 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. @@ -103,11 +125,19 @@ exports.convertPingsToGraph = function(sqlData) { for (var i = 0; i < sqlData.length; i++) { var entry = sqlData[i]; - if (!graphData[entry.ip]) { - graphData[entry.ip] = []; + var name = getServerNameByIp(entry.ip); + + if (!name) { + logger.warn('Missing name for ' + entry.ip); + + continue; } - graphData[entry.ip].push([entry.timestamp, entry.playerCount]); + if (!graphData[name]) { + graphData[name] = []; + } + + graphData[name].push([entry.timestamp, entry.playerCount]); } // Break it into minutes.