Skip to content

Commit

Permalink
Allow custom Graph colors (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugmanrique authored and Cryptkeeper! committed Apr 4, 2017
1 parent 42c744b commit 0955e8d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -5,4 +5,4 @@ production/
database.sql
database.sql-journal
.DS_Store

yarn.lock
5 changes: 5 additions & 0 deletions app.js
Expand Up @@ -26,6 +26,11 @@ function pingAll() {
for (var i = 0; i < servers.length; i++) {
// Make sure we lock our scope.
(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]];
ping.ping(network.ip, network.port, network.type, config.rates.connectTimeout, function(err, res) {
// Handle our ping results, if it succeeded.
Expand Down
4 changes: 2 additions & 2 deletions assets/js/graph.js
Expand Up @@ -117,12 +117,12 @@ function convertGraphData(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: rawData[keys[i]],
yaxis: 1,
label: keys[i],
color: stringToColor(keys[i])
color: getServerColor(keys[i])
});
}

Expand Down
2 changes: 1 addition & 1 deletion assets/js/site.js
Expand Up @@ -151,7 +151,7 @@ function updatePercentageBar() {
id: 'perc_bar_part_' + safeNameCopy,
class: 'perc-bar-part',
html: '',
style: 'background: ' + stringToColor(server) + ';'
style: 'background: ' + getServerColor(server) + ';'
}).appendTo(parent);

div = $('#perc_bar_part_' + safeNameCopy);
Expand Down
18 changes: 16 additions & 2 deletions assets/js/util.js
Expand Up @@ -76,16 +76,30 @@ function getServersByCategory() {
return byCategory;
}

function getServerByIp(ip) {
function getServerByField(id, value) {
for (var i = 0; i < publicConfig.servers.length; i++) {
var entry = publicConfig.servers[i];

if (entry.ip === ip) {
if (entry[id] === value) {
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.
// If nothing is passed, re-render the last update.
// If something is passed, update and then re-render.
Expand Down
12 changes: 12 additions & 0 deletions lib/util.js
Expand Up @@ -100,6 +100,18 @@ exports.getCurrentTimeMs = function() {
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) {
var task = setInterval(func, delay);

Expand Down

0 comments on commit 0955e8d

Please sign in to comment.