From 44c48730c0204e6300fa26a2e9e4caeb1be8e36a Mon Sep 17 00:00:00 2001 From: Hannes Klinckaert Date: Mon, 23 Sep 2019 18:21:28 +0200 Subject: [PATCH 1/4] start implementing live request tracking --- watcher/app.py | 11 +++++++++++ watcher/static/index.js | 23 +++++++++++++++++++++++ watcher/templates/index.html | 19 +++++++++++++++++-- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/watcher/app.py b/watcher/app.py index ee3538a..cf44d65 100644 --- a/watcher/app.py +++ b/watcher/app.py @@ -1,7 +1,9 @@ from time import time import socketio +import socket from flask import Flask, render_template +from flask import request sio = socketio.Server() app = Flask(__name__) @@ -38,6 +40,7 @@ def start_run(run_index): @app.route("/link/start//") def link_start(run, index): + live_request(run, index) if db["current_run"] != run: return "Wrong run number, check that you update your run", 404 else: @@ -70,6 +73,14 @@ def connect(sid, data): current_run = db["run_data"][db["current_run"]] sio.emit('sync_current_run', current_run, room=sid) +def live_request(run, index): + ip = request.remote_addr + request_data = {} + request_data["hostname"] = socket.gethostbyaddr(ip) + request_data["time"] = time() + + sio.emit('live_request', request_data) + if __name__ == '__main__': app.run(host="0.0.0.0", debug=True) diff --git a/watcher/static/index.js b/watcher/static/index.js index ce44fc9..3b5e5de 100644 --- a/watcher/static/index.js +++ b/watcher/static/index.js @@ -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) { + request_time = new Date(request_data["time"] * 1000); + addLiveRequest(request_data["hostname"], request_time); + }); }); + function addLiveRequest(request_host, request_time) { + alert("yes"); + let hostname = request_host; + let timesting = request_time.getHours() + ":" + request_time.getMinutes(); + + let html = ` +
+
+ ${hostname} +
+
+ ${timesting} +
+
+ `; + $('#live_requests').prepend($(html)); + + } + function addLink(link_id, link_data) { let stop = "handoff" in link_data; diff --git a/watcher/templates/index.html b/watcher/templates/index.html index 5c2c47b..cfd49cc 100644 --- a/watcher/templates/index.html +++ b/watcher/templates/index.html @@ -64,11 +64,26 @@ -
-
live requests
+
+
+
+ Hostname +
+
+ Time +
+
Live Requests
+
+
+ Hostname +
+
+ +
+
From b4fd3f97102f1e451eaea17928bf8bb3d1b22f60 Mon Sep 17 00:00:00 2001 From: Maxime Bloch Date: Thu, 26 Sep 2019 17:47:25 +0200 Subject: [PATCH 2/4] fix socket request for live requests, not sure why emit doesnt work there --- watcher/app.py | 13 +++++++------ watcher/static/index.js | 1 + 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/watcher/app.py b/watcher/app.py index cf44d65..435795f 100644 --- a/watcher/app.py +++ b/watcher/app.py @@ -1,9 +1,8 @@ +import socket from time import time import socketio -import socket -from flask import Flask, render_template -from flask import request +from flask import Flask, render_template, request sio = socketio.Server() app = Flask(__name__) @@ -40,7 +39,9 @@ def start_run(run_index): @app.route("/link/start//") def link_start(run, index): - live_request(run, index) + request_data = live_request(run, index) + sio.emit('live_request', request_data) + if db["current_run"] != run: return "Wrong run number, check that you update your run", 404 else: @@ -73,13 +74,13 @@ def connect(sid, data): current_run = db["run_data"][db["current_run"]] sio.emit('sync_current_run', current_run, room=sid) + def live_request(run, index): ip = request.remote_addr request_data = {} request_data["hostname"] = socket.gethostbyaddr(ip) request_data["time"] = time() - - sio.emit('live_request', request_data) + return request_data if __name__ == '__main__': diff --git a/watcher/static/index.js b/watcher/static/index.js index 3b5e5de..6c6f7ce 100644 --- a/watcher/static/index.js +++ b/watcher/static/index.js @@ -44,6 +44,7 @@ require(['jquery', 'socket.io'], function(jq, io) { } }); 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); }); From c6e93f8c7fc3633b15edf2afc8ab9455aa94ce42 Mon Sep 17 00:00:00 2001 From: Maxime Bloch Date: Fri, 27 Sep 2019 17:43:33 +0200 Subject: [PATCH 3/4] fix socket message not sending --- watcher/app.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/watcher/app.py b/watcher/app.py index 435795f..93d3625 100644 --- a/watcher/app.py +++ b/watcher/app.py @@ -37,20 +37,20 @@ def start_run(run_index): return f'Run {run_index} started at {starttime}' -@app.route("/link/start//") -def link_start(run, index): - request_data = live_request(run, index) +@app.route("/link/start//") +def link_start(run, link_index): + 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 else: run_data = db["run_data"][run]["data"] - if index in run_data: + if link_index in run_data: return "you already started in this run. Ignoring this request." else: - run_data[index] = {"id": index, "start": time()} - sio.emit('link_start', run_data[index]) + run_data[link_index] = {"id": link_index, "start": time()} + sio.emit('link_start', run_data[link_index]) return "Success." @@ -78,7 +78,7 @@ def connect(sid, data): def live_request(run, index): ip = request.remote_addr request_data = {} - request_data["hostname"] = socket.gethostbyaddr(ip) + request_data["hostname"] = socket.gethostbyaddr(ip)[0] request_data["time"] = time() return request_data From 35bb6dd5fc81327b5a8741ae98b1d263b3b8d37d Mon Sep 17 00:00:00 2001 From: Maxime Bloch Date: Fri, 27 Sep 2019 19:25:47 +0200 Subject: [PATCH 4/4] add profiling code to test lru cache, remove js popup --- watcher/app.py | 45 +++++++++++++++++++++++++++++++++++----- watcher/requirements.txt | 2 ++ watcher/static/index.js | 1 - 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/watcher/app.py b/watcher/app.py index 93d3625..59a444a 100644 --- a/watcher/app.py +++ b/watcher/app.py @@ -1,7 +1,13 @@ +import asyncio +import atexit +import datetime import socket -from time import time +from functools import lru_cache +from time import sleep, time +import matplotlib.pyplot as plt import socketio +from apscheduler.schedulers.background import BackgroundScheduler from flask import Flask, render_template, request sio = socketio.Server() @@ -19,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(): @@ -39,19 +48,24 @@ def start_run(run_index): @app.route("/link/start//") 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 link_index in run_data: - return "you already started in this run. Ignoring this request." + response = "you already started in this run. Ignoring this request." else: run_data[link_index] = {"id": link_index, "start": time()} sio.emit('link_start', run_data[link_index]) - return "Success." + response = "Success." + stop = time() + function_data.append(stop - start) + function_times.append(len(function_times)) + return response @app.route("/link/handoff//") @@ -78,10 +92,31 @@ def connect(sid, data): def live_request(run, index): ip = request.remote_addr request_data = {} - request_data["hostname"] = socket.gethostbyaddr(ip)[0] + 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()) diff --git a/watcher/requirements.txt b/watcher/requirements.txt index 23c5fdc..fb7f3f8 100644 --- a/watcher/requirements.txt +++ b/watcher/requirements.txt @@ -1,2 +1,4 @@ flask python-socketio +apscheduler +matplotlib diff --git a/watcher/static/index.js b/watcher/static/index.js index 6c6f7ce..37bc8dd 100644 --- a/watcher/static/index.js +++ b/watcher/static/index.js @@ -51,7 +51,6 @@ require(['jquery', 'socket.io'], function(jq, io) { }); function addLiveRequest(request_host, request_time) { - alert("yes"); let hostname = request_host; let timesting = request_time.getHours() + ":" + request_time.getMinutes();