blob: 778bfa428c40e72cd5ce0e4551251c8297eb46b2 [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
gstepanovdae57122015-03-02 20:50:41 +02003import os.path
koder aka kdanilov0f0546c2015-02-17 20:42:05 -08004
gstepanovdae57122015-03-02 20:50:41 +02005from logging import getLogger, INFO
stgleb75c70412015-02-17 02:52:00 +02006from flask import render_template, url_for, make_response, request
gstepanov200d3b42015-02-06 14:01:49 +02007from report import build_vertical_bar, build_lines_chart
stgleb75c70412015-02-17 02:52:00 +02008from web_app import app
gstepanove6ad1f02015-03-02 19:58:00 +02009from persistance.storage_api import builds_list, prepare_build_data, \
10 get_data_for_table, add_data, get_builds_data, \
gstepanov15681d62015-02-23 17:17:50 +020011 get_build_info, get_build_detailed_info
stgleb75c70412015-02-17 02:52:00 +020012from web_app.app import app
stgleb75c70412015-02-17 02:52:00 +020013from werkzeug.routing import Rule
gstepanov3ce37b82015-02-12 16:20:56 +020014
15
gstepanov6c5deb32015-02-12 19:25:46 +020016def merge_builds(b1, b2):
17 d = {}
18
19 for pair in b2.items():
gstepanov8053b012015-02-16 17:25:27 +020020 if pair[0] in b1 and type(pair[1]) is list:
stgleb75c70412015-02-17 02:52:00 +020021 b1[pair[0]].extend(pair[1])
gstepanov8053b012015-02-16 17:25:27 +020022 else:
23 b1[pair[0]] = pair[1]
gstepanov6c5deb32015-02-12 19:25:46 +020024
25
stgleb75c70412015-02-17 02:52:00 +020026app.url_map.add(Rule('/', endpoint='index'))
27app.url_map.add(Rule('/images/<image_name>', endpoint='get_image'))
28app.url_map.add(Rule('/tests/<test_name>', endpoint='render_test'))
29app.url_map.add(Rule('/tests/table/<test_name>/', endpoint='render_table'))
gstepanove6ad1f02015-03-02 19:58:00 +020030app.url_map.add(Rule('/api/tests/<test_name>',
31 endpoint='add_test', methods=['POST']))
stgleb75c70412015-02-17 02:52:00 +020032app.url_map.add(Rule('/api/tests', endpoint='get_all_tests'))
33app.url_map.add(Rule('/api/tests/<test_name>', endpoint='get_test'))
34
35
36@app.endpoint('index')
gstepanov9ddc18f2015-02-11 20:28:59 +020037def index():
38 data = builds_list()
stgleb75c70412015-02-17 02:52:00 +020039
40 for elem in data:
41 elem['url'] = url_for('render_test', test_name=elem['url'])
42
gstepanov9ddc18f2015-02-11 20:28:59 +020043 return render_template("index.html", tests=data)
44
45
stgleb75c70412015-02-17 02:52:00 +020046@app.endpoint('get_image')
gstepanov6ce6bf02015-02-16 19:04:14 +020047def get_image(image_name):
48 with open("static/images/" + image_name, 'rb') as f:
49 image_binary = f.read()
50
51 response = make_response(image_binary)
52 response.headers['Content-Type'] = 'image/png'
53 response.headers['Content-Disposition'] = 'attachment; filename=img.png'
54
55 return response
56
57
stgleb75c70412015-02-17 02:52:00 +020058@app.endpoint('render_test')
gstepanov2164d822015-02-04 19:49:40 +020059def render_test(test_name):
stgleb75c70412015-02-17 02:52:00 +020060 results = prepare_build_data(test_name)
gstepanov15681d62015-02-23 17:17:50 +020061 lab_meta = get_build_detailed_info(test_name)
gstepanovbdc406b2015-02-11 17:35:55 +020062
stgleb75c70412015-02-17 02:52:00 +020063 bars = build_vertical_bar(results)
64 lines = build_lines_chart(results)
65 urls = bars + lines
gstepanov6ce6bf02015-02-16 19:04:14 +020066
gstepanove6ad1f02015-03-02 19:58:00 +020067 urls = [url_for("get_image", image_name=os.path.basename(url))
68 if not url.startswith('http') else url for url in urls]
gstepanov6ce6bf02015-02-16 19:04:14 +020069
stgleb75c70412015-02-17 02:52:00 +020070 return render_template("test.html", urls=urls,
gstepanove6ad1f02015-03-02 19:58:00 +020071 table_url=url_for('render_table',
72 test_name=test_name),
gstepanov9eb8bf32015-02-11 20:13:14 +020073 index_url=url_for('index'), lab_meta=lab_meta)
gstepanovcd77d5a2015-02-06 14:49:34 +020074
75
stgleb75c70412015-02-17 02:52:00 +020076@app.endpoint('render_table')
gstepanovcd77d5a2015-02-06 14:49:34 +020077def render_table(test_name):
stgleb75c70412015-02-17 02:52:00 +020078 builds = get_data_for_table(test_name)
gstepanov15681d62015-02-23 17:17:50 +020079 data = get_build_info(test_name)
gstepanov3ce37b82015-02-12 16:20:56 +020080
stgleb75c70412015-02-17 02:52:00 +020081 header_keys = ['build_id', 'iso_md5', 'type', 'date']
gstepanovcd77d5a2015-02-06 14:49:34 +020082 table = [[]]
gstepanovbdc406b2015-02-11 17:35:55 +020083 if len(builds) > 0:
84 sorted_keys = sorted(builds[0].keys())
gstepanovcd77d5a2015-02-06 14:49:34 +020085
86 for key in sorted_keys:
87 if key not in header_keys:
88 header_keys.append(key)
89
gstepanovbdc406b2015-02-11 17:35:55 +020090 for test in builds:
gstepanovcd77d5a2015-02-06 14:49:34 +020091 row = []
92
93 for header in header_keys:
94 if isinstance(test[header], list):
gstepanove6ad1f02015-03-02 19:58:00 +020095 row.append(str(test[header][0]) + unichr(0x00B1)
96 + str(test[header][1]))
gstepanovcd77d5a2015-02-06 14:49:34 +020097 else:
98 row.append(test[header])
99
100 table.append(row)
101
gstepanovbb854922015-02-09 18:18:17 +0200102 return render_template("table.html", headers=header_keys, table=table,
gstepanove6ad1f02015-03-02 19:58:00 +0200103 back_url=url_for('render_test',
104 test_name=test_name), lab=data)
gstepanov2164d822015-02-04 19:49:40 +0200105
106
stgleb75c70412015-02-17 02:52:00 +0200107@app.endpoint('add_test')
gstepanov2164d822015-02-04 19:49:40 +0200108def add_test(test_name):
stgleb75c70412015-02-17 02:52:00 +0200109 add_data(request.data)
gstepanov2164d822015-02-04 19:49:40 +0200110 return "Created", 201
111
112
stgleb75c70412015-02-17 02:52:00 +0200113@app.endpoint('get_all_tests')
gstepanov3ce37b82015-02-12 16:20:56 +0200114def get_all_tests():
stgleb75c70412015-02-17 02:52:00 +0200115 return json.dumps(get_builds_data())
gstepanov3ce37b82015-02-12 16:20:56 +0200116
117
stgleb75c70412015-02-17 02:52:00 +0200118@app.endpoint('get_test')
gstepanov3ce37b82015-02-12 16:20:56 +0200119def get_test(test_name):
stgleb75c70412015-02-17 02:52:00 +0200120 builds = get_builds_data(test_name)
gstepanov3ce37b82015-02-12 16:20:56 +0200121
stgleb75c70412015-02-17 02:52:00 +0200122 return json.dumps(builds)
gstepanov3ce37b82015-02-12 16:20:56 +0200123
124
gstepanov2164d822015-02-04 19:49:40 +0200125if __name__ == "__main__":
gstepanov81a52ee2015-02-10 16:22:20 +0200126 logger = getLogger("logger")
127 app.logger.setLevel(INFO)
128 app.logger.addHandler(logger)
gstepanove6ad1f02015-03-02 19:58:00 +0200129 app.run(host='0.0.0.0', debug=True)