2019-09-27 19:25:47 +02:00
|
|
|
import asyncio
|
|
|
|
import atexit
|
|
|
|
import datetime
|
2019-09-26 17:47:25 +02:00
|
|
|
import socket
|
2019-09-27 19:25:47 +02:00
|
|
|
from functools import lru_cache
|
|
|
|
from time import sleep, time
|
2019-09-20 14:15:20 +02:00
|
|
|
|
2019-09-27 19:25:47 +02:00
|
|
|
import matplotlib.pyplot as plt
|
2019-09-20 23:50:13 +02:00
|
|
|
import socketio
|
2019-09-27 19:25:47 +02:00
|
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
2019-09-26 17:47:25 +02:00
|
|
|
from flask import Flask, render_template, request
|
2019-09-27 15:20:10 +02:00
|
|
|
from flask_cors import CORS
|
2019-09-20 14:47:55 +02:00
|
|
|
|
2019-09-20 23:50:13 +02:00
|
|
|
sio = socketio.Server()
|
2019-09-20 14:15:20 +02:00
|
|
|
app = Flask(__name__)
|
2019-10-02 11:29:01 +02:00
|
|
|
CORS(app)
|
2019-09-20 23:50:13 +02:00
|
|
|
app.wsgi_app = socketio.WSGIApp(sio, app.wsgi_app)
|
2019-09-20 14:15:20 +02:00
|
|
|
|
|
|
|
# We request users to send the run number to prevent accidentially getting old requests from a previous run
|
|
|
|
# This maybe will give to much errors from people forgetting to change their number
|
|
|
|
# Remove it?
|
|
|
|
|
|
|
|
# TODO
|
|
|
|
# Save and read from a file to persist over reboots
|
|
|
|
# Save progress through time during 1 run
|
|
|
|
# Save result of different runs
|
|
|
|
|
2019-09-22 03:11:19 +02:00
|
|
|
db = {"current_run": None, "run_start_timer": None, "run_data": {}}
|
2019-09-20 14:47:55 +02:00
|
|
|
|
2019-09-27 19:25:47 +02:00
|
|
|
function_times = []
|
|
|
|
function_data = []
|
|
|
|
|
2019-09-20 14:15:20 +02:00
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
def index():
|
2019-09-20 14:47:55 +02:00
|
|
|
return render_template('index.html')
|
|
|
|
|
2019-09-20 14:15:20 +02:00
|
|
|
|
|
|
|
@app.route("/start_run/<run_index>")
|
|
|
|
def start_run(run_index):
|
2019-10-02 13:22:51 +02:00
|
|
|
request_data = live_request(f'/start_run/{run_index}') #make request show up on the live requests
|
|
|
|
|
2019-10-02 11:29:01 +02:00
|
|
|
if not run_index.isdigit():
|
|
|
|
return f'{run_index} is not a number'
|
|
|
|
|
2019-09-20 14:15:20 +02:00
|
|
|
db["current_run"] = run_index
|
2019-09-22 03:11:19 +02:00
|
|
|
starttime = time()
|
2019-09-20 23:50:13 +02:00
|
|
|
if run_index in db["run_data"]:
|
|
|
|
return "This run is already ran, take another number."
|
2019-09-20 14:47:55 +02:00
|
|
|
db["run_data"][run_index] = {"starttime": starttime, "data": {}}
|
2019-09-20 14:15:20 +02:00
|
|
|
# TODO send start request to the first person in the chain. Probably a zeus part already written as example
|
2019-09-20 23:50:13 +02:00
|
|
|
sio.emit('start_run', run_index)
|
2019-09-20 14:15:20 +02:00
|
|
|
return f'Run {run_index} started at {starttime}'
|
|
|
|
|
2019-09-20 14:47:55 +02:00
|
|
|
|
2019-09-27 17:43:33 +02:00
|
|
|
@app.route("/link/start/<run>/<link_index>")
|
|
|
|
def link_start(run, link_index):
|
2019-10-02 13:22:51 +02:00
|
|
|
request_data = live_request(f'/link/start/{run}/{link_index}') #make request show up on the live requests
|
2019-10-02 13:12:08 +02:00
|
|
|
|
2019-10-02 11:29:01 +02:00
|
|
|
if not run.isdigit() or not link_index.isdigit():
|
|
|
|
return f'{run} and/or {link_index} is not a number'
|
|
|
|
|
2019-09-27 19:25:47 +02:00
|
|
|
start = time()
|
2019-10-02 13:12:08 +02:00
|
|
|
|
2019-09-26 17:47:25 +02:00
|
|
|
|
2019-09-20 14:15:20 +02:00
|
|
|
if db["current_run"] != run:
|
2019-09-27 19:25:47 +02:00
|
|
|
response = "Wrong run number, check that you update your run", 404
|
2019-09-20 14:15:20 +02:00
|
|
|
else:
|
2019-09-20 23:50:13 +02:00
|
|
|
run_data = db["run_data"][run]["data"]
|
2019-09-27 17:43:33 +02:00
|
|
|
if link_index in run_data:
|
2019-09-27 19:25:47 +02:00
|
|
|
response = "you already started in this run. Ignoring this request."
|
2019-09-20 17:00:44 +02:00
|
|
|
else:
|
2019-09-27 17:43:33 +02:00
|
|
|
run_data[link_index] = {"id": link_index, "start": time()}
|
|
|
|
sio.emit('link_start', run_data[link_index])
|
2019-09-27 19:25:47 +02:00
|
|
|
response = "Success."
|
|
|
|
stop = time()
|
|
|
|
function_data.append(stop - start)
|
|
|
|
function_times.append(len(function_times))
|
|
|
|
return response
|
2019-09-20 14:15:20 +02:00
|
|
|
|
|
|
|
|
2019-09-20 14:55:11 +02:00
|
|
|
@app.route("/link/handoff/<run>/<index>")
|
|
|
|
def link_handoff(run, index):
|
2019-10-02 13:22:51 +02:00
|
|
|
request_data = live_request(f'/link/handoff/{run}/{link_index}') #make request show up on the live requests
|
|
|
|
|
2019-10-02 11:29:01 +02:00
|
|
|
if not run.isdigit() or not index.isdigit():
|
|
|
|
return f'{run} and/or {index} is not a number'
|
|
|
|
|
2019-09-20 14:15:20 +02:00
|
|
|
if db["current_run"] != run:
|
2019-09-22 03:11:19 +02:00
|
|
|
return "Wrong run number, check that you updated you run", 404
|
2019-09-20 14:15:20 +02:00
|
|
|
else:
|
2019-09-20 17:00:44 +02:00
|
|
|
link_data = db["run_data"][run]["data"][index]
|
2019-09-22 03:11:19 +02:00
|
|
|
if "handoff" in link_data:
|
2019-09-20 17:00:44 +02:00
|
|
|
return "you already handed off control during this run. Ignoring this request"
|
|
|
|
else:
|
2019-09-22 03:11:19 +02:00
|
|
|
link_data["handoff"] = time()
|
|
|
|
sio.emit('link_handoff', link_data)
|
2019-09-20 17:00:44 +02:00
|
|
|
return "Success."
|
2019-09-20 14:15:20 +02:00
|
|
|
|
2019-09-20 14:47:55 +02:00
|
|
|
|
2019-09-22 03:11:19 +02:00
|
|
|
@sio.event
|
|
|
|
def connect(sid, data):
|
|
|
|
if db["current_run"]:
|
|
|
|
current_run = db["run_data"][db["current_run"]]
|
|
|
|
sio.emit('sync_current_run', current_run, room=sid)
|
|
|
|
|
|
|
|
|
2019-10-02 13:22:51 +02:00
|
|
|
def live_request(route_data):
|
2019-09-23 18:21:28 +02:00
|
|
|
ip = request.remote_addr
|
|
|
|
request_data = {}
|
2019-09-27 19:25:47 +02:00
|
|
|
request_data["hostname"] = lookup_hostname(ip)
|
2019-09-23 18:21:28 +02:00
|
|
|
request_data["time"] = time()
|
2019-10-02 13:12:08 +02:00
|
|
|
request_data["route"] = route_data
|
|
|
|
|
|
|
|
sio.emit('live_request', request_data)
|
2019-09-23 18:21:28 +02:00
|
|
|
|
2019-09-22 03:11:19 +02:00
|
|
|
|
2019-09-27 19:25:47 +02:00
|
|
|
@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
|
|
|
|
|
2019-09-20 14:47:55 +02:00
|
|
|
if __name__ == '__main__':
|
2019-09-27 19:25:47 +02:00
|
|
|
# scheduler = BackgroundScheduler()
|
|
|
|
# scheduler.add_job(func=plot_graph, trigger="interval", seconds=5)
|
|
|
|
# scheduler.start()
|
2019-09-20 23:50:13 +02:00
|
|
|
app.run(host="0.0.0.0", debug=True)
|
2019-09-27 19:25:47 +02:00
|
|
|
|
|
|
|
# shut down the scheduler when exiting the app
|
|
|
|
# atexit.register(lambda: scheduler.shutdown())
|