blob: 3673b24fdf265011844d954c538c2186f6f4f2d8 [file] [log] [blame]
gstepanov2164d822015-02-04 19:49:40 +02001from flask import Flask, render_template, url_for, request, g
2from flask_bootstrap import Bootstrap
gstepanov725cc302015-02-09 15:08:06 +02003from config import TEST_PATH
gstepanov200d3b42015-02-06 14:01:49 +02004from report import build_vertical_bar, build_lines_chart
gstepanov725cc302015-02-09 15:08:06 +02005from storage_api import create_storage
gstepanov81a52ee2015-02-10 16:22:20 +02006from logging import getLogger, INFO
7
8import json
9import os.path
gstepanovbb854922015-02-09 18:18:17 +020010import requests
gstepanov2164d822015-02-04 19:49:40 +020011
12app = Flask(__name__)
13Bootstrap(app)
14
15
16def collect_tests():
17 result = []
18
gstepanovc3ffac72015-02-05 15:39:45 +020019 for file in os.listdir(TEST_PATH):
gstepanov2164d822015-02-04 19:49:40 +020020 if file.endswith(".json"):
21 result.append(file.split('.')[0])
22
23 return result
24
25
26def load_test(test_name):
27 test_name += '.json'
28
gstepanov3501ad42015-02-05 15:41:39 +020029 with open(TEST_PATH + "/" + test_name, 'rt') as f:
gstepanov2164d822015-02-04 19:49:40 +020030 raw = f.read()
31
gstepanov0a8fdd32015-02-05 15:54:11 +020032 if raw != "":
33 test = json.loads(raw)
34 else:
35 test = []
gstepanov2164d822015-02-04 19:49:40 +020036
37 return test
38
39
40@app.route("/", methods=['GET', 'POST'])
41def 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'])
53def render_test(test_name):
54 tests = load_test(test_name)
55 header_keys = ['build_id', 'iso_md5', 'type']
gstepanov0a8fdd32015-02-05 15:54:11 +020056 table = [[]]
gstepanov200d3b42015-02-06 14:01:49 +020057 storage = create_storage('file://' + TEST_PATH + '/' + test_name + '.json')
58 results = storage.recent_builds()
gstepanov200d3b42015-02-06 14:01:49 +020059 bars = build_vertical_bar(results)
60 lines = build_lines_chart(results)
61 urls = bars + lines
gstepanov2164d822015-02-04 19:49:40 +020062 if len(tests) > 0:
63 sorted_keys = sorted(tests[0].keys())
64
gstepanov0a8fdd32015-02-05 15:54:11 +020065 for key in sorted_keys:
66 if key not in header_keys:
67 header_keys.append(key)
gstepanov2164d822015-02-04 19:49:40 +020068
gstepanov0a8fdd32015-02-05 15:54:11 +020069 for test in tests:
70 row = []
gstepanov2164d822015-02-04 19:49:40 +020071
gstepanov0a8fdd32015-02-05 15:54:11 +020072 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])
gstepanov2164d822015-02-04 19:49:40 +020077
gstepanov0a8fdd32015-02-05 15:54:11 +020078 table.append(row)
gstepanov2164d822015-02-04 19:49:40 +020079
gstepanovcd77d5a2015-02-06 14:49:34 +020080 return render_template("test.html", urls=urls, table_url=url_for('render_table', test_name=test_name))
81
82
gstepanovbb854922015-02-09 18:18:17 +020083def 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
gstepanovcd77d5a2015-02-06 14:49:34 +0200122@app.route("/tests/table/<test_name>/")
123def render_table(test_name):
124 tests = load_test(test_name)
125 header_keys = ['build_id', 'iso_md5', 'type']
126 table = [[]]
gstepanovbb854922015-02-09 18:18:17 +0200127 meta = {"__meta__" : "http://172.16.52.112:8000/api/nodes"}
gstepanov81a52ee2015-02-10 16:22:20 +0200128 # data = collect_lab_data(tests, meta)
129 data = {}
gstepanovcd77d5a2015-02-06 14:49:34 +0200130 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
gstepanovbb854922015-02-09 18:18:17 +0200148 return render_template("table.html", headers=header_keys, table=table,
149 back_url=url_for('render_test', test_name=test_name), lab=data)
gstepanov2164d822015-02-04 19:49:40 +0200150
151
152@app.route("/tests/<test_name>", methods=['POST'])
153def add_test(test_name):
154 tests = json.loads(request.data)
155
156 if not hasattr(g, "storage"):
gstepanovf8f01c12015-02-09 18:34:01 +0200157 path = "file://" + TEST_PATH + '/' + test_name + ".json"
158 print path
159 g.storage = create_storage(path, "", "")
gstepanov2164d822015-02-04 19:49:40 +0200160
161 for test in tests:
162 g.storage.store(test)
163 return "Created", 201
164
165
gstepanov2164d822015-02-04 19:49:40 +0200166if __name__ == "__main__":
gstepanov81a52ee2015-02-10 16:22:20 +0200167 logger = getLogger("logger")
168 app.logger.setLevel(INFO)
169 app.logger.addHandler(logger)
gstepanov6f7167d2015-02-06 12:51:58 +0200170 app.run(host='0.0.0.0', debug=True)