gstepanov | 94531b8 | 2015-02-11 14:20:34 +0200 | [diff] [blame] | 1 | from urlparse import urlparse |
gstepanov | 6ce6bf0 | 2015-02-16 19:04:14 +0200 | [diff] [blame^] | 2 | from flask import Flask, render_template, url_for, request, g, make_response |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 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 | 8053b01 | 2015-02-16 17:25:27 +0200 | [diff] [blame] | 6 | from storage_api import 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 | 6ce6bf0 | 2015-02-16 19:04:14 +0200 | [diff] [blame^] | 11 | import math |
gstepanov | 94531b8 | 2015-02-11 14:20:34 +0200 | [diff] [blame] | 12 | from web_app.keystone import KeystoneAuth |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 13 | |
| 14 | app = Flask(__name__) |
| 15 | Bootstrap(app) |
| 16 | |
| 17 | |
gstepanov | 3ce37b8 | 2015-02-12 16:20:56 +0200 | [diff] [blame] | 18 | def get_resource_as_string(name, charset='utf-8'): |
| 19 | with app.open_resource(name) as f: |
| 20 | return f.read().decode(charset) |
| 21 | |
| 22 | app.jinja_env.globals['get_resource_as_string'] = get_resource_as_string |
| 23 | |
| 24 | |
gstepanov | 9ddc18f | 2015-02-11 20:28:59 +0200 | [diff] [blame] | 25 | def load_test(test_name): |
| 26 | test_name += '.json' |
| 27 | |
| 28 | with open(TEST_PATH + "/" + test_name, 'rt') as f: |
| 29 | raw = f.read() |
| 30 | |
| 31 | if raw != "": |
| 32 | test = json.loads(raw) |
| 33 | else: |
| 34 | test = [] |
| 35 | import time |
| 36 | creation_time = os.path.getmtime(TEST_PATH + "/" + test_name) |
| 37 | |
| 38 | for t in test: |
| 39 | t['date'] = time.ctime(creation_time) |
| 40 | |
| 41 | return test |
| 42 | |
| 43 | |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 44 | def collect_tests(): |
| 45 | result = [] |
| 46 | |
gstepanov | c3ffac7 | 2015-02-05 15:39:45 +0200 | [diff] [blame] | 47 | for file in os.listdir(TEST_PATH): |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 48 | if file.endswith(".json"): |
| 49 | result.append(file.split('.')[0]) |
| 50 | |
| 51 | return result |
| 52 | |
| 53 | |
gstepanov | 6ce6bf0 | 2015-02-16 19:04:14 +0200 | [diff] [blame^] | 54 | def mean(l): |
| 55 | n = len(l) |
| 56 | |
| 57 | return sum(l) / n |
| 58 | |
| 59 | |
| 60 | def stdev(l): |
| 61 | m = mean(l) |
| 62 | return math.sqrt(sum(map(lambda x: (x - m) ** 2, l))) |
| 63 | |
| 64 | |
| 65 | def prepare_build_data(build): |
| 66 | for item in build.items(): |
| 67 | if type(item[1]) is list: |
| 68 | m = mean(item[1]) |
| 69 | s = stdev(item[1]) |
| 70 | build[item[0]] = [m, s] |
| 71 | |
| 72 | |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 73 | def collect_builds(): |
| 74 | builds = [] |
| 75 | build_set = set() |
| 76 | tests = collect_tests() |
| 77 | |
| 78 | for t in tests: |
| 79 | test = load_test(t) |
| 80 | |
| 81 | for build in test: |
| 82 | if build["type"] not in build_set: |
| 83 | build_set.add(build["type"]) |
| 84 | builds.append(build) |
| 85 | |
gstepanov | 6ce6bf0 | 2015-02-16 19:04:14 +0200 | [diff] [blame^] | 86 | for build in builds: |
| 87 | prepare_build_data(build) |
| 88 | |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 89 | return builds |
| 90 | |
| 91 | |
gstepanov | 9eb8bf3 | 2015-02-11 20:13:14 +0200 | [diff] [blame] | 92 | def builds_list(): |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 93 | data = [] |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 94 | |
| 95 | for build in collect_builds(): |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 96 | d = {} |
gstepanov | 612159f | 2015-02-12 17:15:29 +0200 | [diff] [blame] | 97 | d["type"] = build['type'] |
gstepanov | a0cb925 | 2015-02-12 17:53:31 +0200 | [diff] [blame] | 98 | d["url"] = url_for("render_test", test_name=build['name']) |
gstepanov | 9eb8bf3 | 2015-02-11 20:13:14 +0200 | [diff] [blame] | 99 | d["date"] = build['date'] |
gstepanov | dff5ad9 | 2015-02-12 17:13:09 +0200 | [diff] [blame] | 100 | d["name"] = build['name'] |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 101 | data.append(d) |
| 102 | |
gstepanov | 9eb8bf3 | 2015-02-11 20:13:14 +0200 | [diff] [blame] | 103 | return data |
| 104 | |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 105 | |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 106 | def create_measurement(build): |
| 107 | m = Measurement() |
| 108 | m.build = build.pop("build_id") |
| 109 | m.build_type = build.pop("type") |
| 110 | m.md5 = build.pop("iso_md5") |
gstepanov | 9eb8bf3 | 2015-02-11 20:13:14 +0200 | [diff] [blame] | 111 | m.date = build.pop("date") |
gstepanov | dff5ad9 | 2015-02-12 17:13:09 +0200 | [diff] [blame] | 112 | m.date = build.pop("name") |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 113 | m.results = {k: v for k, v in build.items()} |
| 114 | |
| 115 | return m |
| 116 | |
| 117 | |
gstepanov | e1ee9dc | 2015-02-11 18:09:46 +0200 | [diff] [blame] | 118 | def total_lab_info(data): |
| 119 | d = {} |
| 120 | d['nodes_count'] = len(data['nodes']) |
| 121 | d['total_memory'] = 0 |
| 122 | d['total_disk'] = 0 |
| 123 | d['processor_count'] = 0 |
| 124 | |
| 125 | for node in data['nodes']: |
| 126 | d['total_memory'] += node['memory']['total'] |
| 127 | d['processor_count'] += len(node['processors']) |
| 128 | |
| 129 | for disk in node['disks']: |
| 130 | d['total_disk'] += disk['size'] |
| 131 | |
gstepanov | 9eb8bf3 | 2015-02-11 20:13:14 +0200 | [diff] [blame] | 132 | to_gb = lambda x: x / (1024 ** 3) |
| 133 | d['total_memory'] = format(to_gb(d['total_memory']), ',d') |
| 134 | d['total_disk'] = format(to_gb(d['total_disk']), ',d') |
gstepanov | e1ee9dc | 2015-02-11 18:09:46 +0200 | [diff] [blame] | 135 | return d |
| 136 | |
gstepanov | 9eb8bf3 | 2015-02-11 20:13:14 +0200 | [diff] [blame] | 137 | |
gstepanov | 9ddc18f | 2015-02-11 20:28:59 +0200 | [diff] [blame] | 138 | def collect_lab_data(meta): |
| 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="") |
| 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 | 6c5deb3 | 2015-02-12 19:25:46 +0200 | [diff] [blame] | 179 | def merge_builds(b1, b2): |
| 180 | d = {} |
| 181 | |
| 182 | for pair in b2.items(): |
gstepanov | 8053b01 | 2015-02-16 17:25:27 +0200 | [diff] [blame] | 183 | if pair[0] in b1 and type(pair[1]) is list: |
| 184 | b1[pair[0]].extend(pair[1]) |
| 185 | else: |
| 186 | b1[pair[0]] = pair[1] |
gstepanov | 6c5deb3 | 2015-02-12 19:25:46 +0200 | [diff] [blame] | 187 | |
| 188 | |
gstepanov | 9ddc18f | 2015-02-11 20:28:59 +0200 | [diff] [blame] | 189 | @app.route("/", methods=['GET', 'POST']) |
| 190 | def index(): |
| 191 | data = builds_list() |
| 192 | return render_template("index.html", tests=data) |
| 193 | |
| 194 | |
gstepanov | 6ce6bf0 | 2015-02-16 19:04:14 +0200 | [diff] [blame^] | 195 | @app.route("/images/<image_name>") |
| 196 | def get_image(image_name): |
| 197 | with open("static/images/" + image_name, 'rb') as f: |
| 198 | image_binary = f.read() |
| 199 | |
| 200 | response = make_response(image_binary) |
| 201 | response.headers['Content-Type'] = 'image/png' |
| 202 | response.headers['Content-Disposition'] = 'attachment; filename=img.png' |
| 203 | |
| 204 | return response |
| 205 | |
| 206 | |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 207 | @app.route("/tests/<test_name>", methods=['GET']) |
| 208 | def render_test(test_name): |
gstepanov | 9eb8bf3 | 2015-02-11 20:13:14 +0200 | [diff] [blame] | 209 | tests = [] |
| 210 | header_keys = ['build_id', 'iso_md5', 'type', 'date'] |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 211 | table = [[]] |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 212 | builds = collect_builds() |
gstepanov | a0cb925 | 2015-02-12 17:53:31 +0200 | [diff] [blame] | 213 | |
| 214 | l = filter(lambda x: x['name'] == test_name, builds) |
| 215 | |
| 216 | if l[0]['type'] == 'GA': |
| 217 | builds = filter(lambda x: x['type'] == 'GA', builds) |
| 218 | else: |
| 219 | l.extend(filter(lambda x: x['type'] in ['GA', 'master'] and x not in l, builds)) |
| 220 | builds = l |
| 221 | |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 222 | results = {} |
gstepanov | e1ee9dc | 2015-02-11 18:09:46 +0200 | [diff] [blame] | 223 | meta = {"__meta__": "http://172.16.52.112:8000/api/nodes"} |
| 224 | data = collect_lab_data(meta) |
| 225 | lab_meta = total_lab_info(data) |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 226 | |
| 227 | for build in builds: |
gstepanov | a0cb925 | 2015-02-12 17:53:31 +0200 | [diff] [blame] | 228 | type = build['type'] |
| 229 | m = create_measurement(build) |
| 230 | results[type] = m |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 231 | |
gstepanov | 200d3b4 | 2015-02-06 14:01:49 +0200 | [diff] [blame] | 232 | bars = build_vertical_bar(results) |
| 233 | lines = build_lines_chart(results) |
| 234 | urls = bars + lines |
gstepanov | 6ce6bf0 | 2015-02-16 19:04:14 +0200 | [diff] [blame^] | 235 | |
| 236 | urls = [url_for("get_image", image_name=os.path.basename(url)) if not url.startswith('http') else url for url in urls] |
| 237 | |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 238 | if len(tests) > 0: |
| 239 | sorted_keys = sorted(tests[0].keys()) |
| 240 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 241 | for key in sorted_keys: |
| 242 | if key not in header_keys: |
| 243 | header_keys.append(key) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 244 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 245 | for test in tests: |
| 246 | row = [] |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 247 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 248 | for header in header_keys: |
| 249 | if isinstance(test[header], list): |
| 250 | row.append(str(test[header][0]) + unichr(0x00B1) + str(test[header][1])) |
| 251 | else: |
| 252 | row.append(test[header]) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 253 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 254 | table.append(row) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 255 | |
gstepanov | 9eb8bf3 | 2015-02-11 20:13:14 +0200 | [diff] [blame] | 256 | return render_template("test.html", urls=urls, table_url=url_for('render_table', test_name=test_name), |
| 257 | index_url=url_for('index'), lab_meta=lab_meta) |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 258 | |
| 259 | |
| 260 | @app.route("/tests/table/<test_name>/") |
| 261 | def render_table(test_name): |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 262 | builds = collect_builds() |
gstepanov | 6e4a7eb | 2015-02-12 17:57:59 +0200 | [diff] [blame] | 263 | l = filter(lambda x: x['name'] == test_name, builds) |
| 264 | if l[0]['type'] == 'GA': |
| 265 | builds = filter(lambda x: x['type'] == 'GA', builds) |
gstepanov | 3ce37b8 | 2015-02-12 16:20:56 +0200 | [diff] [blame] | 266 | else: |
gstepanov | 6e4a7eb | 2015-02-12 17:57:59 +0200 | [diff] [blame] | 267 | l.extend(filter(lambda x: x['type'] in ['GA', 'master'] and x not in l, builds)) |
| 268 | builds = l |
gstepanov | 3ce37b8 | 2015-02-12 16:20:56 +0200 | [diff] [blame] | 269 | |
gstepanov | 9eb8bf3 | 2015-02-11 20:13:14 +0200 | [diff] [blame] | 270 | header_keys = ['build_id', 'iso_md5', 'type' ,'date'] |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 271 | table = [[]] |
gstepanov | 94531b8 | 2015-02-11 14:20:34 +0200 | [diff] [blame] | 272 | meta = {"__meta__": "http://172.16.52.112:8000/api/nodes"} |
gstepanov | e1ee9dc | 2015-02-11 18:09:46 +0200 | [diff] [blame] | 273 | data = collect_lab_data(meta) |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 274 | |
| 275 | if len(builds) > 0: |
| 276 | sorted_keys = sorted(builds[0].keys()) |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 277 | |
| 278 | for key in sorted_keys: |
| 279 | if key not in header_keys: |
| 280 | header_keys.append(key) |
| 281 | |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 282 | for test in builds: |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 283 | row = [] |
| 284 | |
| 285 | for header in header_keys: |
| 286 | if isinstance(test[header], list): |
| 287 | row.append(str(test[header][0]) + unichr(0x00B1) + str(test[header][1])) |
| 288 | else: |
| 289 | row.append(test[header]) |
| 290 | |
| 291 | table.append(row) |
| 292 | |
gstepanov | bb85492 | 2015-02-09 18:18:17 +0200 | [diff] [blame] | 293 | return render_template("table.html", headers=header_keys, table=table, |
| 294 | back_url=url_for('render_test', test_name=test_name), lab=data) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 295 | |
| 296 | |
gstepanov | 3ce37b8 | 2015-02-12 16:20:56 +0200 | [diff] [blame] | 297 | @app.route("/api/tests/<test_name>", methods=['POST']) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 298 | def add_test(test_name): |
gstepanov | 6c5deb3 | 2015-02-12 19:25:46 +0200 | [diff] [blame] | 299 | test = json.loads(request.data) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 300 | |
gstepanov | 6c5deb3 | 2015-02-12 19:25:46 +0200 | [diff] [blame] | 301 | file_name = TEST_PATH + '/' + 'storage' + ".json" |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 302 | |
gstepanov | 6c5deb3 | 2015-02-12 19:25:46 +0200 | [diff] [blame] | 303 | if not os.path.exists(file_name): |
| 304 | with open(file_name, "w+") as f: |
| 305 | f.write(json.dumps([])) |
| 306 | |
| 307 | builds = collect_builds() |
| 308 | res = None |
| 309 | |
| 310 | for b in builds: |
| 311 | if b['name'] == test['name']: |
| 312 | res = b |
| 313 | break |
| 314 | |
| 315 | if res is None: |
| 316 | builds.append(test) |
| 317 | else: |
| 318 | merge_builds(res, test) |
| 319 | |
| 320 | with open(TEST_PATH + '/' + 'storage' + ".json", 'w+') as f: |
| 321 | f.write(json.dumps(builds)) |
| 322 | |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 323 | return "Created", 201 |
| 324 | |
| 325 | |
gstepanov | 3ce37b8 | 2015-02-12 16:20:56 +0200 | [diff] [blame] | 326 | @app.route("/api/tests", methods=['GET']) |
| 327 | def get_all_tests(): |
| 328 | return json.dumps(collect_builds()) |
| 329 | |
| 330 | |
| 331 | @app.route("/api/tests/<test_name>", methods=['GET']) |
| 332 | def get_test(test_name): |
| 333 | builds = collect_builds() |
| 334 | |
| 335 | for build in builds: |
| 336 | if build["type"] == test_name: |
| 337 | return json.dumps(build) |
| 338 | return "Not Found", 404 |
| 339 | |
| 340 | |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 341 | if __name__ == "__main__": |
gstepanov | 81a52ee | 2015-02-10 16:22:20 +0200 | [diff] [blame] | 342 | logger = getLogger("logger") |
| 343 | app.logger.setLevel(INFO) |
| 344 | app.logger.addHandler(logger) |
gstepanov | 6f7167d | 2015-02-06 12:51:58 +0200 | [diff] [blame] | 345 | app.run(host='0.0.0.0', debug=True) |