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 | e1ee9dc | 2015-02-11 18:09:46 +0200 | [diff] [blame^] | 80 | def total_lab_info(data): |
| 81 | d = {} |
| 82 | d['nodes_count'] = len(data['nodes']) |
| 83 | d['total_memory'] = 0 |
| 84 | d['total_disk'] = 0 |
| 85 | d['processor_count'] = 0 |
| 86 | |
| 87 | for node in data['nodes']: |
| 88 | d['total_memory'] += node['memory']['total'] |
| 89 | d['processor_count'] += len(node['processors']) |
| 90 | |
| 91 | for disk in node['disks']: |
| 92 | d['total_disk'] += disk['size'] |
| 93 | |
| 94 | return d |
| 95 | |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 96 | @app.route("/tests/<test_name>", methods=['GET']) |
| 97 | def render_test(test_name): |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 98 | tests = [] #load_test(test_name) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 99 | header_keys = ['build_id', 'iso_md5', 'type'] |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 100 | table = [[]] |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 101 | builds_to_compare = ['GA', 'master', test_name] |
| 102 | builds = collect_builds() |
| 103 | results = {} |
gstepanov | e1ee9dc | 2015-02-11 18:09:46 +0200 | [diff] [blame^] | 104 | meta = {"__meta__": "http://172.16.52.112:8000/api/nodes"} |
| 105 | data = collect_lab_data(meta) |
| 106 | lab_meta = total_lab_info(data) |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 107 | |
| 108 | for build in builds: |
| 109 | if build['type'] in builds_to_compare: |
| 110 | type = build['type'] |
| 111 | m = create_measurement(build) |
| 112 | results[type] = m |
| 113 | |
gstepanov | 200d3b4 | 2015-02-06 14:01:49 +0200 | [diff] [blame] | 114 | bars = build_vertical_bar(results) |
| 115 | lines = build_lines_chart(results) |
| 116 | urls = bars + lines |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 117 | if len(tests) > 0: |
| 118 | sorted_keys = sorted(tests[0].keys()) |
| 119 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 120 | for key in sorted_keys: |
| 121 | if key not in header_keys: |
| 122 | header_keys.append(key) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 123 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 124 | for test in tests: |
| 125 | row = [] |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 126 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 127 | for header in header_keys: |
| 128 | if isinstance(test[header], list): |
| 129 | row.append(str(test[header][0]) + unichr(0x00B1) + str(test[header][1])) |
| 130 | else: |
| 131 | row.append(test[header]) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 132 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 133 | table.append(row) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 134 | |
gstepanov | e1ee9dc | 2015-02-11 18:09:46 +0200 | [diff] [blame^] | 135 | return render_template("test.html", urls=urls, table_url=url_for('render_table', test_name=test_name), lab_meta=lab_meta) |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 136 | |
| 137 | |
gstepanov | e1ee9dc | 2015-02-11 18:09:46 +0200 | [diff] [blame^] | 138 | def collect_lab_data(meta): |
gstepanov | 94531b8 | 2015-02-11 14:20:34 +0200 | [diff] [blame] | 139 | u = urlparse(meta['__meta__']) |
| 140 | cred = {"username": "admin", "password": "admin", "tenant_name": "admin"} |
| 141 | keystone = KeystoneAuth(root_url=meta['__meta__'], creds=cred, admin_node_ip=u.hostname) |
| 142 | lab_info = keystone.do(method='get', path="") |
gstepanov | bb85492 | 2015-02-09 18:18:17 +0200 | [diff] [blame] | 143 | nodes = [] |
| 144 | result = {} |
| 145 | |
| 146 | for node in lab_info: |
| 147 | d = {} |
| 148 | d['name'] = node['name'] |
| 149 | p = [] |
| 150 | i = [] |
| 151 | disks = [] |
| 152 | devices = [] |
| 153 | |
| 154 | for processor in node['meta']['cpu']['spec']: |
| 155 | p.append(processor) |
| 156 | |
| 157 | for iface in node['meta']['interfaces']: |
| 158 | i.append(iface) |
| 159 | |
| 160 | m = node['meta']['memory'].copy() |
| 161 | |
| 162 | for disk in node['meta']['disks']: |
| 163 | disks.append(disk) |
| 164 | |
| 165 | d['memory'] = m |
| 166 | d['disks'] = disks |
| 167 | d['devices'] = devices |
| 168 | d['interfaces'] = i |
| 169 | d['processors'] = p |
| 170 | |
| 171 | nodes.append(d) |
| 172 | |
| 173 | result['nodes'] = nodes |
| 174 | result['name'] = 'Perf-1 Env' |
| 175 | |
| 176 | return result |
| 177 | |
| 178 | |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 179 | @app.route("/tests/table/<test_name>/") |
| 180 | def render_table(test_name): |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 181 | builds = collect_builds() |
| 182 | builds = filter(lambda x: x["type"] in ['GA', 'master', test_name], builds) |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 183 | header_keys = ['build_id', 'iso_md5', 'type'] |
| 184 | table = [[]] |
gstepanov | 94531b8 | 2015-02-11 14:20:34 +0200 | [diff] [blame] | 185 | meta = {"__meta__": "http://172.16.52.112:8000/api/nodes"} |
gstepanov | e1ee9dc | 2015-02-11 18:09:46 +0200 | [diff] [blame^] | 186 | data = collect_lab_data(meta) |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 187 | |
| 188 | if len(builds) > 0: |
| 189 | sorted_keys = sorted(builds[0].keys()) |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 190 | |
| 191 | for key in sorted_keys: |
| 192 | if key not in header_keys: |
| 193 | header_keys.append(key) |
| 194 | |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 195 | for test in builds: |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 196 | row = [] |
| 197 | |
| 198 | for header in header_keys: |
| 199 | if isinstance(test[header], list): |
| 200 | row.append(str(test[header][0]) + unichr(0x00B1) + str(test[header][1])) |
| 201 | else: |
| 202 | row.append(test[header]) |
| 203 | |
| 204 | table.append(row) |
| 205 | |
gstepanov | bb85492 | 2015-02-09 18:18:17 +0200 | [diff] [blame] | 206 | return render_template("table.html", headers=header_keys, table=table, |
| 207 | back_url=url_for('render_test', test_name=test_name), lab=data) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 208 | |
| 209 | |
| 210 | @app.route("/tests/<test_name>", methods=['POST']) |
| 211 | def add_test(test_name): |
| 212 | tests = json.loads(request.data) |
| 213 | |
| 214 | if not hasattr(g, "storage"): |
gstepanov | f8f01c1 | 2015-02-09 18:34:01 +0200 | [diff] [blame] | 215 | path = "file://" + TEST_PATH + '/' + test_name + ".json" |
| 216 | print path |
| 217 | g.storage = create_storage(path, "", "") |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 218 | |
| 219 | for test in tests: |
| 220 | g.storage.store(test) |
| 221 | return "Created", 201 |
| 222 | |
| 223 | |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 224 | if __name__ == "__main__": |
gstepanov | 81a52ee | 2015-02-10 16:22:20 +0200 | [diff] [blame] | 225 | logger = getLogger("logger") |
| 226 | app.logger.setLevel(INFO) |
| 227 | app.logger.addHandler(logger) |
gstepanov | 6f7167d | 2015-02-06 12:51:58 +0200 | [diff] [blame] | 228 | app.run(host='0.0.0.0', debug=True) |