add persistency, update server url in client and server js

This commit is contained in:
Maxime Bloch 2019-10-08 18:14:07 +02:00
parent 468861d36f
commit 3dea109886
No known key found for this signature in database
GPG key ID: CE32A7D95B7D6418
7 changed files with 65 additions and 35 deletions

View file

@ -47,7 +47,7 @@ A local http file server of some kind.
Go into the correct directory with the code Go into the correct directory with the code
cd watcher cd client
with python's http server with python's http server

4
client/handoff.sh Normal file → Executable file
View file

@ -3,4 +3,6 @@
RUN_NUMBER=0 RUN_NUMBER=0
LINK_NUMBER=0 LINK_NUMBER=0
curl http://10.1.0.155/link/handoff/${RUN_NUMBER}/${LINK_NUMBER} SERVER_URL="10.1.0.155:5000"
curl http://${SERVER_URL}/link/handoff/${RUN_NUMBER}/${LINK_NUMBER}

View file

@ -9,7 +9,7 @@
<form name="link"> <form name="link">
<div class="input-group"> <div class="input-group">
<label for="url-field">Watch server url</label> <label for="url-field">Watch server url</label>
<input name="url-field" type="text" value="localhost:5000" required></input> <input name="url-field" type="text" value="10.1.0.155:5000" required></input>
</div> </div>
<div class="input-group"> <div class="input-group">
<label for="run-field">Run id</label> <label for="run-field">Run id</label>

4
client/start.sh Normal file → Executable file
View file

@ -3,4 +3,6 @@
RUN_NUMBER=0 RUN_NUMBER=0
LINK_NUMBER=0 LINK_NUMBER=0
curl http://10.1.0.155/link/start/${RUN_NUMBER}/${LINK_NUMBER} SERVER_URL="10.1.0.155:5000"
curl http://${SERVER_URL}/link/start/${RUN_NUMBER}/${LINK_NUMBER}

View file

