gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 1 | from flask import Flask, render_template, url_for, request, g |
| 2 | from flask_bootstrap import Bootstrap |
| 3 | import json |
| 4 | import os.path |
gstepanov | 725cc30 | 2015-02-09 15:08:06 +0200 | [diff] [blame] | 5 | from config import TEST_PATH |
gstepanov | 200d3b4 | 2015-02-06 14:01:49 +0200 | [diff] [blame] | 6 | from report import build_vertical_bar, build_lines_chart |
gstepanov | 725cc30 | 2015-02-09 15:08:06 +0200 | [diff] [blame] | 7 | from storage_api import create_storage |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 8 | |
| 9 | app = Flask(__name__) |
| 10 | Bootstrap(app) |
| 11 | |
| 12 | |
| 13 | def collect_tests(): |
| 14 | result = [] |
| 15 | |
gstepanov | c3ffac7 | 2015-02-05 15:39:45 +0200 | [diff] [blame] | 16 | for file in os.listdir(TEST_PATH): |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 17 | if file.endswith(".json"): |
| 18 | result.append(file.split('.')[0]) |
| 19 | |
| 20 | return result |
| 21 | |
| 22 | |
| 23 | def load_test(test_name): |
| 24 | test_name += '.json' |
| 25 | |
gstepanov | 3501ad4 | 2015-02-05 15:41:39 +0200 | [diff] [blame] | 26 | with open(TEST_PATH + "/" + test_name, 'rt') as f: |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 27 | raw = f.read() |
| 28 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 29 | if raw != "": |
| 30 | test = json.loads(raw) |
| 31 | else: |
| 32 | test = [] |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 33 | |
| 34 | return test |
| 35 | |
| 36 | |
| 37 | @app.route("/", methods=['GET', 'POST']) |
| 38 | def 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']) |
| 50 | def render_test(test_name): |
| 51 | tests = load_test(test_name) |
| 52 | header_keys = ['build_id', 'iso_md5', 'type'] |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 53 | table = [[]] |
gstepanov | 200d3b4 | 2015-02-06 14:01:49 +0200 | [diff] [blame] | 54 | storage = create_storage('file://' + TEST_PATH + '/' + test_name + '.json') |
| 55 | results = storage.recent_builds() |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 56 | |
gstepanov | 200d3b4 | 2015-02-06 14:01:49 +0200 | [diff] [blame] | 57 | bars = build_vertical_bar(results) |
| 58 | lines = build_lines_chart(results) |
| 59 | urls = bars + lines |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 60 | if len(tests) > 0: |
| 61 | sorted_keys = sorted(tests[0].keys()) |
| 62 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 63 | for key in sorted_keys: |
| 64 | if key not in header_keys: |
| 65 | header_keys.append(key) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 66 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 67 | for test in tests: |
| 68 | row = [] |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 69 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 70 | 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]) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 75 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 76 | table.append(row) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 77 | |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 78 | 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>/") |
| 82 | def 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)) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 106 | |
| 107 | |
| 108 | @app.route("/tests/<test_name>", methods=['POST']) |
| 109 | def add_test(test_name): |
| 110 | tests = json.loads(request.data) |
| 111 | |
| 112 | if not hasattr(g, "storage"): |
gstepanov | 3c38ead | 2015-02-09 15:33:36 +0200 | [diff] [blame^] | 113 | g.storage = create_storage("file://" + TEST_PATH + '/' + test_name + ".json", "", "") |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 114 | |
| 115 | for test in tests: |
| 116 | g.storage.store(test) |
| 117 | return "Created", 201 |
| 118 | |
| 119 | |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 120 | if __name__ == "__main__": |
gstepanov | 6f7167d | 2015-02-06 12:51:58 +0200 | [diff] [blame] | 121 | app.run(host='0.0.0.0', debug=True) |