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 | 725cc30 | 2015-02-09 15:08:06 +0200 | [diff] [blame] | 6 | from storage_api import create_storage |
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 | |
| 27 | def load_test(test_name): |
| 28 | test_name += '.json' |
| 29 | |
gstepanov | 3501ad4 | 2015-02-05 15:41:39 +0200 | [diff] [blame] | 30 | with open(TEST_PATH + "/" + test_name, 'rt') as f: |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 31 | raw = f.read() |
| 32 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 33 | if raw != "": |
| 34 | test = json.loads(raw) |
| 35 | else: |
| 36 | test = [] |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 37 | |
| 38 | return test |
| 39 | |
| 40 | |
| 41 | @app.route("/", methods=['GET', 'POST']) |
| 42 | def index(): |
| 43 | data = [] |
| 44 | for test in collect_tests(): |
| 45 | d = {} |
| 46 | d["name"] = test |
| 47 | d["url"] = url_for("render_test", test_name=test) |
| 48 | data.append(d) |
| 49 | |
| 50 | return render_template("index.html", tests=data) |
| 51 | |
| 52 | |
| 53 | @app.route("/tests/<test_name>", methods=['GET']) |
| 54 | def render_test(test_name): |
| 55 | tests = load_test(test_name) |
| 56 | header_keys = ['build_id', 'iso_md5', 'type'] |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 57 | table = [[]] |
gstepanov | 200d3b4 | 2015-02-06 14:01:49 +0200 | [diff] [blame] | 58 | storage = create_storage('file://' + TEST_PATH + '/' + test_name + '.json') |
| 59 | results = storage.recent_builds() |
gstepanov | 200d3b4 | 2015-02-06 14:01:49 +0200 | [diff] [blame] | 60 | bars = build_vertical_bar(results) |
| 61 | lines = build_lines_chart(results) |
| 62 | urls = bars + lines |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 63 | if len(tests) > 0: |
| 64 | sorted_keys = sorted(tests[0].keys()) |
| 65 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 66 | for key in sorted_keys: |
| 67 | if key not in header_keys: |
| 68 | header_keys.append(key) |
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 test in tests: |
| 71 | row = [] |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 72 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 73 | for header in header_keys: |
| 74 | if isinstance(test[header], list): |
| 75 | row.append(str(test[header][0]) + unichr(0x00B1) + str(test[header][1])) |
| 76 | else: |
| 77 | row.append(test[header]) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 78 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame] | 79 | table.append(row) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 80 | |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 81 | return render_template("test.html", urls=urls, table_url=url_for('render_table', test_name=test_name)) |
| 82 | |
| 83 | |
gstepanov | bb85492 | 2015-02-09 18:18:17 +0200 | [diff] [blame] | 84 | def collect_lab_data(tests, meta): |
gstepanov | 94531b8 | 2015-02-11 14:20:34 +0200 | [diff] [blame^] | 85 | u = urlparse(meta['__meta__']) |
| 86 | cred = {"username": "admin", "password": "admin", "tenant_name": "admin"} |
| 87 | keystone = KeystoneAuth(root_url=meta['__meta__'], creds=cred, admin_node_ip=u.hostname) |
| 88 | lab_info = keystone.do(method='get', path="") |
gstepanov | bb85492 | 2015-02-09 18:18:17 +0200 | [diff] [blame] | 89 | nodes = [] |
| 90 | result = {} |
| 91 | |
| 92 | for node in lab_info: |
| 93 | d = {} |
| 94 | d['name'] = node['name'] |
| 95 | p = [] |
| 96 | i = [] |
| 97 | disks = [] |
| 98 | devices = [] |
| 99 | |
| 100 | for processor in node['meta']['cpu']['spec']: |
| 101 | p.append(processor) |
| 102 | |
| 103 | for iface in node['meta']['interfaces']: |
| 104 | i.append(iface) |
| 105 | |
| 106 | m = node['meta']['memory'].copy() |
| 107 | |
| 108 | for disk in node['meta']['disks']: |
| 109 | disks.append(disk) |
| 110 | |
| 111 | d['memory'] = m |
| 112 | d['disks'] = disks |
| 113 | d['devices'] = devices |
| 114 | d['interfaces'] = i |
| 115 | d['processors'] = p |
| 116 | |
| 117 | nodes.append(d) |
| 118 | |
| 119 | result['nodes'] = nodes |
| 120 | result['name'] = 'Perf-1 Env' |
| 121 | |
| 122 | return result |
| 123 | |
| 124 | |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 125 | @app.route("/tests/table/<test_name>/") |
| 126 | def render_table(test_name): |
| 127 | tests = load_test(test_name) |
| 128 | header_keys = ['build_id', 'iso_md5', 'type'] |
| 129 | table = [[]] |
gstepanov | 94531b8 | 2015-02-11 14:20:34 +0200 | [diff] [blame^] | 130 | meta = {"__meta__": "http://172.16.52.112:8000/api/nodes"} |
| 131 | data = collect_lab_data(tests, meta) |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 132 | if len(tests) > 0: |
| 133 | sorted_keys = sorted(tests[0].keys()) |
| 134 | |
| 135 | for key in sorted_keys: |
| 136 | if key not in header_keys: |
| 137 | header_keys.append(key) |
| 138 | |
| 139 | for test in tests: |
| 140 | row = [] |
| 141 | |
| 142 | for header in header_keys: |
| 143 | if isinstance(test[header], list): |
| 144 | row.append(str(test[header][0]) + unichr(0x00B1) + str(test[header][1])) |
| 145 | else: |
| 146 | row.append(test[header]) |
| 147 | |
| 148 | table.append(row) |
| 149 | |
gstepanov | bb85492 | 2015-02-09 18:18:17 +0200 | [diff] [blame] | 150 | return render_template("table.html", headers=header_keys, table=table, |
| 151 | back_url=url_for('render_test', test_name=test_name), lab=data) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 152 | |
| 153 | |
| 154 | @app.route("/tests/<test_name>", methods=['POST']) |
| 155 | def add_test(test_name): |
| 156 | tests = json.loads(request.data) |
| 157 | |
| 158 | if not hasattr(g, "storage"): |
gstepanov | f8f01c1 | 2015-02-09 18:34:01 +0200 | [diff] [blame] | 159 | path = "file://" + TEST_PATH + '/' + test_name + ".json" |
| 160 | print path |
| 161 | g.storage = create_storage(path, "", "") |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 162 | |
| 163 | for test in tests: |
| 164 | g.storage.store(test) |
| 165 | return "Created", 201 |
| 166 | |
| 167 | |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 168 | if __name__ == "__main__": |
gstepanov | 81a52ee | 2015-02-10 16:22:20 +0200 | [diff] [blame] | 169 | logger = getLogger("logger") |
| 170 | app.logger.setLevel(INFO) |
| 171 | app.logger.addHandler(logger) |
gstepanov | 6f7167d | 2015-02-06 12:51:58 +0200 | [diff] [blame] | 172 | app.run(host='0.0.0.0', debug=True) |