blob: ac0bc152086fd3c5860f822728b494f872c900b2 [file] [log] [blame]
koder aka kdanilov0f0546c2015-02-17 20:42:05 -08001# <koder>: order imports in usual way
gstepanov0832b772015-02-20 17:48:19 +02002import json
gstepanov15681d62015-02-23 17:17:50 +02003from logging import getLogger, INFO
koder aka kdanilov0f0546c2015-02-17 20:42:05 -08004
stgleb75c70412015-02-17 02:52:00 +02005from flask import render_template, url_for, make_response, request
gstepanov200d3b42015-02-06 14:01:49 +02006from report import build_vertical_bar, build_lines_chart
stgleb75c70412015-02-17 02:52:00 +02007from web_app import app
gstepanov15681d62015-02-23 17:17:50 +02008from persistance.storage_api import builds_list, prepare_build_data, get_data_for_table, add_data, get_builds_data, \
9 get_build_info, get_build_detailed_info
stgleb75c70412015-02-17 02:52:00 +020010from web_app.app import app
11import os.path
12from werkzeug.routing import Rule
gstepanov3ce37b82015-02-12 16:20:56 +020013
14
gstepanov6c5deb32015-02-12 19:25:46 +020015def merge_builds(b1, b2):
16 d = {}
17
18 for pair in b2.items():
gstepanov8053b012015-02-16 17:25:27 +020019 if pair[0] in b1 and type(pair[1]) is list:
stgleb75c70412015-02-17 02:52:00 +020020 b1[pair[0]].extend(pair[1])
gstepanov8053b012015-02-16 17:25:27 +020021 else:
22 b1[pair[0]] = pair[1]
gstepanov6c5deb32015-02-12 19:25:46 +020023
24
stgleb75c70412015-02-17 02:52:00 +020025app.url_map.add(Rule('/', endpoint='index'))
26app.url_map.add(Rule('/images/<image_name>', endpoint='get_image'))
27app.url_map.add(Rule('/tests/<test_name>', endpoint='render_test'))
28app.url_map.add(Rule('/tests/table/<test_name>/', endpoint='render_table'))
29app.url_map.add(Rule('/api/tests/<test_name>', endpoint='add_test', methods=['POST']))
30app.url_map.add(Rule('/api/tests', endpoint='get_all_tests'))
31app.url_map.add(Rule('/api/tests/<test_name>', endpoint='get_test'))
32
33
34@app.endpoint('index')
gstepanov9ddc18f2015-02-11 20:28:59 +020035def index():
36 data = builds_list()
stgleb75c70412015-02-17 02:52:00 +020037
38 for elem in data:
39 elem['url'] = url_for('render_test', test_name=elem['url'])
40
gstepanov9ddc18f2015-02-11 20:28:59 +020041 return render_template("index.html", tests=data)
42
43
stgleb75c70412015-02-17 02:52:00 +020044@app.endpoint('get_image')
gstepanov6ce6bf02015-02-16 19:04:14 +020045def get_image(image_name):
46 with open("static/images/" + image_name, 'rb') as f:
47 image_binary = f.read()
48
49 response = make_response(image_binary)
50 response.headers['Content-Type'] = 'image/png'
51 response.headers['Content-Disposition'] = 'attachment; filename=img.png'
52
53 return response
54
55
stgleb75c70412015-02-17 02:52:00 +020056@app.endpoint('render_test')
gstepanov2164d822015-02-04 19:49:40 +020057def render_test(test_name):
stgleb75c70412015-02-17 02:52:00 +020058 results = prepare_build_data(test_name)
gstepanov15681d62015-02-23 17:17:50 +020059 lab_meta = get_build_detailed_info(test_name)
gstepanovbdc406b2015-02-11 17:35:55 +020060
stgleb75c70412015-02-17 02:52:00 +020061 bars = build_vertical_bar(results)
62 lines = build_lines_chart(results)
63 urls = bars + lines
gstepanov6ce6bf02015-02-16 19:04:14 +020064
65 urls = [url_for("get_image", image_name=os.path.basename(url)) if not url.startswith('http') else url for url in urls]
66
stgleb75c70412015-02-17 02:52:00 +020067 return render_template("test.html", urls=urls,
68 table_url=url_for('render_table', test_name=test_name),
gstepanov9eb8bf32015-02-11 20:13:14 +020069 index_url=url_for('index'), lab_meta=lab_meta)
gstepanovcd77d5a2015-02-06 14:49:34 +020070
71
stgleb75c70412015-02-17 02:52:00 +020072@app.endpoint('render_table')
gstepanovcd77d5a2015-02-06 14:49:34 +020073def render_table(test_name):
stgleb75c70412015-02-17 02:52:00 +020074 builds = get_data_for_table(test_name)
gstepanov15681d62015-02-23 17:17:50 +020075 data = get_build_info(test_name)
gstepanov3ce37b82015-02-12 16:20:56 +020076
stgleb75c70412015-02-17 02:52:00 +020077 header_keys = ['build_id', 'iso_md5', 'type', 'date']
gstepanovcd77d5a2015-02-06 14:49:34 +020078 table = [[]]
gstepanovbdc406b2015-02-11 17:35:55 +020079 if len(builds) > 0:
80 sorted_keys = sorted(builds[0].keys())
gstepanovcd77d5a2015-02-06 14:49:34 +020081
82 for key in sorted_keys:
83 if key not in header_keys:
84 header_keys.append(key)
85
gstepanovbdc406b2015-02-11 17:35:55 +020086 for test in builds:
gstepanovcd77d5a2015-02-06 14:49:34 +020087 row = []
88
89 for header in header_keys:
90 if isinstance(test[header], list):
91 row.append(str(test[header][0]) + unichr(0x00B1) + str(test[header][1]))
92 else:
93 row.append(test[header])
94
95 table.append(row)
96
gstepanovbb854922015-02-09 18:18:17 +020097 return render_template("table.html", headers=header_keys, table=table,
98 back_url=url_for('render_test', test_name=test_name), lab=data)
gstepanov2164d822015-02-04 19:49:40 +020099
100
stgleb75c70412015-02-17 02:52:00 +0200101@app.endpoint('add_test')
gstepanov2164d822015-02-04 19:49:40 +0200102def add_test(test_name):
stgleb75c70412015-02-17 02:52:00 +0200103 add_data(request.data)
gstepanov2164d822015-02-04 19:49:40 +0200104 return "Created", 201
105
106
stgleb75c70412015-02-17 02:52:00 +0200107@app.endpoint('get_all_tests')
gstepanov3ce37b82015-02-12 16:20:56 +0200108def get_all_tests():
stgleb75c70412015-02-17 02:52:00 +0200109 return json.dumps(get_builds_data())
gstepanov3ce37b82015-02-12 16:20:56 +0200110
111
stgleb75c70412015-02-17 02:52:00 +0200112@app.endpoint('get_test')
gstepanov3ce37b82015-02-12 16:20:56 +0200113def get_test(test_name):
stgleb75c70412015-02-17 02:52:00 +0200114 builds = get_builds_data(test_name)
gstepanov3ce37b82015-02-12 16:20:56 +0200115
stgleb75c70412015-02-17 02:52:00 +0200116 return json.dumps(builds)
gstepanov3ce37b82015-02-12 16:20:56 +0200117
118
gstepanov2164d822015-02-04 19:49:40 +0200119if __name__ == "__main__":
gstepanov81a52ee2015-02-10 16:22:20 +0200120 logger = getLogger("logger")
121 app.logger.setLevel(INFO)
122 app.logger.addHandler(logger)
stgleb75c70412015-02-17 02:52:00 +0200123 app.run(host='0.0.0.0', debug=True)