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 | |
gstepanov | 3ce37b8 | 2015-02-12 16:20:56 +0200 | [diff] [blame] | 17 | def get_resource_as_string(name, charset='utf-8'): |
| 18 | with app.open_resource(name) as f: |
| 19 | return f.read().decode(charset) |
| 20 | |
| 21 | app.jinja_env.globals['get_resource_as_string'] = get_resource_as_string |
| 22 | |
| 23 | |
gstepanov | 9ddc18f | 2015-02-11 20:28:59 +0200 | [diff] [blame] | 24 | def load_test(test_name): |
| 25 | test_name += '.json' |
| 26 | |
| 27 | with open(TEST_PATH + "/" + test_name, 'rt') as f: |
| 28 | raw = f.read() |
| 29 | |
| 30 | if raw != "": |
| 31 | test = json.loads(raw) |
| 32 | else: |
| 33 | test = [] |
| 34 | import time |
| 35 | creation_time = os.path.getmtime(TEST_PATH + "/" + test_name) |
| 36 | |
| 37 | for t in test: |
| 38 | t['date'] = time.ctime(creation_time) |
| 39 | |
| 40 | return test |
| 41 | |
| 42 | |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 43 | def collect_tests(): |
| 44 | result = [] |
| 45 | |
gstepanov | c3ffac7 | 2015-02-05 15:39:45 +0200 | [diff] [blame] | 46 | for file in os.listdir(TEST_PATH): |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 47 | if file.endswith(".json"): |
| 48 | result.append(file.split('.')[0]) |
| 49 | |
| 50 | return result |
| 51 | |
| 52 | |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 53 | def collect_builds(): |
| 54 | builds = [] |
| 55 | build_set = set() |
| 56 | tests = collect_tests() |
| 57 | |
| 58 | for t in tests: |
| 59 | test = load_test(t) |
| 60 | |
| 61 | for build in test: |
| 62 | if build["type"] not in build_set: |
| 63 | build_set.add(build["type"]) |
| 64 | builds.append(build) |
| 65 | |
| 66 | return builds |
| 67 | |
| 68 | |
gstepanov | 9eb8bf3 | 2015-02-11 20:13:14 +0200 | [diff] [blame] | 69 | def builds_list(): |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 70 | data = [] |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 71 | |
| 72 | for build in collect_builds(): |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 73 | d = {} |
gstepanov | 612159f | 2015-02-12 17:15:29 +0200 | [diff] [blame] | 74 | d["type"] = build['type'] |
gstepanov | a0cb925 | 2015-02-12 17:53:31 +0200 | [diff] [blame] | 75 | d["url"] = url_for("render_test", test_name=build['name']) |
gstepanov | 9eb8bf3 | 2015-02-11 20:13:14 +0200 | [diff] [blame] | 76 | d["date"] = build['date'] |
gstepanov | dff5ad9 | 2015-02-12 17:13:09 +0200 | [diff] [blame] | 77 | d["name"] = build['name'] |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 78 | data.append(d) |
| 79 | |
gstepanov | 9eb8bf3 | 2015-02-11 20:13:14 +0200 | [diff] [blame] | 80 | return data |
| 81 | |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 82 | |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 83 | def create_measurement(build): |
| 84 | m = Measurement() |
| 85 | m.build = build.pop("build_id") |
| 86 | m.build_type = build.pop("type") |
| 87 | m.md5 = build.pop("iso_md5") |
gstepanov | 9eb8bf3 | 2015-02-11 20:13:14 +0200 | [diff] [blame] | 88 | m.date = build.pop("date") |
gstepanov | dff5ad9 | 2015-02-12 17:13:09 +0200 | [diff] [blame] | 89 | m.date = build.pop("name") |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 90 | m.results = {k: v for k, v in build.items()} |
| 91 | |
| 92 | return m |
| 93 | |
| 94 | |
gstepanov | e1ee9dc | 2015-02-11 18:09:46 +0200 | [diff] [blame] | 95 | def total_lab_info(data): |
| 96 | d = {} |
| 97 | d['nodes_count'] = len(data['nodes']) |
| 98 | d['total_memory'] = 0 |
| 99 | d['total_disk'] = 0 |
| 100 | d['processor_count'] = 0 |
| 101 | |
| 102 | for node in data['nodes']: |
| 103 | d['total_memory'] += node['memory']['total'] |
| 104 | d['processor_count'] += len(node['processors']) |
| 105 | |
| 106 | for disk in node['disks']: |
| 107 | d['total_disk'] += disk['size'] |
| 108 | |
gstepanov | 9eb8bf3 | 2015-02-11 20:13:14 +0200 | [diff] [blame] | 109 | to_gb = lambda x: x / (1024 ** 3) |
| 110 | d['total_memory'] = format(to_gb(d['total_memory']), ',d') |
| 111 | d['total_disk'] = format(to_gb(d['total_disk']), ',d') |
gstepanov | e1ee9dc | 2015-02-11 18:09:46 +0200 | [diff] [blame] | 112 | return d |
| 113 | |
gstepanov | 9eb8bf3 | 2015-02-11 20:13:14 +0200 | [diff] [blame] | 114 | |
gstepanov | 9ddc18f | 2015-02-11 20:28:59 +0200 | [diff] [blame] | 115 | def collect_lab_data(meta): |
| 116 | u = urlparse(meta['__meta__']) |
| 117 | cred = {"username": "admin", "password": "admin", "tenant_name": "admin"} |
| 118 | keystone = KeystoneAuth(root_url=meta['__meta__'], creds=cred, admin_node_ip=u.hostname) |
| 119 | lab_info = keystone.do(method='get', path="") |
| 120 | nodes = [] |
| 121 | result = {} |
| 122 | |
| 123 | for node in lab_info: |
| 124 | d = {} |
| 125 | d['name'] = node['name'] |
| 126 | p = [] |
| 127 | i = [] |
| 128 | disks = [] |
| 129 | devices = [] |
| 130 | |
| 131 | for processor in node['meta']['cpu']['spec']: |
| 132 | p.append(processor) |
| 133 | |
| 134 | for iface in node['meta']['interfaces']: |
| 135 | i.append(iface) |
| 136 | |
| 137 | m = node['meta']['memory'].copy() |
| 138 | |
| 139 | for disk in node['meta']['disks']: |
| 140 | disks.append(disk) |
| 141 | |
| 142 | d['memory'] = m |
| 143 | d['disks'] = disks |
| 144 | d['devices'] = devices |
| 145 | d['interfaces'] = i |
| 146 | d['processors'] = p |
| 147 | |
| 148 | nodes.append(d) |
| 149 | |
| 150 | result['nodes'] = nodes |
| 151 | result['name'] = 'Perf-1 Env' |
| 152 | |
| 153 | return result |
| 154 | |
| 155 | |
gstepanov | 6c5deb3 | 2015-02-12 19:25:46 +0200 | [diff] [blame^] | 156 | def merge_builds(b1, b2): |
| 157 | d = {} |
| 158 | |
| 159 | for pair in b2.items(): |
| 160 | b1[pair[0]] = pair[1] |
| 161 | |
| 162 | |
gstepanov | 9ddc18f | 2015-02-11 20:28:59 +0200 | [diff] [blame] | 163 | @app.route("/", methods=['GET', 'POST']) |
| 164 | def index(): |
| 165 | data = builds_list() |
| 166 | return render_template("index.html", tests=data) |
| 167 | |
| 168 | |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 169 | @app.route("/tests/<test_name>", methods=['GET']) |
| 170 | def render_test(test_name): |
gstepanov | 9eb8bf3 | 2015-02-11 20:13:14 +0200 | [diff] [blame] | 171 | tests = [] |
| 172 | header_keys = ['build_id', 'iso_md5', 'type', 'date'] |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 173 | table = [[]] |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 174 | builds = collect_builds() |
gstepanov | a0cb925 | 2015-02-12 17:53:31 +0200 | [diff] [blame] | 175 | |
| 176 | l = filter(lambda x: x['name'] == test_name, builds) |
| 177 | |
| 178 | if l[0]['type'] == 'GA': |
| 179 | builds = filter(lambda x: x['type'] == 'GA', builds) |
| 180 | else: |
| 181 | l.extend(filter(lambda x: x['type'] in ['GA', 'master'] and x not in l, builds)) |
| 182 | builds = l |
| 183 | |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 184 | results = {} |
gstepanov | e1ee9dc | 2015-02-11 18:09:46 +0200 | [diff] [blame] | 185 | meta = {"__meta__": "http://172.16.52.112:8000/api/nodes"} |
| 186 | data = collect_lab_data(meta) |
| 187 | lab_meta = total_lab_info(data) |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 188 | |
| 189 | for build in builds: |
gstepanov | a0cb925 | 2015-02-12 17:53:31 +0200 | [diff] [blame] | 190 | type = build['type'] |
| 191 | m = create_measurement(build) |
| 192 | results[type] = m |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 193 | |
gstepanov | 200d3b4 | 2015-02-06 14:01:49 +0200 | [diff] [blame] | 194 | bars = build_vertical_bar(results) |
| 195 | lines = build_lines_chart(results) |
| 196 | urls = bars + lines |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 197 | if len(tests) > 0: |
| 198 | sorted_keys = sorted(tests[0].keys()) |
| 199 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 200 | for key in sorted_keys: |
| 201 | if key not in header_keys: |
| 202 | header_keys.append(key) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 203 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 204 | for test in tests: |
| 205 | row = [] |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 206 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 207 | for header in header_keys: |
| 208 | if isinstance(test[header], list): |
| 209 | row.append(str(test[header][0]) + unichr(0x00B1) + str(test[header][1])) |
| 210 | else: |
| 211 | row.append(test[header]) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 212 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 213 | table.append(row) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 214 | |
gstepanov | 9eb8bf3 | 2015-02-11 20:13:14 +0200 | [diff] [blame] | 215 | return render_template("test.html", urls=urls, table_url=url_for('render_table', test_name=test_name), |
| 216 | index_url=url_for('index'), lab_meta=lab_meta) |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 217 | |
| 218 | |
| 219 | @app.route("/tests/table/<test_name>/") |
| 220 | def render_table(test_name): |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 221 | builds = collect_builds() |
gstepanov | 6e4a7eb | 2015-02-12 17:57:59 +0200 | [diff] [blame] | 222 | l = filter(lambda x: x['name'] == test_name, builds) |
| 223 | if l[0]['type'] == 'GA': |
| 224 | builds = filter(lambda x: x['type'] == 'GA', builds) |
gstepanov | 3ce37b8 | 2015-02-12 16:20:56 +0200 | [diff] [blame] | 225 | else: |
gstepanov | 6e4a7eb | 2015-02-12 17:57:59 +0200 | [diff] [blame] | 226 | l.extend(filter(lambda x: x['type'] in ['GA', 'master'] and x not in l, builds)) |
| 227 | builds = l |
gstepanov | 3ce37b8 | 2015-02-12 16:20:56 +0200 | [diff] [blame] | 228 | |
gstepanov | 9eb8bf3 | 2015-02-11 20:13:14 +0200 | [diff] [blame] | 229 | header_keys = ['build_id', 'iso_md5', 'type' ,'date'] |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 230 | table = [[]] |
gstepanov | 94531b8 | 2015-02-11 14:20:34 +0200 | [diff] [blame] | 231 | meta = {"__meta__": "http://172.16.52.112:8000/api/nodes"} |
gstepanov | e1ee9dc | 2015-02-11 18:09:46 +0200 | [diff] [blame] | 232 | data = collect_lab_data(meta) |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 233 | |
| 234 | if len(builds) > 0: |
| 235 | sorted_keys = sorted(builds[0].keys()) |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 236 | |
| 237 | for key in sorted_keys: |
| 238 | if key not in header_keys: |
| 239 | header_keys.append(key) |
| 240 | |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 241 | for test in builds: |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 242 | row = [] |
| 243 | |
| 244 | for header in header_keys: |
| 245 | if isinstance(test[header], list): |
| 246 | row.append(str(test[header][0]) + unichr(0x00B1) + str(test[header][1])) |
| 247 | else: |
| 248 | row.append(test[header]) |
| 249 | |
| 250 | table.append(row) |
| 251 | |
gstepanov | bb85492 | 2015-02-09 18:18:17 +0200 | [diff] [blame] | 252 | return render_template("table.html", headers=header_keys, table=table, |
| 253 | back_url=url_for('render_test', test_name=test_name), lab=data) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 254 | |
| 255 | |
gstepanov | 3ce37b8 | 2015-02-12 16:20:56 +0200 | [diff] [blame] | 256 | @app.route("/api/tests/<test_name>", methods=['POST']) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 257 | def add_test(test_name): |
gstepanov | 6c5deb3 | 2015-02-12 19:25:46 +0200 | [diff] [blame^] | 258 | test = json.loads(request.data) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 259 | |
gstepanov | 6c5deb3 | 2015-02-12 19:25:46 +0200 | [diff] [blame^] | 260 | file_name = TEST_PATH + '/' + 'storage' + ".json" |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 261 | |
gstepanov | 6c5deb3 | 2015-02-12 19:25:46 +0200 | [diff] [blame^] | 262 | if not os.path.exists(file_name): |
| 263 | with open(file_name, "w+") as f: |
| 264 | f.write(json.dumps([])) |
| 265 | |
| 266 | builds = collect_builds() |
| 267 | res = None |
| 268 | |
| 269 | for b in builds: |
| 270 | if b['name'] == test['name']: |
| 271 | res = b |
| 272 | break |
| 273 | |
| 274 | if res is None: |
| 275 | builds.append(test) |
| 276 | else: |
| 277 | merge_builds(res, test) |
| 278 | |
| 279 | with open(TEST_PATH + '/' + 'storage' + ".json", 'w+') as f: |
| 280 | f.write(json.dumps(builds)) |
| 281 | |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 282 | return "Created", 201 |
| 283 | |
| 284 | |
gstepanov | 3ce37b8 | 2015-02-12 16:20:56 +0200 | [diff] [blame] | 285 | @app.route("/api/tests", methods=['GET']) |
| 286 | def get_all_tests(): |
| 287 | return json.dumps(collect_builds()) |
| 288 | |
| 289 | |
| 290 | @app.route("/api/tests/<test_name>", methods=['GET']) |
| 291 | def get_test(test_name): |
| 292 | builds = collect_builds() |
| 293 | |
| 294 | for build in builds: |
| 295 | if build["type"] == test_name: |
| 296 | return json.dumps(build) |
| 297 | return "Not Found", 404 |
| 298 | |
| 299 | |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 300 | if __name__ == "__main__": |
gstepanov | 81a52ee | 2015-02-10 16:22:20 +0200 | [diff] [blame] | 301 | logger = getLogger("logger") |
| 302 | app.logger.setLevel(INFO) |
| 303 | app.logger.addHandler(logger) |
gstepanov | 6f7167d | 2015-02-06 12:51:58 +0200 | [diff] [blame] | 304 | app.run(host='0.0.0.0', debug=True) |