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 | bb85492 | 2015-02-09 18:18:17 +0200 | [diff] [blame] | 8 | import requests |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 9 | |
| 10 | app = Flask(__name__) |
| 11 | Bootstrap(app) |
| 12 | |
| 13 | |
| 14 | def collect_tests(): |
| 15 | result = [] |
| 16 | |
gstepanov | c3ffac7 | 2015-02-05 15:39:45 +0200 | [diff] [blame] | 17 | for file in os.listdir(TEST_PATH): |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 18 | if file.endswith(".json"): |
| 19 | result.append(file.split('.')[0]) |
| 20 | |
| 21 | return result |
| 22 | |
| 23 | |
| 24 | def load_test(test_name): |
| 25 | test_name += '.json' |
| 26 | |
gstepanov | 3501ad4 | 2015-02-05 15:41:39 +0200 | [diff] [blame] | 27 | with open(TEST_PATH + "/" + test_name, 'rt') as f: |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 28 | raw = f.read() |
| 29 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 30 | if raw != "": |
| 31 | test = json.loads(raw) |
| 32 | else: |
| 33 | test = [] |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 34 | |
| 35 | return test |
| 36 | |
| 37 | |
| 38 | @app.route("/", methods=['GET', 'POST']) |
| 39 | def index(): |
| 40 | data = [] |
| 41 | for test in collect_tests(): |
| 42 | d = {} |
| 43 | d["name"] = test |
| 44 | d["url"] = url_for("render_test", test_name=test) |
| 45 | data.append(d) |
| 46 | |
| 47 | return render_template("index.html", tests=data) |
| 48 | |
| 49 | |
| 50 | @app.route("/tests/<test_name>", methods=['GET']) |
| 51 | def render_test(test_name): |
| 52 | tests = load_test(test_name) |
| 53 | header_keys = ['build_id', 'iso_md5', 'type'] |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 54 | table = [[]] |
gstepanov | 200d3b4 | 2015-02-06 14:01:49 +0200 | [diff] [blame] | 55 | storage = create_storage('file://' + TEST_PATH + '/' + test_name + '.json') |
| 56 | results = storage.recent_builds() |
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 | |
gstepanov | bb85492 | 2015-02-09 18:18:17 +0200 | [diff] [blame] | 81 | def collect_lab_data(tests, meta): |
| 82 | print requests.get(meta['__meta__'], cookies={"token" : '72237e94dc2b408482c29edaf3609da7'}).content |
| 83 | lab_info = json.loads(requests.get(meta['__meta__'], cookies={"token" : '72237e94dc2b408482c29edaf3609da7'}).content) |
| 84 | nodes = [] |
| 85 | result = {} |
| 86 | |
| 87 | for node in lab_info: |
| 88 | d = {} |
| 89 | d['name'] = node['name'] |
| 90 | p = [] |
| 91 | i = [] |
| 92 | disks = [] |
| 93 | devices = [] |
| 94 | |
| 95 | for processor in node['meta']['cpu']['spec']: |
| 96 | p.append(processor) |
| 97 | |
| 98 | for iface in node['meta']['interfaces']: |
| 99 | i.append(iface) |
| 100 | |
| 101 | m = node['meta']['memory'].copy() |
| 102 | |
| 103 | for disk in node['meta']['disks']: |
| 104 | disks.append(disk) |
| 105 | |
| 106 | d['memory'] = m |
| 107 | d['disks'] = disks |
| 108 | d['devices'] = devices |
| 109 | d['interfaces'] = i |
| 110 | d['processors'] = p |
| 111 | |
| 112 | nodes.append(d) |
| 113 | |
| 114 | result['nodes'] = nodes |
| 115 | result['name'] = 'Perf-1 Env' |
| 116 | |
| 117 | return result |
| 118 | |
| 119 | |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 120 | @app.route("/tests/table/<test_name>/") |
| 121 | def render_table(test_name): |
| 122 | tests = load_test(test_name) |
| 123 | header_keys = ['build_id', 'iso_md5', 'type'] |
| 124 | table = [[]] |
gstepanov | bb85492 | 2015-02-09 18:18:17 +0200 | [diff] [blame] | 125 | meta = {"__meta__" : "http://172.16.52.112:8000/api/nodes"} |
| 126 | data = collect_lab_data(tests, meta) |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 127 | |
| 128 | if len(tests) > 0: |
| 129 | sorted_keys = sorted(tests[0].keys()) |
| 130 | |
| 131 | for key in sorted_keys: |
| 132 | if key not in header_keys: |
| 133 | header_keys.append(key) |
| 134 | |
| 135 | for test in tests: |
| 136 | row = [] |
| 137 | |
| 138 | for header in header_keys: |
| 139 | if isinstance(test[header], list): |
| 140 | row.append(str(test[header][0]) + unichr(0x00B1) + str(test[header][1])) |
| 141 | else: |
| 142 | row.append(test[header]) |
| 143 | |
| 144 | table.append(row) |
| 145 | |
gstepanov | bb85492 | 2015-02-09 18:18:17 +0200 | [diff] [blame] | 146 | return render_template("table.html", headers=header_keys, table=table, |
| 147 | back_url=url_for('render_test', test_name=test_name), lab=data) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 148 | |
| 149 | |
| 150 | @app.route("/tests/<test_name>", methods=['POST']) |
| 151 | def add_test(test_name): |
| 152 | tests = json.loads(request.data) |
| 153 | |
| 154 | if not hasattr(g, "storage"): |
gstepanov | f8f01c1 | 2015-02-09 18:34:01 +0200 | [diff] [blame^] | 155 | path = "file://" + TEST_PATH + '/' + test_name + ".json" |
| 156 | print path |
| 157 | g.storage = create_storage(path, "", "") |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 158 | |
| 159 | for test in tests: |
| 160 | g.storage.store(test) |
| 161 | return "Created", 201 |
| 162 | |
| 163 | |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 164 | if __name__ == "__main__": |
gstepanov | 6f7167d | 2015-02-06 12:51:58 +0200 | [diff] [blame] | 165 | app.run(host='0.0.0.0', debug=True) |