Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 1 | import argparse |
| 2 | from collections import OrderedDict |
| 3 | import sys |
| 4 | |
| 5 | import charts |
| 6 | import storage_api |
| 7 | |
| 8 | |
Yulia Portnova | 919f3be | 2015-02-06 12:49:22 +0200 | [diff] [blame^] | 9 | def ssize_to_kb(ssize): |
| 10 | try: |
| 11 | smap = dict(k=1, K=1, M=1024, m=1024, G=1024**2, g=1024**2) |
| 12 | for ext, coef in smap.items(): |
| 13 | if ssize.endswith(ext): |
| 14 | return int(ssize[:-1]) * coef |
| 15 | |
| 16 | if int(ssize) % 1024 != 0: |
| 17 | raise ValueError() |
| 18 | |
| 19 | return int(ssize) / 1024 |
| 20 | |
| 21 | except (ValueError, TypeError, AttributeError): |
| 22 | tmpl = "Unknow size format {0!r} (or size not multiples 1024)" |
| 23 | raise ValueError(tmpl.format(ssize)) |
| 24 | |
| 25 | |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 26 | def parse_args(argv): |
| 27 | parser = argparse.ArgumentParser() |
| 28 | parser.add_argument('-s', '--storage', help='storage location', dest="url") |
| 29 | parser.add_argument('-e', '--email', help='user email', |
| 30 | default="aaa@gmail.com") |
| 31 | parser.add_argument('-p', '--password', help='user password', |
| 32 | default="1234") |
| 33 | return parser.parse_args(argv) |
| 34 | |
| 35 | |
Yulia Portnova | 919f3be | 2015-02-06 12:49:22 +0200 | [diff] [blame^] | 36 | def build_vertical_bar(results): |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 37 | data = {} |
Yulia Portnova | 919f3be | 2015-02-06 12:49:22 +0200 | [diff] [blame^] | 38 | charts_url = [] |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 39 | |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 40 | for build, results in results.items(): |
Yulia Portnova | 919f3be | 2015-02-06 12:49:22 +0200 | [diff] [blame^] | 41 | for key, value in results.results.items(): |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 42 | keys = key.split(' ') |
| 43 | if not data.get(keys[2]): |
| 44 | data[keys[2]] = {} |
| 45 | if not data[keys[2]].get(build): |
| 46 | data[keys[2]][build] = {} |
| 47 | data[keys[2]][build][' '.join([keys[0], keys[1]])] = value |
| 48 | |
| 49 | for name, value in data.items(): |
| 50 | title = name |
| 51 | legend = [] |
| 52 | dataset = [] |
| 53 | scale_x = [] |
| 54 | for build_id, build_results in value.items(): |
| 55 | legend.append(build_id) |
| 56 | ordered_build_results = OrderedDict(sorted(build_results.items(), |
| 57 | key=lambda t: t[0])) |
| 58 | if not scale_x: |
| 59 | scale_x = ordered_build_results.keys() |
| 60 | dataset.append(ordered_build_results.values()) |
| 61 | |
| 62 | bar = charts.render_vertical_bar(title, legend, dataset, |
| 63 | scale_x=scale_x) |
Yulia Portnova | 919f3be | 2015-02-06 12:49:22 +0200 | [diff] [blame^] | 64 | charts_url.append(str(bar)) |
| 65 | return charts_url |
| 66 | |
| 67 | |
| 68 | def build_lines_chart(results): |
| 69 | data = {} |
| 70 | charts_url = [] |
| 71 | |
| 72 | for build, results in results.items(): |
| 73 | for key, value in results.results.items(): |
| 74 | keys = key.split(' ') |
| 75 | if not data.get(' '.join([keys[0], keys[1]])): |
| 76 | data[' '.join([keys[0], keys[1]])] = {} |
| 77 | if not data[' '.join([keys[0], keys[1]])].get(build): |
| 78 | data[' '.join([keys[0], keys[1]])][build] = {} |
| 79 | data[' '.join([keys[0], keys[1]])][build][keys[2]] = value |
| 80 | |
| 81 | for name, value in data.items(): |
| 82 | title = name |
| 83 | legend = [] |
| 84 | dataset = [] |
| 85 | scale_x = [] |
| 86 | for build_id, build_results in value.items(): |
| 87 | legend.append(build_id) |
| 88 | ordered_build_results = OrderedDict(sorted(build_results.items(), |
| 89 | key=lambda t: ssize_to_kb(t[0]))) |
| 90 | if not scale_x: |
| 91 | scale_x = ordered_build_results.keys() |
| 92 | dataset.append(zip(*ordered_build_results.values())[0]) |
| 93 | |
| 94 | chart = charts.render_lines(title, legend, dataset, scale_x) |
| 95 | charts_url.append(str(chart)) |
| 96 | |
| 97 | return charts_url |
| 98 | |
| 99 | |
| 100 | def render_html(charts_urls): |
| 101 | templ = open("report.html", 'r').read() |
| 102 | body = "<div><ol>%s</ol></div>" |
| 103 | li = "<li><img src='%s'></li>" |
| 104 | ol = [] |
| 105 | for chart in charts_urls: |
| 106 | ol.append(li % chart) |
| 107 | html = templ % {'body': body % '\n'.join(ol)} |
| 108 | open('results.html', 'w').write(html) |
| 109 | |
| 110 | |
| 111 | def report(url, email=None, password=None): |
| 112 | storage = storage_api.create_storage(url, email, password) |
| 113 | results = storage.recent_builds() |
| 114 | bars = build_vertical_bar(results) |
| 115 | lines = build_lines_chart(results) |
| 116 | |
| 117 | render_html(bars + lines) |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 118 | |
| 119 | |
| 120 | def main(argv): |
| 121 | opts = parse_args(argv) |
| 122 | report(opts.url) |
| 123 | return 0 |
| 124 | |
| 125 | |
| 126 | if __name__ == '__main__': |
| 127 | exit(main(sys.argv[1:])) |