blob: ada3f176da208e5426ebe037a0992a59a6b0bd2a [file] [log] [blame]
gstepanov2164d822015-02-04 19:49:40 +02001from flask import Flask, render_template, url_for, request, g
2from flask_bootstrap import Bootstrap
3import json
4import os.path
gstepanov200d3b42015-02-06 14:01:49 +02005from report import build_vertical_bar, build_lines_chart
gstepanovc3ffac72015-02-05 15:39:45 +02006from storage_api import create_storage, TEST_PATH
gstepanov2164d822015-02-04 19:49:40 +02007
8app = Flask(__name__)
9Bootstrap(app)
10
11
12def collect_tests():
13 result = []
14
gstepanovc3ffac72015-02-05 15:39:45 +020015 for file in os.listdir(TEST_PATH):
gstepanov2164d822015-02-04 19:49:40 +020016 if file.endswith(".json"):
17 result.append(file.split('.')[0])
18
19 return result
20
21
22def load_test(test_name):
23 test_name += '.json'
24
gstepanov3501ad42015-02-05 15:41:39 +020025 with open(TEST_PATH + "/" + test_name, 'rt') as f:
gstepanov2164d822015-02-04 19:49:40 +020026 raw = f.read()
27
gstepanov0a8fdd32015-02-05 15:54:11 +020028 if raw != "":
29 test = json.loads(raw)
30 else:
31 test = []
gstepanov2164d822015-02-04 19:49:40 +020032
33 return test
34
35
36@app.route("/", methods=['GET', 'POST'])
37def index():
38 data = []
39 for test in collect_tests():
40 d = {}
41 d["name"] = test
42 d["url"] = url_for("render_test", test_name=test)
43 data.append(d)
44
45 return render_template("index.html", tests=data)
46
47
48@app.route("/tests/<test_name>", methods=['GET'])
49def render_test(test_name):
50 tests = load_test(test_name)
51 header_keys = ['build_id', 'iso_md5', 'type']
gstepanov0a8fdd32015-02-05 15:54:11 +020052 table = [[]]
gstepanov200d3b42015-02-06 14:01:49 +020053 storage = create_storage('file://' + TEST_PATH + '/' + test_name + '.json')
54 results = storage.recent_builds()
gstepanov2164d822015-02-04 19:49:40 +020055
gstepanov200d3b42015-02-06 14:01:49 +020056 bars = build_vertical_bar(results)
57 lines = build_lines_chart(results)
58 urls = bars + lines
gstepanov2164d822015-02-04 19:49:40 +020059 if len(tests) > 0:
60 sorted_keys = sorted(tests[0].keys())
61
gstepanov0a8fdd32015-02-05 15:54:11 +020062 for key in sorted_keys:
63 if key not in header_keys:
64 header_keys.append(key)
gstepanov2164d822015-02-04 19:49:40 +020065
gstepanov0a8fdd32015-02-05 15:54:11 +020066 for test in tests:
67 row = []
gstepanov2164d822015-02-04 19:49:40 +020068
gstepanov0a8fdd32015-02-05 15:54:11 +020069 for header in header_keys:
70 if isinstance(test[header], list):
71 row.append(str(test[header][0]) + unichr(0x00B1) + str(test[header][1]))
72 else:
73 row.append(test[header])
gstepanov2164d822015-02-04 19:49:40 +020074
gstepanov0a8fdd32015-02-05 15:54:11 +020075 table.append(row)
gstepanov2164d822015-02-04 19:49:40 +020076
gstepanov200d3b42015-02-06 14:01:49 +020077 return render_template("table.html", headers=header_keys, table=table, urls=urls)
gstepanov2164d822015-02-04 19:49:40 +020078
79
80@app.route("/tests/<test_name>", methods=['POST'])
81def add_test(test_name):
82 tests = json.loads(request.data)
83
84 if not hasattr(g, "storage"):
gstepanov993c74b2015-02-05 15:20:38 +020085 g.storage = create_storage("file://" + os.path.dirname(__file__) + "/test_results/sample.json", "", "")
gstepanov2164d822015-02-04 19:49:40 +020086
87 for test in tests:
88 g.storage.store(test)
89 return "Created", 201
90
91
gstepanov2164d822015-02-04 19:49:40 +020092if __name__ == "__main__":
gstepanov6f7167d2015-02-06 12:51:58 +020093 app.run(host='0.0.0.0', debug=True)