diff --git a/scripts/flaskdemo/__init__.py b/scripts/flaskdemo/__init__.py new file mode 100644 index 0000000..12638ca --- /dev/null +++ b/scripts/flaskdemo/__init__.py @@ -0,0 +1,5 @@ +from flask import Flask + +app = Flask(__name__) + +from flaskdemo import routes diff --git a/scripts/flaskdemo/routes.py b/scripts/flaskdemo/routes.py new file mode 100644 index 0000000..5b83718 --- /dev/null +++ b/scripts/flaskdemo/routes.py @@ -0,0 +1,50 @@ +from flask import jsonify, render_template +from flaskdemo import app +import json +import pathlib +import time + + +def find_latest(directory_name): + list_of_paths = pathlib.Path(directory_name).glob("*") + list_of_dirs = [] + for path in list_of_paths: + if path.is_dir(): + list_of_dirs.append(path) + latest_dir = max(list_of_dirs, key=lambda p: p.stat().st_ctime) + + return latest_dir, latest_dir / "events.jsonl" + + +def load(directory_name): + directory, path = find_latest(directory_name) + output_dic = [] + + clock = 0 + while not path.exists(): + clock += 1 + time.sleep(1) + if clock >= 60: + raise FileExistsError(f"{path} is not found!") + + with path.open("r") as json_file: + json_list = list(json_file) + for json_str in json_list: + item = json.loads(json_str) + tmp = {} + for key in item.keys(): + tmp[key.replace(".", "_")] = item[key] + output_dic.append(tmp) + + return output_dic, directory + + +@app.route("/get_linechart_data") +def get_linechart_data(): + output, directory = load("./output/run_20200727_159587_7ft4i98n") + return jsonify(output) + + +@app.route("/") +def index(): + return render_template("home.html") diff --git a/scripts/flaskdemo/static/css/main.css b/scripts/flaskdemo/static/css/main.css new file mode 100644 index 0000000..8a686d2 --- /dev/null +++ b/scripts/flaskdemo/static/css/main.css @@ -0,0 +1,21 @@ +#pieChart { + position:relative; + top:40%; + left:10px; + width:400px; + height: 400px; + display: inline-block; +} +#barChart { + position:relative; + top:40%; + height: 400px; + display: inline-block; +} +.slice { + font-size: 12pt; + font-family: Verdana; + fill: white; + font-weight: bold; + cursor: pointer; +} diff --git a/scripts/flaskdemo/static/js/lineChart.js b/scripts/flaskdemo/static/js/lineChart.js new file mode 100644 index 0000000..3c341f3 --- /dev/null +++ b/scripts/flaskdemo/static/js/lineChart.js @@ -0,0 +1,81 @@ +function d3lineChart(data) { + // set the dimensions and margins of the graph + var margin = {top: 10, right: 30, bottom: 30, left: 60}, + width = 460 - margin.left - margin.right, + height = 400 - margin.top - margin.bottom; + + var arr = []; + if (data.length >= 1) { + arr = Object.keys(data[0]); + } + var color = ['#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00','#ffff33','#a65628','#f781bf','#999999', '#e41623', '#3d6eb8']; + + for (let i = 0; i < arr.length; ++i) { + if(arr[i] === "_runtime" || arr[i] === "_timestamp") { + continue; + } + + // append the svg object to the body of the page + + var svg = d3.select("#lineChart") + .append("svg") + .attr("width", width + margin.left + margin.right) + .attr("height", height + margin.top + margin.bottom) + .append("g") + .attr("transform", + "translate(" + margin.left + "," + margin.top + ")"); + + var x = d3.scaleLinear() + .domain([0, d3.max(data, function(d) { return d._runtime; }) + 5]) + .range([ 0, width ]); + svg.append("g") + .attr("transform", "translate(0," + height + ")") + .call(d3.axisBottom(x)); + // Add Y axis + var y = d3.scaleLinear() + .domain([d3.min(data, function(d) { return Reflect.get(d, arr[i]); }) - 2, d3.max(data, function(d) { return Reflect.get(d, arr[i]); }) + 2]) + .range([ height, 0 ]); + svg.append("g") + .call(d3.axisLeft(y)); + + // Add X axis label: + svg.append("text") + .attr("text-anchor", "end") + .attr("x", width) + .attr("y", height + margin.top + 20) + .text("Time(minutes)"); + + // Y axis label: + svg.append("text") + .attr("text-anchor", "end") + .attr("transform", "rotate(-90)") + .attr("transform", "rotate(-90)") + .attr("y", -margin.left + 20) + .attr("x", -margin.top) + .text(arr[i]); + + + // Add the line + svg.append("path") + .datum(data) + .attr("fill", "none") + .attr("stroke", color[i]) + .attr("stroke-width", 1.5) + .attr("d", d3.line() + .x(function(d) { return x(d._runtime) }) + .y(function(d) { return y(Reflect.get(d, arr[i])) }) + ); + + // Add the points + svg.append("g") + .selectAll("dot") + .data(data) + .enter() + .append("circle") + .attr("cx", function(d) { return x(d._runtime) } ) + .attr("cy", function(d) { return y(Reflect.get(d, arr[i])) } ) + .attr("r", 5) + .attr("fill", color[i]); + } + +} \ No newline at end of file diff --git a/scripts/flaskdemo/static/js/main.js b/scripts/flaskdemo/static/js/main.js new file mode 100644 index 0000000..eb8d336 --- /dev/null +++ b/scripts/flaskdemo/static/js/main.js @@ -0,0 +1,7 @@ +queue() + .defer(d3.json, lineChartDataUrl) + .await(ready); + +function ready(error, dataset) { + d3lineChart(dataset); +} \ No newline at end of file diff --git a/scripts/flaskdemo/templates/base.html b/scripts/flaskdemo/templates/base.html new file mode 100644 index 0000000..d23fabc --- /dev/null +++ b/scripts/flaskdemo/templates/base.html @@ -0,0 +1,30 @@ + + + + + + + + Home page + + + + +
+
+
+ {% block content %}{% endblock %} +
+
+
+ + + + + + + + + \ No newline at end of file diff --git a/scripts/flaskdemo/templates/home.html b/scripts/flaskdemo/templates/home.html new file mode 100644 index 0000000..1add758 --- /dev/null +++ b/scripts/flaskdemo/templates/home.html @@ -0,0 +1,4 @@ +{% extends "base.html" %} +{% block content %} +
+{% endblock content %} \ No newline at end of file diff --git a/scripts/flaskdemo/templates/tmp.html b/scripts/flaskdemo/templates/tmp.html new file mode 100644 index 0000000..c629376 --- /dev/null +++ b/scripts/flaskdemo/templates/tmp.html @@ -0,0 +1,87 @@ + + + + + + + + + +
+ + + \ No newline at end of file diff --git a/scripts/runweb.py b/scripts/runweb.py new file mode 100644 index 0000000..bf3047e --- /dev/null +++ b/scripts/runweb.py @@ -0,0 +1,4 @@ +from flaskdemo import app + +if __name__ == "__main__": + app.run()