blob: 820be22c33a5fee7b6304744267f38563f82a1d7 [file] [log] [blame]
gstepanov94531b82015-02-11 14:20:34 +02001from urlparse import urlparse
gstepanov2164d822015-02-04 19:49:40 +02002from flask import Flask, render_template, url_for, request, g
3from flask_bootstrap import Bootstrap
gstepanov725cc302015-02-09 15:08:06 +02004from config import TEST_PATH
gstepanov200d3b42015-02-06 14:01:49 +02005from report import build_vertical_bar, build_lines_chart
gstepanov725cc302015-02-09 15:08:06 +02006from storage_api import create_storage
gstepanov81a52ee2015-02-10 16:22:20 +02007from logging import getLogger, INFO
8
9import json
10import os.path
gstepanov94531b82015-02-11 14:20:34 +020011from web_app.keystone import KeystoneAuth
gstepanov2164d822015-02-04 19:49:40 +020012
13app = Flask(__name__)
14Bootstrap(app)
15
16
17def collect_tests():
18 result = []
19
gstepanovc3ffac72015-02-05 15:39:45 +020020 for file in os.listdir(TEST_PATH):
gstepanov2164d822015-02-04 19:49:40 +020021 if file.endswith(".json"):
22 result.append(file.split('.')[0])
23
24 return result
25
26
27def load_test(test_name):
28 test_name += '.json'
29
gstepanov3501ad42015-02-05 15:41:39 +020030 with open(TEST_PATH + "/" + test_name, 'rt') as f:
gstepanov2164d822015-02-04 19:49:40 +020031 raw = f.read()
32
gstepanov0a8fdd32015-02-05 15:54:11 +020033 if raw != "":
34 test = json.loads(raw)
35 else:
36 test = []
gstepanov2164d822015-02-04 19:49:40 +020037
38 return test
39
40
41@app.route("/", methods=['GET', 'POST'])
42def 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'])
54def render_test(test_name):
55 tests = load_test(test_name)
56 header_keys = ['build_id', 'iso_md5', 'type']
gstepanov0a8fdd32015-02-05 15:54:11 +020057 table = [[]]
gstepanov200d3b42015-02-06 14:01:49 +020058 storage = create_storage('file://' + TEST_PATH + '/' + test_name + '.json')
59 results = storage.recent_builds()
gstepanov200d3b42015-02-06 14:01:49 +020060 bars = build_vertical_bar(results)
61 lines = build_lines_chart(results)
62 urls = bars + lines
gstepanov2164d822015-02-04 19:49:40 +020063 if len(tests) > 0:
64 sorted_keys = sorted(tests[0].keys())
65
gstepanov0a8fdd32015-02-05 15:54:11 +020066 for key in sorted_keys:
67 if key not in header_keys:
68 header_keys.append(key)
gstepanov2164d822015-02-04 19:49:40 +020069
gstepanov0a8fdd32015-02-05 15:54:11 +020070 for test in tests:
71 row = []
gstepanov2164d822015-02-04 19:49:40 +020072
gstepanov0a8fdd32015-02-05 15:54:11 +020073 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])
gstepanov2164d822015-02-04 19:49:40 +020078
gstepanov0a8fdd32015-02-05 15:54:11 +020079 table.append(row)
gstepanov2164d822015-02-04 19:49:40 +020080
gstepanovcd77d5a2015-02-06 14:49:34 +020081 return render_template("test.html", urls=urls, table_url=url_for('render_table', test_name=test_name))
82
83
gstepanovbb854922015-02-09 18:18:17 +020084def collect_lab_data(tests, meta):
gstepanov94531b82015-02-11 14:20:34 +020085 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="")
gstepanovbb854922015-02-09 18:18:17 +020089 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
gstepanovcd77d5a2015-02-06 14:49:34 +0200125@app.route("/tests/table/<test_name>/")
126def render_table(test_name):
127 tests = load_test(test_name)
128 header_keys = ['build_id', 'iso_md5', 'type']
129 table = [[]]
gstepanov94531b82015-02-11 14:20:34 +0200130 meta = {"__meta__": "http://172.16.52.112:8000/api/nodes"}
131 data = collect_lab_data(tests, meta)
gstepanovcd77d5a2015-02-06 14:49:34 +0200132 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
gstepanovbb854922015-02-09 18:18:17 +0200150 return render_template("table.html", headers=header_keys, table=table,
151 back_url=url_for('render_test', test_name=test_name), lab=data)
gstepanov2164d822015-02-04 19:49:40 +0200152
153
154@app.route("/tests/<test_name>", methods=['POST'])
155def add_test(test_name):
156 tests = json.loads(request.data)
157
158 if not hasattr(g, "storage"):
gstepanovf8f01c12015-02-09 18:34:01 +0200159 path = "file://" + TEST_PATH + '/' + test_name + ".json"
160 print path
161 g.storage = create_storage(path, "", "")
gstepanov2164d822015-02-04 19:49:40 +0200162
163 for test in tests:
164 g.storage.store(test)
165 return "Created", 201
166
167
gstepanov2164d822015-02-04 19:49:40 +0200168if __name__ == "__main__":
gstepanov81a52ee2015-02-10 16:22:20 +0200169 logger = getLogger("logger")
170 app.logger.setLevel(INFO)
171 app.logger.addHandler(logger)
gstepanov6f7167d2015-02-06 12:51:58 +0200172 app.run(host='0.0.0.0', debug=True)