@ -1,8 +1,11 @@
import asyncio import asyncio
import atexit import atexit
import datetime import datetime
import json
import os.path
import socket import socket
from functools import lru_cache from functools import lru_cache
from pathlib import Path
from time import sleep, time from time import sleep, time
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
@ -13,7 +16,7 @@ from flask_cors import CORS
sio = socketio.Server() sio = socketio.Server()
app = Flask(__name__) app = Flask(__name__)
CORS(app) CORS(app, resources={r"/*": {"origins": "*"}})
app.wsgi_app = socketio.WSGIApp(sio, app.wsgi_app) app.wsgi_app = socketio.WSGIApp(sio, app.wsgi_app)
# We request users to send the run number to prevent accidentially getting old requests from a previous run # We request users to send the run number to prevent accidentially getting old requests from a previous run
@ -31,6 +34,21 @@ function_times = []
function_data = [] function_data = []
def save_data():
with open('data.json', 'w') as outfile:
json.dump(db, outfile)
def load_data():
with open('data.json', 'r') as infile:
db = json.load(infile)
data_file = Path("data.json")
if data_file.is_file():
load_data()
@app.route("/") @app.route("/")
def index(): def index():
return render_template('index.html') return render_template('index.html')
@ -38,16 +56,22 @@ def index():
@app.route("/start_run/<run_index>") @app.route("/start_run/<run_index>")
def start_run(run_index): def start_run(run_index):
request_data = live_request(f'/start_run/{run_index}') #make request show up on the live requests if "Authorization" not in request.headers or request.headers[
'Authorization'] != "dsfqlkjmlkjls,,n":
return "Unauthorized"
request_data = live_request(
f'/start_run/{run_index}') #make request show up on the live requests
if not run_index.isdigit(): if not run_index.isdigit():
return f'{run_index} is not a number' return f'{run_index} is not a number'
db["current_run"] = run_index
starttime = time()
if run_index in db["run_data"]: if run_index in db["run_data"]:
return "This run is already ran, take another number." return "This run is already ran, take another number."
db["current_run"] = run_index
starttime = time()
db["run_data"][run_index] = {"starttime": starttime, "data": {}} db["run_data"][run_index] = {"starttime": starttime, "data": {}}
save_data()
# TODO send start request to the first person in the chain. Probably a zeus part already written as example # TODO send start request to the first person in the chain. Probably a zeus part already written as example
sio.emit('start_run', run_index) sio.emit('start_run', run_index)
return f'Run {run_index} started at {starttime}' return f'Run {run_index} started at {starttime}'
@ -55,14 +79,14 @@ def start_run(run_index):
@app.route("/link/start/<run>/<link_index>") @app.route("/link/start/<run>/<link_index>")
def link_start(run, link_index): def link_start(run, link_index):
request_data = live_request(f'/link/start/{run}/{link_index}') #make request show up on the live requests request_data = live_request(f'/link/start/{run}/{link_index}'
) #make request show up on the live requests
if not run.isdigit() or not link_index.isdigit(): if not run.isdigit() or not link_index.isdigit():
return f'{run} and/or {link_index} is not a number' return f'{run} and/or {link_index} is not a number'
start = time() start = time()
if db["current_run"] != run: if db["current_run"] != run:
response = "Wrong run number, check that you update your run", 404 response = "Wrong run number, check that you update your run", 404
else: else:
@ -71,6 +95,7 @@ def link_start(run, link_index):
response = "you already started in this run. Ignoring this request." response = "you already started in this run. Ignoring this request."
else: else:
run_data[link_index] = {"id": link_index, "start": time()} run_data[link_index] = {"id": link_index, "start": time()}
save_data()
sio.emit('link_start', run_data[link_index]) sio.emit('link_start', run_data[link_index])
response = "Success." response = "Success."
stop = time() stop = time()
@ -79,17 +104,21 @@ def link_start(run, link_index):
return response return response
@app.route("/link/handoff/<run>/<index>") @app.route("/link/handoff/<run>/<link_index>")
def link_handoff(run, index): def link_handoff(run, link_index):
request_data = live_request(f'/link/handoff/{run}/{link_index}') #make request show up on the live requests request_data = live_request(f'/link/handoff/{run}/{link_index}'
) #make request show up on the live requests
if not run.isdigit() or not index.isdigit(): if not run.isdigit() or not link_index.isdigit():
return f'{run} and/or {index} is not a number' return f'{run} and/or {link_index} is not a number'
if db["current_run"] != run: if db["current_run"] != run:
return "Wrong run number, check that you updated you run", 404 return "Wrong run number, check that you updated you run", 404
else: else:
link_data = db["run_data"][run]["data"][index] if index not in db["run_data"][run]["data"]:
return "This link did not start yet..."
link_data = db["run_data"][run]["data"][link_index]
save_data()
if "handoff" in link_data: if "handoff" in link_data:
return "you already handed off control during this run. Ignoring this request" return "you already handed off control during this run. Ignoring this request"
else: else:
@ -134,7 +163,7 @@ if __name__ == '__main__':
# scheduler = BackgroundScheduler() # scheduler = BackgroundScheduler()
# scheduler.add_job(func=plot_graph, trigger="interval", seconds=5) # scheduler.add_job(func=plot_graph, trigger="interval", seconds=5)
# scheduler.start() # scheduler.start()
app.run(host="0.0.0.0", debug=True) app.run(host="10.1.0.155", debug=True)
# shut down the scheduler when exiting the app # shut down the scheduler when exiting the app
# atexit.register(lambda: scheduler.shutdown()) # atexit.register(lambda: scheduler.shutdown())

View file

@ -23,7 +23,7 @@ require(['jquery', 'socket.io'], function(jq, io) {
$(function() { $(function() {
const socket = io('localhost:5000'); const socket = io('10.1.0.155:5000');
socket.on('start_run', function(run_index) {}); socket.on('start_run', function(run_index) {});
socket.on('link_start', function(link_data) { socket.on('link_start', function(link_data) {

View file

@ -21,31 +21,28 @@
<body> <body>
<div class="container"> <div class="container">
<div id="current_run" class="card"> <div id="current_run" class="card">
<div class="item"> <!-- <div class="item">
<div> <div>
0 0
</div> </div>
<div> <div>
<button type="button">Start</button> <button type="button" onclick="$.get('10.1.0.155:5000/start_run/0')">Start</button>
</div> </div>
<div> <div>
<i class="fas fa-question" style="color:grey"></i> <i class="fas fa-question" style="color:grey"></i>
</div> </div>
</div> </div> -->
<div class="card__title"> <div class="card__title">
Links Links
</div> </div>
</div> </div>
<div class="card"> <div class="card">
<div class="card__title"> <div class="card__title">
Runs Runs
</div> </div>
</div> </div>
<div id="live_requests" class="card"> <div id="live_requests" class="card">
<div class="card__title"> <div class="card__title">
Live Requests Live Requests
</div> </div>