blob: 4bd99218ce70d72cfc6967356cd50408caf8c1c9 [file] [log] [blame]
gstepanov2164d822015-02-04 19:49:40 +02001from flask import Flask, render_template, url_for, request, g
2from flask_bootstrap import Bootstrap
3import json
4import os.path
gstepanov725cc302015-02-09 15:08:06 +02005from config import TEST_PATH
gstepanov200d3b42015-02-06 14:01:49 +02006from report import build_vertical_bar, build_lines_chart
gstepanov725cc302015-02-09 15:08:06 +02007from storage_api import create_storage
gstepanovbb854922015-02-09 18:18:17 +02008import requests
gstepanov2164d822015-02-04 19:49:40 +02009
10app = Flask(__name__)
11Bootstrap(app)
12
13
14def collect_tests():
15 result = []
16
gstepanovc3ffac72015-02-05 15:39:45 +020017 for file in os.listdir(TEST_PATH):
gstepanov2164d822015-02-04 19:49:40 +020018 if file.endswith(".json"):
19 result.append(file.split('.')[0])
20
21 return result
22
23
24def load_test(test_name):
25 test_name += '.json'
26
gstepanov3501ad42015-02-05 15:41:39 +020027 with open(TEST_PATH + "/" + test_name, 'rt') as f:
gstepanov2164d822015-02-04 19:49:40 +020028 raw = f.read()
29
gstepanov0a8fdd32015-02-05 15:54:11 +020030 if raw != "":
31 test = json.loads(raw)
32 else:
33 test = []
gstepanov2164d822015-02-04 19:49:40 +020034
35 return test
36
37
38@app.route("/", methods=['GET', 'POST'])
39def index():
40 data = []
41 for test in collect_tests():
42 d = {}
43 d["name"] = test
44 d["url"] = url_for("render_test", test_name=test)
45 data.append(d)
46
47 return render_template("index.html", tests=data)
48
49
50@app.route("/tests/<test_name>", methods=['GET'])
51def render_test(test_name):
52 tests = load_test(test_name)
53 header_keys = ['build_id', 'iso_md5', 'type']
gstepanov0a8fdd32015-02-05 15:54:11 +020054 table = [[]]
gstepanov200d3b42015-02-06 14:01:49 +020055 storage = create_storage('file://' + TEST_PATH + '/' + test_name + '.json')
56 results = storage.recent_builds()
gstepanov200d3b42015-02-06 14:01:49 +020057 bars = build_vertical_bar(results)
58 lines = build_lines_chart(results)
59 urls = bars + lines
gstepanov2164d822015-02-04 19:49:40 +020060 if len(tests) > 0:
61 sorted_keys = sorted(tests[0].keys())
62
gstepanov0a8fdd32015-02-05 15:54:11 +020063 for key in sorted_keys:
64 if key not in header_keys:
65 header_keys.append(key)
gstepanov2164d822015-02-04 19:49:40 +020066
gstepanov0a8fdd32015-02-05 15:54:11 +020067 for test in tests:
68 row = []
gstepanov2164d822015-02-04 19:49:40 +020069
gstepanov0a8fdd32015-02-05 15:54:11 +020070 for header in header_keys:
71 if isinstance(test[header], list):
72 row.append(str(test[header][0]) + unichr(0x00B1) + str(test[header][1]))
73 else:
74 row.append(test[header])
gstepanov2164d822015-02-04 19:49:40 +020075
gstepanov0a8fdd32015-02-05 15:54:11 +020076 table.append(row)
gstepanov2164d822015-02-04 19:49:40 +020077
gstepanovcd77d5a2015-02-06 14:49:34 +020078 return render_template("test.html", urls=urls, table_url=url_for('render_table', test_name=test_name))
79
80
gstepanovbb854922015-02-09 18:18:17 +020081def collect_lab_data(tests, meta):
82 print requests.get(meta['__meta__'], cookies={"token" : '72237e94dc2b408482c29edaf3609da7'}).content
83 lab_info = json.loads(requests.get(meta['__meta__'], cookies={"token" : '72237e94dc2b408482c29edaf3609da7'}).content)
84 nodes = []
85 result = {}
86
87 for node in lab_info:
88 d = {}
89 d['name'] = node['name']
90 p = []
91 i = []
92 disks = []
93 devices = []
94
95 for processor in node['meta']['cpu']['spec']:
96 p.append(processor)
97
98 for iface in node['meta']['interfaces']:
99 i.append(iface)
100
101 m = node['meta']['memory'].copy()
102
103 for disk in node['meta']['disks']:
104 disks.append(disk)
105
106 d['memory'] = m
107 d['disks'] = disks
108 d['devices'] = devices
109 d['interfaces'] = i
110 d['processors'] = p
111
112 nodes.append(d)
113
114 result['nodes'] = nodes
115 result['name'] = 'Perf-1 Env'
116
117 return result
118
119
gstepanovcd77d5a2015-02-06 14:49:34 +0200120@app.route("/tests/table/<test_name>/")
121def render_table(test_name):
122 tests = load_test(test_name)
123 header_keys = ['build_id', 'iso_md5', 'type']
124 table = [[]]
gstepanovbb854922015-02-09 18:18:17 +0200125 meta = {"__meta__" : "http://172.16.52.112:8000/api/nodes"}
126 data = collect_lab_data(tests, meta)
gstepanovcd77d5a2015-02-06 14:49:34 +0200127
128 if len(tests) > 0:
129 sorted_keys = sorted(tests[0].keys())
130
131 for key in sorted_keys:
132 if key not in header_keys:
133 header_keys.append(key)
134
135 for test in tests:
136 row = []
137
138 for header in header_keys:
139 if isinstance(test[header], list):
140 row.append(str(test[header][0]) + unichr(0x00B1) + str(test[header][1]))
141 else:
142 row.append(test[header])
143
144 table.append(row)
145
gstepanovbb854922015-02-09 18:18:17 +0200146 return render_template("table.html", headers=header_keys, table=table,
147 back_url=url_for('render_test', test_name=test_name), lab=data)
gstepanov2164d822015-02-04 19:49:40 +0200148
149
150@app.route("/tests/<test_name>", methods=['POST'])
151def add_test(test_name):
152 tests = json.loads(request.data)
153
154 if not hasattr(g, "storage"):
gstepanovf8f01c12015-02-09 18:34:01 +0200155 path = "file://" + TEST_PATH + '/' + test_name + ".json"
156 print path
157 g.storage = create_storage(path, "", "")
gstepanov2164d822015-02-04 19:49:40 +0200158
159 for test in tests:
160 g.storage.store(test)
161 return "Created", 201
162
163
gstepanov2164d822015-02-04 19:49:40 +0200164if __name__ == "__main__":
gstepanov6f7167d2015-02-06 12:51:58 +0200165 app.run(host='0.0.0.0', debug=True)