diff --git a/watcher/app.py b/watcher/app.py index fa68c22..ee3538a 100644 --- a/watcher/app.py +++ b/watcher/app.py @@ -1,4 +1,4 @@ -from datetime import datetime +from time import time import socketio from flask import Flask, render_template @@ -16,7 +16,7 @@ app.wsgi_app = socketio.WSGIApp(sio, app.wsgi_app) # Save progress through time during 1 run # Save result of different runs -db = {"current_run": 0, "run_start_timer": None, "run_data": {}} +db = {"current_run": None, "run_start_timer": None, "run_data": {}} @app.route("/") @@ -27,7 +27,7 @@ def index(): @app.route("/start_run/") def start_run(run_index): db["current_run"] = run_index - starttime = datetime.now() + starttime = time() if run_index in db["run_data"]: return "This run is already ran, take another number." db["run_data"][run_index] = {"starttime": starttime, "data": {}} @@ -39,29 +39,37 @@ def start_run(run_index): @app.route("/link/start//") def link_start(run, index): if db["current_run"] != run: - return "Wrong run number, you are probably behind.", 404 + return "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." else: - run_data[index] = {"start": datetime.now()} - sio.emit('link_start', index) + run_data[index] = {"id": index, "start": time()} + sio.emit('link_start', run_data[index]) return "Success." @app.route("/link/handoff//") def link_handoff(run, index): if db["current_run"] != run: - return "Wrong run number, you are probably behind.", 404 + return "Wrong run number, check that you updated you run", 404 else: link_data = db["run_data"][run]["data"][index] - if link_data["handoff"]: + if "handoff" in link_data: return "you already handed off control during this run. Ignoring this request" else: - link_data["handoff"] = datetime.now() + link_data["handoff"] = time() + sio.emit('link_handoff', link_data) return "Success." +@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) + + 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 41303b7..2e54462 100644 --- a/watcher/static/index.js +++ b/watcher/static/index.js @@ -1,11 +1,71 @@ -import io from 'socket.io-client'; - -$(function() { - var socket = io(); - socket.on('start_run', function(run_index){ - }) - - socket.on('link_start', function(link_id) { - $('#current_run').append($('
').text(link_id)); - }); +requirejs.config({ + paths: { + 'socket.io': 'https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io', + 'jquery': 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min', + }, + waitSeconds: 3 +}); + + + +require(['jquery', 'socket.io'], function(jq, io) { + + function getDurationString(link_data) { + // Print time diff in following format + // 1s + // 11s + // 1:11s + // 1:01s + let duration = new Date((link_data["handoff"] - link_data["start"]) * 1000); + let minutes = duration.getMinutes(); + let seconds = minutes > 0 ? (duration.getSeconds() > 9 ? duration.getSeconds() : "0" + duration.getSeconds()) : duration.getSeconds(); + let durationString = (minutes > 0 ? (minutes + "m") : "") + seconds + "s"; + return durationString; + } + + + $(function() { + const socket = io('localhost:5000'); + socket.on('start_run', function(run_index) {}); + + socket.on('link_start', function(link_data) { + addLink(link_data["id"], link_data); + }); + socket.on('link_handoff', function(link_data) { + stop = new Date(link_data["handoff"] * 1000); + let stopString = stop.getHours() + ":" + stop.getMinutes(); + + $(`#link-${link_data["id"]} .link-status`).css("color", "green").removeClass("fa-question").addClass("fa-check"); + $(`#link-${link_data["id"]} .link-time`).text(`Time: ${getDurationString(link_data)}`); + }); + + socket.on('sync_current_run', function(run_data) { + $('#current_run>div:gt(0)').remove(); + for (link_id in run_data["data"]) { + addLink(link_id, run_data["data"][link_id]); + } + }); + }); + + function addLink(link_id, link_data) { + let start = new Date(link_data["start"] * 1000); + let datestring = start.getHours() + ":" + start.getMinutes(); + let stop = undefined; + let stopString = undefined; + if (link_data["handoff"]) { + stop = new Date(link_data["handoff"] * 1000); + stopString = stop.getHours() + ":" + stop.getMinutes(); + + } + let html = ` + +`; + $('#current_run').append($(html)); + } }); diff --git a/watcher/templates/index.html b/watcher/templates/index.html index 0310c4b..1e0219d 100644 --- a/watcher/templates/index.html +++ b/watcher/templates/index.html @@ -2,55 +2,54 @@ - - - +
-
-
-
- 1 -
-
- -
-
- -
-
-
-
- 1 +
+
+
+ 0 +
+
+ +
+
+ +
-
- +
+
+ 1 +
+
+ Time: ... +
+
+ +
-
- -
-
-
-
-
-
- 1 -
-
- total time -
-
- max fase -
-
-
-
live requests
-
+
+
+
+ 1 +
+
+ total time +
+
+ max fase +
+
+
+
+
live requests
+
- + +