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