Use a basic time tracker instead of Date manipulation
This commit is contained in:
parent
fcff7a0fec
commit
3373ebc9ee
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
*.log
|
*.log
|
||||||
|
.idea/
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
var request = require('request');
|
var request = require('request');
|
||||||
|
|
||||||
var logger = require('./logger');
|
var logger = require('./logger');
|
||||||
|
var profiler = require('./profiler');
|
||||||
|
|
||||||
var serviceNameLookup = {
|
var serviceNameLookup = {
|
||||||
'minecraft.net': 'Website',
|
'minecraft.net': 'Website',
|
||||||
@ -24,7 +25,7 @@ function updateService(name, status) {
|
|||||||
|
|
||||||
// If it's an outage, track when it started.
|
// If it's an outage, track when it started.
|
||||||
if (status === 'yellow'|| status === 'red') {
|
if (status === 'yellow'|| status === 'red') {
|
||||||
newEntry.startTime = (new Date).getTime();
|
newEntry.startTime = profiler.getCurrentTimeMs();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate a nice title from the color.
|
// Generate a nice title from the color.
|
||||||
@ -55,15 +56,15 @@ exports.update = function(timeout) {
|
|||||||
try {
|
try {
|
||||||
body = JSON.parse(body);
|
body = JSON.parse(body);
|
||||||
|
|
||||||
for (var i = 0; i < body.length; i++) {
|
for (var i = 0; i < body.length; i++) {
|
||||||
var service = body[i];
|
var service = body[i];
|
||||||
var name = Object.keys(service)[0]; // Because they return an array of object, we have to do this :(
|
var name = Object.keys(service)[0]; // Because they return an array of object, we have to do this :(
|
||||||
|
|
||||||
// If it's not in the lookup, we don't care about it.
|
// If it's not in the lookup, we don't care about it.
|
||||||
if (name in serviceNameLookup) {
|
if (name in serviceNameLookup) {
|
||||||
updateService(name, service[name]);
|
updateService(name, service[name]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.log('debug', 'Updated Mojang services: %s', JSON.stringify(serviceStates));
|
logger.log('debug', 'Updated Mojang services: %s', JSON.stringify(serviceStates));
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
|
10
lib/ping.js
10
lib/ping.js
@ -1,9 +1,11 @@
|
|||||||
var mcpe_ping = require('mcpe-ping');
|
var mcpe_ping = require('mcpe-ping');
|
||||||
var mcpc_ping = require('mc-ping-updated');
|
var mcpc_ping = require('mc-ping-updated');
|
||||||
|
|
||||||
|
var profiler = require('./profiler');
|
||||||
|
|
||||||
// This is a wrapper function for mc-ping-updated, mainly used to convert the data structure of the result.
|
// This is a wrapper function for mc-ping-updated, mainly used to convert the data structure of the result.
|
||||||
function pingMinecraftPC(host, port, timeout, callback) {
|
function pingMinecraftPC(host, port, timeout, callback) {
|
||||||
var milliseconds = (new Date).getTime();
|
profiler.track(host);
|
||||||
|
|
||||||
// Try catch incase the down stream module is bad at handling exceptions.
|
// Try catch incase the down stream module is bad at handling exceptions.
|
||||||
try {
|
try {
|
||||||
@ -18,7 +20,7 @@ function pingMinecraftPC(host, port, timeout, callback) {
|
|||||||
max: res.players.max
|
max: res.players.max
|
||||||
},
|
},
|
||||||
version: res.version.protocol,
|
version: res.version.protocol,
|
||||||
latency: (new Date).getTime() - milliseconds
|
latency: profiler.untrack(host)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, timeout);
|
}, timeout);
|
||||||
@ -29,7 +31,7 @@ function pingMinecraftPC(host, port, timeout, callback) {
|
|||||||
|
|
||||||
// This is a wrapper function for mcpe-ping, mainly used to convert the data structure of the result.
|
// This is a wrapper function for mcpe-ping, mainly used to convert the data structure of the result.
|
||||||
function pingMinecraftPE(host, port, timeout, callback) {
|
function pingMinecraftPE(host, port, timeout, callback) {
|
||||||
var milliseconds = (new Date).getTime();
|
profiler.track(host);
|
||||||
|
|
||||||
// Try catch incase the down stream module is bad at handling exceptions.
|
// Try catch incase the down stream module is bad at handling exceptions.
|
||||||
try {
|
try {
|
||||||
@ -44,7 +46,7 @@ function pingMinecraftPE(host, port, timeout, callback) {
|
|||||||
max: res.maxPlayers
|
max: res.maxPlayers
|
||||||
},
|
},
|
||||||
version: res.version,
|
version: res.version,
|
||||||
latency: (new Date).getTime() - milliseconds
|
latency: profiler.untrack(host)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, timeout);
|
}, timeout);
|
||||||
|
31
lib/profiler.js
Normal file
31
lib/profiler.js
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
var logger = require('./logger');
|
||||||
|
|
||||||
|
var timestamps = {};
|
||||||
|
|
||||||
|
function getCurrentTimeMs() {
|
||||||
|
return (new Date).getTime();
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.track = function(name) {
|
||||||
|
if (timestamps[name]) {
|
||||||
|
throw new Error(name + ' is already being profiled!');
|
||||||
|
}
|
||||||
|
|
||||||
|
timestamps[name] = getCurrentTimeMs();
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.untrack = function(name) {
|
||||||
|
if (!timestamps[name]) {
|
||||||
|
throw new Error(name + ' isn\'t being profiled!');
|
||||||
|
}
|
||||||
|
|
||||||
|
var timestamp = getCurrentTimeMs() - timestamps[name];
|
||||||
|
|
||||||
|
delete timestamps[name];
|
||||||
|
|
||||||
|
logger.log('debug', name + ' took ' + timestamp + 'ms');
|
||||||
|
|
||||||
|
return timestamp;
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.getCurrentTimeMs = getCurrentTimeMs();
|
Loading…
Reference in New Issue
Block a user