Allow custom Graph colors (#76)

This commit is contained in:
Hugo Manrique 2017-04-03 23:10:46 -04:00 committed by Cryptkeeper!
parent 42c744b9dd
commit 0955e8dbcd
6 changed files with 37 additions and 6 deletions

2
.gitignore vendored

@ -5,4 +5,4 @@ production/
database.sql database.sql
database.sql-journal database.sql-journal
.DS_Store .DS_Store
yarn.lock

5
app.js

@ -26,6 +26,11 @@ function pingAll() {
for (var i = 0; i < servers.length; i++) { for (var i = 0; i < servers.length; i++) {
// Make sure we lock our scope. // Make sure we lock our scope.
(function(network) { (function(network) {
// Asign auto generated color if not present
if (!network.color) {
network.color = util.stringToColor(network.name);
}
var attemptedVersion = config.versions[network.type][currentVersionIndex[network.type]]; var attemptedVersion = config.versions[network.type][currentVersionIndex[network.type]];
ping.ping(network.ip, network.port, network.type, config.rates.connectTimeout, function(err, res) { ping.ping(network.ip, network.port, network.type, config.rates.connectTimeout, function(err, res) {
// Handle our ping results, if it succeeded. // Handle our ping results, if it succeeded.

@ -117,12 +117,12 @@ function convertGraphData(rawData) {
var keys = Object.keys(rawData); var keys = Object.keys(rawData);
for (var i = 0; i < keys.length; i++) { for (var i = 0; i < keys.length; i++) {
data.push({ data.push({
data: rawData[keys[i]], data: rawData[keys[i]],
yaxis: 1, yaxis: 1,
label: keys[i], label: keys[i],
color: stringToColor(keys[i]) color: getServerColor(keys[i])
}); });
} }

@ -151,7 +151,7 @@ function updatePercentageBar() {
id: 'perc_bar_part_' + safeNameCopy, id: 'perc_bar_part_' + safeNameCopy,
class: 'perc-bar-part', class: 'perc-bar-part',
html: '', html: '',
style: 'background: ' + stringToColor(server) + ';' style: 'background: ' + getServerColor(server) + ';'
}).appendTo(parent); }).appendTo(parent);
div = $('#perc_bar_part_' + safeNameCopy); div = $('#perc_bar_part_' + safeNameCopy);

@ -76,16 +76,30 @@ function getServersByCategory() {
return byCategory; return byCategory;
} }
function getServerByIp(ip) { function getServerByField(id, value) {
for (var i = 0; i < publicConfig.servers.length; i++) { for (var i = 0; i < publicConfig.servers.length; i++) {
var entry = publicConfig.servers[i]; var entry = publicConfig.servers[i];
if (entry.ip === ip) { if (entry[id] === value) {
return entry; return entry;
} }
} }
} }
function getServerByIp(ip) {
return getServerByField('ip', ip);
}
function getServerByName(name) {
return getServerByField('name', name);
}
function getServerColor(name) {
var server = getServerByName(name);
return server ? server.color : stringToColor(name);
}
// Generate (and set) the HTML that displays Mojang status. // Generate (and set) the HTML that displays Mojang status.
// If nothing is passed, re-render the last update. // If nothing is passed, re-render the last update.
// If something is passed, update and then re-render. // If something is passed, update and then re-render.

@ -100,6 +100,18 @@ exports.getCurrentTimeMs = function() {
return new Date().getTime(); return new Date().getTime();
}; };
exports.stringToColor = function(base) {
var hash;
for (var i = base.length - 1, hash = 0; i >= 0; i--) {
hash = base.charCodeAt(i) + ((hash << 5) - hash);
}
color = Math.floor(Math.abs((Math.sin(hash) * 10000) % 1 * 16777216)).toString(16);
return '#' + Array(6 - color.length + 1).join('0') + color;
}
exports.setIntervalNoDelay = function(func, delay) { exports.setIntervalNoDelay = function(func, delay) {
var task = setInterval(func, delay); var task = setInterval(func, delay);