blob: a5be111c43fd9ca19a6d7564592452db33d4697d [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
gstepanov725cc302015-02-09 15:08:06 +02005from config import TEST_PATH
gstepanov200d3b42015-02-06 14:01:49 +02006from report import build_vertical_bar, build_lines_chart
gstepanov725cc302015-02-09 15:08:06 +02007from storage_api import create_storage
gstepanov2164d822015-02-04 19:49:40 +02008
9app = Flask(__name__)
10Bootstrap(app)
11
12
13def collect_tests():
14 result = []
15
gstepanovc3ffac72015-02-05 15:39:45 +020016 for file in os.listdir(TEST_PATH):
gstepanov2164d822015-02-04 19:49:40 +020017 if file.endswith(".json"):
18 result.append(file.split('.')[0])
19
20 return result
21
22
23def load_test(test_name):
24 test_name += '.json'
25
gstepanov3501ad42015-02-05 15:41:39 +020026 with open(TEST_PATH + "/" + test_name, 'rt') as f:
gstepanov2164d822015-02-04 19:49:40 +020027 raw = f.read()
28
gstepanov0a8fdd32015-02-05 15:54:11 +020029 if raw != "":
30 test = json.loads(raw)
31 else:
32 test = []
gstepanov2164d822015-02-04 19:49:40 +020033
34 return test
35
36
37@app.route("/", methods=['GET', 'POST'])
38def index():
39 data = []
40 for test in collect_tests():
41 d = {}
42 d["name"] = test
43 d["url"] = url_for("render_test", test_name=test)
44 data.append(d)
45
46 return render_template("index.html", tests=data)
47
48
49@app.route("/tests/<test_name>", methods=['GET'])
50def render_test(test_name):
51 tests = load_test(test_name)
52 header_keys = ['build_id', 'iso_md5', 'type']
gstepanov0a8fdd32015-02-05 15:54:11 +020053 table = [[]]
gstepanov200d3b42015-02-06 14:01:49 +020054 storage = create_storage('file://' + TEST_PATH + '/' + test_name + '.json')
55 results = storage.recent_builds()
gstepanov2164d822015-02-04 19:49:40 +020056
gstepanov200d3b42015-02-06 14:01:49 +020057 bars = build_vertical_bar(results)
58 lines = build_lines_chart(results)
59 urls = bars + lines
gstepanov2164d822015-02-04 19:49:40 +020060 if len(tests) > 0:
61 sorted_keys = sorted(tests[0].keys())
62
gstepanov0a8fdd32015-02-05 15:54:11 +020063 for key in sorted_keys:
64 if key not in header_keys:
65 header_keys.append(key)
gstepanov2164d822015-02-04 19:49:40 +020066
gstepanov0a8fdd32015-02-05 15:54:11 +020067 for test in tests:
68 row = []
gstepanov2164d822015-02-04 19:49:40 +020069
gstepanov0a8fdd32015-02-05 15:54:11 +020070 for header in header_keys:
71 if isinstance(test[header], list):
72 row.append(str(test[header][0]) + unichr(0x00B1) + str(test[header][1]))
73 else:
74 row.append(test[header])
gstepanov2164d822015-02-04 19:49:40 +020075
gstepanov0a8fdd32015-02-05 15:54:11 +020076 table.append(row)
gstepanov2164d822015-02-04 19:49:40 +020077
gstepanovcd77d5a2015-02-06 14:49:34 +020078 return render_template("test.html", urls=urls, table_url=url_for('render_table', test_name=test_name))
79
80
81@app.route("/tests/table/<test_name>/")
82def render_table(test_name):
83 tests = load_test(test_name)
84 header_keys = ['build_id', 'iso_md5', 'type']
85 table = [[]]
86
87 if len(tests) > 0:
88 sorted_keys = sorted(tests[0].keys())
89
90 for key in sorted_keys:
91 if key not in header_keys:
92 header_keys.append(key)
93
94 for test in tests:
95 row = []
96
97 for header in header_keys:
98 if isinstance(test[header], list):
99 row.append(str(test[header][0]) + unichr(0x00B1) + str(test[header][1]))
100 else:
101 row.append(test[header])
102
103 table.append(row)
104
105 return render_template("table.html", headers=header_keys, table=table, back_url=url_for('render_test', test_name=test_name))
gstepanov2164d822015-02-04 19:49:40 +0200106
107
108@app.route("/tests/<test_name>", methods=['POST'])
109def add_test(test_name):
110 tests = json.loads(request.data)
111
112 if not hasattr(g, "storage"):
gstepanov3c38ead2015-02-09 15:33:36 +0200113 g.storage = create_storage("file://" + TEST_PATH + '/' + test_name + ".json", "", "")
gstepanov2164d822015-02-04 19:49:40 +0200114
115 for test in tests:
116 g.storage.store(test)
117 return "Created", 201
118
119
gstepanov2164d822015-02-04 19:49:40 +0200120if __name__ == "__main__":
gstepanov6f7167d2015-02-06 12:51:58 +0200121 app.run(host='0.0.0.0', debug=True)