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