gstepanov | 94531b8 | 2015-02-11 14:20:34 +0200 | [diff] [blame] | 1 | from urlparse import urlparse |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 2 | from flask import Flask, render_template, url_for, request, g |
| 3 | from flask_bootstrap import Bootstrap |
gstepanov | 725cc30 | 2015-02-09 15:08:06 +0200 | [diff] [blame] | 4 | from config import TEST_PATH |
gstepanov | 200d3b4 | 2015-02-06 14:01:49 +0200 | [diff] [blame] | 5 | from report import build_vertical_bar, build_lines_chart |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame^] | 6 | from storage_api import create_storage, Measurement |
gstepanov | 81a52ee | 2015-02-10 16:22:20 +0200 | [diff] [blame] | 7 | from logging import getLogger, INFO |
| 8 | |
| 9 | import json |
| 10 | import os.path |
gstepanov | 94531b8 | 2015-02-11 14:20:34 +0200 | [diff] [blame] | 11 | from web_app.keystone import KeystoneAuth |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 12 | |
| 13 | app = Flask(__name__) |
| 14 | Bootstrap(app) |
| 15 | |
| 16 | |
| 17 | def collect_tests(): |
| 18 | result = [] |
| 19 | |
gstepanov | c3ffac7 | 2015-02-05 15:39:45 +0200 | [diff] [blame] | 20 | for file in os.listdir(TEST_PATH): |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 21 | if file.endswith(".json"): |
| 22 | result.append(file.split('.')[0]) |
| 23 | |
| 24 | return result |
| 25 | |
| 26 | |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame^] | 27 | def collect_builds(): |
| 28 | builds = [] |
| 29 | build_set = set() |
| 30 | tests = collect_tests() |
| 31 | |
| 32 | for t in tests: |
| 33 | test = load_test(t) |
| 34 | |
| 35 | for build in test: |
| 36 | if build["type"] not in build_set: |
| 37 | build_set.add(build["type"]) |
| 38 | builds.append(build) |
| 39 | |
| 40 | return builds |
| 41 | |
| 42 | |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 43 | def load_test(test_name): |
| 44 | test_name += '.json' |
| 45 | |
gstepanov | 3501ad4 | 2015-02-05 15:41:39 +0200 | [diff] [blame] | 46 | with open(TEST_PATH + "/" + test_name, 'rt') as f: |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 47 | raw = f.read() |
| 48 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 49 | if raw != "": |
| 50 | test = json.loads(raw) |
| 51 | else: |
| 52 | test = [] |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 53 | |
| 54 | return test |
| 55 | |
| 56 | |
| 57 | @app.route("/", methods=['GET', 'POST']) |
| 58 | def index(): |
| 59 | data = [] |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame^] | 60 | |
| 61 | for build in collect_builds(): |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 62 | d = {} |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame^] | 63 | d["name"] = build['type'] |
| 64 | d["url"] = url_for("render_test", test_name=build['type']) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 65 | data.append(d) |
| 66 | |
| 67 | return render_template("index.html", tests=data) |
| 68 | |
| 69 | |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame^] | 70 | def create_measurement(build): |
| 71 | m = Measurement() |
| 72 | m.build = build.pop("build_id") |
| 73 | m.build_type = build.pop("type") |
| 74 | m.md5 = build.pop("iso_md5") |
| 75 | m.results = {k: v for k, v in build.items()} |
| 76 | |
| 77 | return m |
| 78 | |
| 79 | |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 80 | @app.route("/tests/<test_name>", methods=['GET']) |
| 81 | def render_test(test_name): |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame^] | 82 | tests = [] #load_test(test_name) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 83 | header_keys = ['build_id', 'iso_md5', 'type'] |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 84 | table = [[]] |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame^] | 85 | builds_to_compare = ['GA', 'master', test_name] |
| 86 | builds = collect_builds() |
| 87 | results = {} |
| 88 | |
| 89 | for build in builds: |
| 90 | if build['type'] in builds_to_compare: |
| 91 | type = build['type'] |
| 92 | m = create_measurement(build) |
| 93 | results[type] = m |
| 94 | |
gstepanov | 200d3b4 | 2015-02-06 14:01:49 +0200 | [diff] [blame] | 95 | bars = build_vertical_bar(results) |
| 96 | lines = build_lines_chart(results) |
| 97 | urls = bars + lines |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 98 | if len(tests) > 0: |
| 99 | sorted_keys = sorted(tests[0].keys()) |
| 100 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 101 | for key in sorted_keys: |
| 102 | if key not in header_keys: |
| 103 | header_keys.append(key) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 104 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 105 | for test in tests: |
| 106 | row = [] |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 107 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 108 | for header in header_keys: |
| 109 | if isinstance(test[header], list): |
| 110 | row.append(str(test[header][0]) + unichr(0x00B1) + str(test[header][1])) |
| 111 | else: |
| 112 | row.append(test[header]) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 113 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 114 | table.append(row) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 115 | |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 116 | return render_template("test.html", urls=urls, table_url=url_for('render_table', test_name=test_name)) |
| 117 | |
| 118 | |
gstepanov | bb85492 | 2015-02-09 18:18:17 +0200 | [diff] [blame] | 119 | def collect_lab_data(tests, meta): |
gstepanov | 94531b8 | 2015-02-11 14:20:34 +0200 | [diff] [blame] | 120 | u = urlparse(meta['__meta__']) |
| 121 | cred = {"username": "admin", "password": "admin", "tenant_name": "admin"} |
| 122 | keystone = KeystoneAuth(root_url=meta['__meta__'], creds=cred, admin_node_ip=u.hostname) |
| 123 | lab_info = keystone.do(method='get', path="") |
gstepanov | bb85492 | 2015-02-09 18:18:17 +0200 | [diff] [blame] | 124 | nodes = [] |
| 125 | result = {} |
| 126 | |
| 127 | for node in lab_info: |
| 128 | d = {} |
| 129 | d['name'] = node['name'] |
| 130 | p = [] |
| 131 | i = [] |
| 132 | disks = [] |
| 133 | devices = [] |
| 134 | |
| 135 | for processor in node['meta']['cpu']['spec']: |
| 136 | p.append(processor) |
| 137 | |
| 138 | for iface in node['meta']['interfaces']: |
| 139 | i.append(iface) |
| 140 | |
| 141 | m = node['meta']['memory'].copy() |
| 142 | |
| 143 | for disk in node['meta']['disks']: |
| 144 | disks.append(disk) |
| 145 | |
| 146 | d['memory'] = m |
| 147 | d['disks'] = disks |
| 148 | d['devices'] = devices |
| 149 | d['interfaces'] = i |
| 150 | d['processors'] = p |
| 151 | |
| 152 | nodes.append(d) |
| 153 | |
| 154 | result['nodes'] = nodes |
| 155 | result['name'] = 'Perf-1 Env' |
| 156 | |
| 157 | return result |
| 158 | |
| 159 | |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 160 | @app.route("/tests/table/<test_name>/") |
| 161 | def render_table(test_name): |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame^] | 162 | builds = collect_builds() |
| 163 | builds = filter(lambda x: x["type"] in ['GA', 'master', test_name], builds) |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 164 | header_keys = ['build_id', 'iso_md5', 'type'] |
| 165 | table = [[]] |
gstepanov | 94531b8 | 2015-02-11 14:20:34 +0200 | [diff] [blame] | 166 | meta = {"__meta__": "http://172.16.52.112:8000/api/nodes"} |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame^] | 167 | data = collect_lab_data(builds, meta) |
| 168 | |
| 169 | if len(builds) > 0: |
| 170 | sorted_keys = sorted(builds[0].keys()) |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 171 | |
| 172 | for key in sorted_keys: |
| 173 | if key not in header_keys: |
| 174 | header_keys.append(key) |
| 175 | |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame^] | 176 | for test in builds: |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 177 | row = [] |
| 178 | |
| 179 | for header in header_keys: |
| 180 | if isinstance(test[header], list): |
| 181 | row.append(str(test[header][0]) + unichr(0x00B1) + str(test[header][1])) |
| 182 | else: |
| 183 | row.append(test[header]) |
| 184 | |
| 185 | table.append(row) |
| 186 | |
gstepanov | bb85492 | 2015-02-09 18:18:17 +0200 | [diff] [blame] | 187 | return render_template("table.html", headers=header_keys, table=table, |
| 188 | back_url=url_for('render_test', test_name=test_name), lab=data) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 189 | |
| 190 | |
| 191 | @app.route("/tests/<test_name>", methods=['POST']) |
| 192 | def add_test(test_name): |
| 193 | tests = json.loads(request.data) |
| 194 | |
| 195 | if not hasattr(g, "storage"): |
gstepanov | f8f01c1 | 2015-02-09 18:34:01 +0200 | [diff] [blame] | 196 | path = "file://" + TEST_PATH + '/' + test_name + ".json" |
| 197 | print path |
| 198 | g.storage = create_storage(path, "", "") |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 199 | |
| 200 | for test in tests: |
| 201 | g.storage.store(test) |
| 202 | return "Created", 201 |
| 203 | |
| 204 | |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 205 | if __name__ == "__main__": |
gstepanov | 81a52ee | 2015-02-10 16:22:20 +0200 | [diff] [blame] | 206 | logger = getLogger("logger") |
| 207 | app.logger.setLevel(INFO) |
| 208 | app.logger.addHandler(logger) |
gstepanov | 6f7167d | 2015-02-06 12:51:58 +0200 | [diff] [blame] | 209 | app.run(host='0.0.0.0', debug=True) |