diff --git a/app/app.py b/app/app.py index 468fbd9..65bca6b 100644 --- a/app/app.py +++ b/app/app.py @@ -2,25 +2,24 @@ import mcstatus, yaml, time, threading from bottle import route, run, template, static_file, error +last_data = None data = {} json_response = None -with open('config.yml', 'r') as cfg_file: - servers_config = yaml.load(cfg_file) +def update_config(): + with open('config.yml', 'r') as cfg_file: + servers_config = yaml.load(cfg_file) -# c = 0.0 - -for category in servers_config: - print category - data[category] = {} - for server in servers_config[category]: - print "- " + server + ": " + servers_config[category][server] - ip = servers_config[category][server] - if "/" not in ip: - ip += "/25565" - status = mcstatus.McServer(ip.split("/")[0], ip.split("/")[1]) - # c += 1 - data[category][server] = status + for category in servers_config: + print category + data[category] = {} + for server in servers_config[category]: + print "- " + server + ": " + servers_config[category][server] + ip = servers_config[category][server] + if "/" not in ip: + ip += "/25565" + status = mcstatus.McServer(ip.split("/")[0], ip.split("/")[1]) + data[category][server] = status def update_all(): # i = 0.0 @@ -65,13 +64,23 @@ def schedule_update(): def schedule_json(): threading.Timer(1.5, schedule_json).start() + global last_data + last_data = json_response global json_response json_response = generate_json() +def schedule_config(): + threading.Timer(600, schedule_config).start() + update_config() + @route('/status') def index(): return json_response +@route('/last') +def index(): + return last_data + @route('/') def server_static(): return static_file('index.html', '..') @@ -84,10 +93,11 @@ def error404(error): def server_static(filename): return static_file(filename, root = '..') +schedule_config() schedule_update() schedule_json() try: - run(host='localhost', port=8080) + run(host='127.0.0.1', port=8288) except KeyboardInterrupt: sys.exit(0) diff --git a/populate.js b/populate.js index 43fde28..ddccf61 100644 --- a/populate.js +++ b/populate.js @@ -1,7 +1,57 @@ +var last_data = null; + +var chances = {}; + function populate() { - $.getJSON('status?' + Math.floor(Math.random()*30000), function (data) { + $.getJSON('status?' + Math.floor(Math.random() * 30000), function(data) { var x = ""; + for (var chancer in chances) { + strings = chancer.split("_-_-_"); + console.log( "Strings:" ); + console.log( strings ); + if( strings.length == 2 && strings[0] !== undefined && strings[1] !== undefined ) { + if( data.dead[strings[0]] !== undefined && $.inArray( strings[1], data.dead[strings[0]] ) > -1 ) { + + if( chances[ chancer ] == 5 ) { + var notification = new Notification('A server on is down!', { + //icon: 'http://path.to.com/image.png', + body: "Hey! " + strings[1] + " is down on the " + strings[0] + " machine!", + }); + } + if( (chances[ chancer ] % 100) == 0 ) { + var notification = new Notification('It\'s been a while!!', { + //icon: 'http://path.to.com/image.png', + body: "Hey! " + strings[1] + " is down on the " + strings[0] + " machine!", + }); + } + + chances[ chancer ] = chances[ chancer ] + 1; + console.log( "Chances add one for " + chancer + ".\nTotal: " + chances[ chancer ] ); + } else { + console.log( "Not B." ); + delete chances[ chancer ] + } + } else { + console.log( "Not A" ); + delete chances[ chancer ]; + } + } + + if (last_data != null) { + for (var category in last_data.alive) { + for (var entry in last_data.alive[category]) { + if (data['dead'][category] !== undefined && $.inArray(entry, data['dead'][category]) > -1) { + console.log("dead: " + category + " - " + entry); + if( chances[ category + "_-_-_" + entry ] == undefined ) + chances[ category + "_-_-_" + entry ] = 1; + } + } + } + + } + last_data = data; + for (var category in data.alive) { x += "
"; @@ -32,7 +82,10 @@ function populate() { }); } -$(document).ready(function () { - window.setInterval(populate, 3000); +$(document).ready(function() { + if (Notification.permission !== "granted") + Notification.requestPermission(); + + window.setInterval(populate, 5000); populate(); });