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 |
| 5 | from storage_api import create_storage |
| 6 | |
| 7 | app = Flask(__name__) |
| 8 | Bootstrap(app) |
| 9 | |
| 10 | |
| 11 | def collect_tests(): |
| 12 | result = [] |
| 13 | |
| 14 | for file in os.listdir("../test_results"): |
| 15 | if file.endswith(".json"): |
| 16 | result.append(file.split('.')[0]) |
| 17 | |
| 18 | return result |
| 19 | |
| 20 | |
| 21 | def load_test(test_name): |
| 22 | test_name += '.json' |
| 23 | |
| 24 | with open("../" + test_name, 'rt') as f: |
| 25 | raw = f.read() |
| 26 | |
| 27 | if raw == '': |
| 28 | raise Exception("Test is emoty") |
| 29 | |
| 30 | test = json.loads(raw) |
| 31 | |
| 32 | return test |
| 33 | |
| 34 | |
| 35 | @app.route("/", methods=['GET', 'POST']) |
| 36 | def index(): |
| 37 | data = [] |
| 38 | for test in collect_tests(): |
| 39 | d = {} |
| 40 | d["name"] = test |
| 41 | d["url"] = url_for("render_test", test_name=test) |
| 42 | data.append(d) |
| 43 | |
| 44 | return render_template("index.html", tests=data) |
| 45 | |
| 46 | |
| 47 | @app.route("/tests/<test_name>", methods=['GET']) |
| 48 | def render_test(test_name): |
| 49 | tests = load_test(test_name) |
| 50 | header_keys = ['build_id', 'iso_md5', 'type'] |
| 51 | table = [] |
| 52 | |
| 53 | if len(tests) > 0: |
| 54 | sorted_keys = sorted(tests[0].keys()) |
| 55 | |
| 56 | for key in sorted_keys: |
| 57 | if key not in header_keys: |
| 58 | header_keys.append(key) |
| 59 | |
| 60 | for test in tests: |
| 61 | row = [] |
| 62 | |
| 63 | for header in header_keys: |
| 64 | if isinstance(test[header], list): |
| 65 | row.append(str(test[header][0]) + unichr(0x00B1) + str(test[header][1])) |
| 66 | else: |
| 67 | row.append(test[header]) |
| 68 | |
| 69 | table.append(row) |
| 70 | |
| 71 | return render_template("table.html", headers=header_keys, table=table) |
| 72 | |
| 73 | |
| 74 | @app.route("/tests/<test_name>", methods=['POST']) |
| 75 | def add_test(test_name): |
| 76 | tests = json.loads(request.data) |
| 77 | |
| 78 | if not hasattr(g, "storage"): |
| 79 | g.storage = create_storage("file:///home/gstepanov/rally-results-processor/test_results/sample.json", "", "") |
| 80 | |
| 81 | for test in tests: |
| 82 | g.storage.store(test) |
| 83 | return "Created", 201 |
| 84 | |
| 85 | |
| 86 | |
| 87 | if __name__ == "__main__": |
| 88 | app.run(debug=True) |