Compare commits

...

4 Commits

Author SHA1 Message Date
Maxime Bloch 35bb6dd5fc
add profiling code to test lru cache, remove js popup 2019-09-27 19:25:47 +02:00
Maxime Bloch c6e93f8c7f
fix socket message not sending 2019-09-27 17:43:33 +02:00
Maxime Bloch b4fd3f9710
fix socket request for live requests, not sure why emit doesnt work there 2019-09-26 17:47:25 +02:00
Hannes Klinckaert 44c48730c0 start implementing live request tracking 2019-09-23 18:21:28 +02:00
4 changed files with 99 additions and 12 deletions

View File

@ -1,7 +1,14 @@
from time import time
import asyncio
import atexit
import datetime
import socket
from functools import lru_cache
from time import sleep, time
import matplotlib.pyplot as plt
import socketio
from flask import Flask, render_template
from apscheduler.schedulers.background import BackgroundScheduler
from flask import Flask, render_template, request
sio = socketio.Server()
app = Flask(__name__)
@ -18,6 +25,9 @@ app.wsgi_app = socketio.WSGIApp(sio, app.wsgi_app)
db = {"current_run": None, "run_start_timer": None, "run_data": {}}
function_times = []
function_data = []
@app.route("/")
def index():
@ -36,18 +46,26 @@ def start_run(run_index):
return f'Run {run_index} started at {starttime}'
@app.route("/link/start/<run>/<index>")
def link_start(run, index):
@app.route("/link/start/<run>/<link_index>")
def link_start(run, link_index):
start = time()
request_data = live_request(run, link_index)
sio.emit('live_request', request_data)
if db["current_run"] != run:
return "Wrong run number, check that you update your run", 404
response = "Wrong run number, check that you update your run", 404
else:
run_data = db["run_data"][run]["data"]
if index in run_data:
return "you already started in this run. Ignoring this request."
if link_index in run_data:
response = "you already started in this run. Ignoring this request."
else:
run_data[index] = {"id": index, "start": time()}
sio.emit('link_start', run_data[index])
return "Success."
run_data[link_index] = {"id": link_index, "start": time()}
sio.emit('link_start', run_data[link_index])
response = "Success."
stop = time()
function_data.append(stop - start)
function_times.append(len(function_times))
return response
@app.route("/link/handoff/<run>/<index>")
@ -71,5 +89,34 @@ def connect(sid, data):
sio.emit('sync_current_run', current_run, room=sid)
def live_request(run, index):
ip = request.remote_addr
request_data = {}
request_data["hostname"] = lookup_hostname(ip)
request_data["time"] = time()
return request_data
@lru_cache()
def lookup_hostname(ip):
return socket.gethostbyaddr(ip)[0].split(".")[0]
# def plot_graph():
# try:
# x = function_times
# y = function_data
# plt.plot(x, y)
# plt.show()
# except:
# pass
if __name__ == '__main__':
# scheduler = BackgroundScheduler()
# scheduler.add_job(func=plot_graph, trigger="interval", seconds=5)
# scheduler.start()
app.run(host="0.0.0.0", debug=True)
# shut down the scheduler when exiting the app
# atexit.register(lambda: scheduler.shutdown())

View File

@ -1,2 +1,4 @@
flask
python-socketio
apscheduler
matplotlib

View File

@ -43,8 +43,31 @@ require(['jquery', 'socket.io'], function(jq, io) {
addLink(link_id, run_data["data"][link_id]);
}
});
socket.on('live_request', function(request_data) {
console.log("live request");
request_time = new Date(request_data["time"] * 1000);
addLiveRequest(request_data["hostname"], request_time);
});
});
function addLiveRequest(request_host, request_time) {
let hostname = request_host;
let timesting = request_time.getHours() + ":" + request_time.getMinutes();
let html = `
<div class="item">
<div>
${hostname}
</div>
<div>
${timesting}
</div>
</div>
`;
$('#live_requests').prepend($(html));
}
function addLink(link_id, link_data) {
let stop = "handoff" in link_data;

View File

@ -64,11 +64,26 @@
</div>
</div>
<div class="card">
<div class="item">live requests</div>
<div id="live_requests" class="card">
<div class="item">
<div>
Hostname
</div>
<div>
Time
</div>
</div>
<div class="card__title">
Live Requests
</div>
<div class="item">
<div>
Hostname
</div>
<div>
</div>
</div>
</div>
</div>
</body>