koder aka kdanilov | 0f0546c | 2015-02-17 20:42:05 -0800 | [diff] [blame] | 1 | # <koder>: order imports in usual way |
gstepanov | 0832b77 | 2015-02-20 17:48:19 +0200 | [diff] [blame] | 2 | import json |
gstepanov | 15681d6 | 2015-02-23 17:17:50 +0200 | [diff] [blame^] | 3 | from logging import getLogger, INFO |
koder aka kdanilov | 0f0546c | 2015-02-17 20:42:05 -0800 | [diff] [blame] | 4 | |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 5 | from flask import render_template, url_for, make_response, request |
gstepanov | 200d3b4 | 2015-02-06 14:01:49 +0200 | [diff] [blame] | 6 | from report import build_vertical_bar, build_lines_chart |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 7 | from web_app import app |
gstepanov | 15681d6 | 2015-02-23 17:17:50 +0200 | [diff] [blame^] | 8 | from 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 |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 10 | from web_app.app import app |
| 11 | import os.path |
| 12 | from werkzeug.routing import Rule |
gstepanov | 3ce37b8 | 2015-02-12 16:20:56 +0200 | [diff] [blame] | 13 | |
| 14 | |
gstepanov | 6c5deb3 | 2015-02-12 19:25:46 +0200 | [diff] [blame] | 15 | def merge_builds(b1, b2): |
| 16 | d = {} |
| 17 | |
| 18 | for pair in b2.items(): |
gstepanov | 8053b01 | 2015-02-16 17:25:27 +0200 | [diff] [blame] | 19 | if pair[0] in b1 and type(pair[1]) is list: |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 20 | b1[pair[0]].extend(pair[1]) |
gstepanov | 8053b01 | 2015-02-16 17:25:27 +0200 | [diff] [blame] | 21 | else: |
| 22 | b1[pair[0]] = pair[1] |
gstepanov | 6c5deb3 | 2015-02-12 19:25:46 +0200 | [diff] [blame] | 23 | |
| 24 | |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 25 | app.url_map.add(Rule('/', endpoint='index')) |
| 26 | app.url_map.add(Rule('/images/<image_name>', endpoint='get_image')) |
| 27 | app.url_map.add(Rule('/tests/<test_name>', endpoint='render_test')) |
| 28 | app.url_map.add(Rule('/tests/table/<test_name>/', endpoint='render_table')) |
| 29 | app.url_map.add(Rule('/api/tests/<test_name>', endpoint='add_test', methods=['POST'])) |
| 30 | app.url_map.add(Rule('/api/tests', endpoint='get_all_tests')) |
| 31 | app.url_map.add(Rule('/api/tests/<test_name>', endpoint='get_test')) |
| 32 | |
| 33 | |
| 34 | @app.endpoint('index') |
gstepanov | 9ddc18f | 2015-02-11 20:28:59 +0200 | [diff] [blame] | 35 | def index(): |
| 36 | data = builds_list() |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 37 | |
| 38 | for elem in data: |
| 39 | elem['url'] = url_for('render_test', test_name=elem['url']) |
| 40 | |
gstepanov | 9ddc18f | 2015-02-11 20:28:59 +0200 | [diff] [blame] | 41 | return render_template("index.html", tests=data) |
| 42 | |
| 43 | |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 44 | @app.endpoint('get_image') |
gstepanov | 6ce6bf0 | 2015-02-16 19:04:14 +0200 | [diff] [blame] | 45 | def 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 | |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 56 | @app.endpoint('render_test') |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 57 | def render_test(test_name): |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 58 | results = prepare_build_data(test_name) |
gstepanov | 15681d6 | 2015-02-23 17:17:50 +0200 | [diff] [blame^] | 59 | lab_meta = get_build_detailed_info(test_name) |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 60 | |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 61 | bars = build_vertical_bar(results) |
| 62 | lines = build_lines_chart(results) |
| 63 | urls = bars + lines |
gstepanov | 6ce6bf0 | 2015-02-16 19:04:14 +0200 | [diff] [blame] | 64 | |
| 65 | urls = [url_for("get_image", image_name=os.path.basename(url)) if not url.startswith('http') else url for url in urls] |
| 66 | |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 67 | return render_template("test.html", urls=urls, |
| 68 | table_url=url_for('render_table', test_name=test_name), |
gstepanov | 9eb8bf3 | 2015-02-11 20:13:14 +0200 | [diff] [blame] | 69 | index_url=url_for('index'), lab_meta=lab_meta) |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 70 | |
| 71 | |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 72 | @app.endpoint('render_table') |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 73 | def render_table(test_name): |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 74 | builds = get_data_for_table(test_name) |
gstepanov | 15681d6 | 2015-02-23 17:17:50 +0200 | [diff] [blame^] | 75 | data = get_build_info(test_name) |
gstepanov | 3ce37b8 | 2015-02-12 16:20:56 +0200 | [diff] [blame] | 76 | |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 77 | header_keys = ['build_id', 'iso_md5', 'type', 'date'] |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 78 | table = [[]] |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 79 | if len(builds) > 0: |
| 80 | sorted_keys = sorted(builds[0].keys()) |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 81 | |
| 82 | for key in sorted_keys: |
| 83 | if key not in header_keys: |
| 84 | header_keys.append(key) |
| 85 | |
gstepanov | bdc406b | 2015-02-11 17:35:55 +0200 | [diff] [blame] | 86 | for test in builds: |
gstepanov | cd77d5a | 2015-02-06 14:49:34 +0200 | [diff] [blame] | 87 | 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 | |
gstepanov | bb85492 | 2015-02-09 18:18:17 +0200 | [diff] [blame] | 97 | return render_template("table.html", headers=header_keys, table=table, |
| 98 | back_url=url_for('render_test', test_name=test_name), lab=data) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 99 | |
| 100 | |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 101 | @app.endpoint('add_test') |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 102 | def add_test(test_name): |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 103 | add_data(request.data) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 104 | return "Created", 201 |
| 105 | |
| 106 | |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 107 | @app.endpoint('get_all_tests') |
gstepanov | 3ce37b8 | 2015-02-12 16:20:56 +0200 | [diff] [blame] | 108 | def get_all_tests(): |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 109 | return json.dumps(get_builds_data()) |
gstepanov | 3ce37b8 | 2015-02-12 16:20:56 +0200 | [diff] [blame] | 110 | |
| 111 | |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 112 | @app.endpoint('get_test') |
gstepanov | 3ce37b8 | 2015-02-12 16:20:56 +0200 | [diff] [blame] | 113 | def get_test(test_name): |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 114 | builds = get_builds_data(test_name) |
gstepanov | 3ce37b8 | 2015-02-12 16:20:56 +0200 | [diff] [blame] | 115 | |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 116 | return json.dumps(builds) |
gstepanov | 3ce37b8 | 2015-02-12 16:20:56 +0200 | [diff] [blame] | 117 | |
| 118 | |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 119 | if __name__ == "__main__": |
gstepanov | 81a52ee | 2015-02-10 16:22:20 +0200 | [diff] [blame] | 120 | logger = getLogger("logger") |
| 121 | app.logger.setLevel(INFO) |
| 122 | app.logger.addHandler(logger) |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 123 | app.run(host='0.0.0.0', debug=True) |