xref: /aosp_15_r20/external/toolchain-utils/crosperf/results_report_templates.py (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1# -*- coding: utf-8 -*-
2# Copyright 2016 The ChromiumOS Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Text templates used by various parts of results_report."""
7
8import html
9from string import Template
10
11
12_TabMenuTemplate = Template(
13    """
14<div class='tab-menu'>
15  <a href="javascript:switchTab('$table_name', 'html')">HTML</a>
16  <a href="javascript:switchTab('$table_name', 'text')">Text</a>
17  <a href="javascript:switchTab('$table_name', 'tsv')">TSV</a>
18</div>"""
19)
20
21
22def _GetTabMenuHTML(table_name):
23    # N.B. cgi.escape does some very basic HTML escaping. Nothing more.
24    escaped = html.escape(table_name)
25    return _TabMenuTemplate.substitute(table_name=escaped)
26
27
28_ExperimentFileHTML = """
29<div class='results-section'>
30  <div class='results-section-title'>Experiment File</div>
31  <div class='results-section-content'>
32    <pre>%s</pre>
33</div>
34"""
35
36
37def _GetExperimentFileHTML(experiment_file_text):
38    if not experiment_file_text:
39        return ""
40    return _ExperimentFileHTML % (
41        html.escape(experiment_file_text, quote=False),
42    )
43
44
45_ResultsSectionHTML = Template(
46    """
47<div class='results-section'>
48  <div class='results-section-title'>$sect_name</div>
49  <div class='results-section-content'>
50    <div id='${short_name}-html'>$html_table</div>
51    <div id='${short_name}-text'><pre>$text_table</pre></div>
52    <div id='${short_name}-tsv'><pre>$tsv_table</pre></div>
53  </div>
54  $tab_menu
55</div>
56"""
57)
58
59
60def _GetResultsSectionHTML(print_table, table_name, data):
61    first_word = table_name.strip().split()[0]
62    short_name = first_word.lower()
63    return _ResultsSectionHTML.substitute(
64        sect_name=table_name,
65        html_table=print_table(data, "HTML"),
66        text_table=print_table(data, "PLAIN"),
67        tsv_table=print_table(data, "TSV"),
68        tab_menu=_GetTabMenuHTML(short_name),
69        short_name=short_name,
70    )
71
72
73_MainHTML = Template(
74    """
75<html>
76<head>
77  <style type="text/css">
78    body {
79      font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
80      font-size: 12px;
81    }
82
83    pre {
84      margin: 10px;
85      color: #039;
86      font-size: 14px;
87    }
88
89    .chart {
90      display: inline;
91    }
92
93    .hidden {
94      visibility: hidden;
95    }
96
97    .results-section {
98      border: 1px solid #b9c9fe;
99      margin: 10px;
100    }
101
102    .results-section-title {
103      background-color: #b9c9fe;
104      color: #039;
105      padding: 7px;
106      font-size: 14px;
107      width: 200px;
108    }
109
110    .results-section-content {
111      margin: 10px;
112      padding: 10px;
113      overflow:auto;
114    }
115
116    #box-table-a {
117      font-size: 12px;
118      width: 480px;
119      text-align: left;
120      border-collapse: collapse;
121    }
122
123    #box-table-a th {
124      padding: 6px;
125      background: #b9c9fe;
126      border-right: 1px solid #fff;
127      border-bottom: 1px solid #fff;
128      color: #039;
129      text-align: center;
130    }
131
132    #box-table-a td {
133      padding: 4px;
134      background: #e8edff;
135      border-bottom: 1px solid #fff;
136      border-right: 1px solid #fff;
137      color: #669;
138      border-top: 1px solid transparent;
139    }
140
141    #box-table-a tr:hover td {
142      background: #d0dafd;
143      color: #339;
144    }
145
146  </style>
147  <script type='text/javascript' src='https://www.google.com/jsapi'></script>
148  <script type='text/javascript'>
149    google.load('visualization', '1', {packages:['corechart']});
150    google.setOnLoadCallback(init);
151    function init() {
152      switchTab('summary', 'html');
153      ${perf_init};
154      switchTab('full', 'html');
155      drawTable();
156    }
157    function drawTable() {
158      ${chart_js};
159    }
160    function switchTab(table, tab) {
161      document.getElementById(table + '-html').style.display = 'none';
162      document.getElementById(table + '-text').style.display = 'none';
163      document.getElementById(table + '-tsv').style.display = 'none';
164      document.getElementById(table + '-' + tab).style.display = 'block';
165    }
166  </script>
167</head>
168
169<body>
170  $summary_table
171  $perf_html
172  <div class='results-section'>
173    <div class='results-section-title'>Charts</div>
174    <div class='results-section-content'>$chart_divs</div>
175  </div>
176  $full_table
177  $experiment_file
178</body>
179</html>
180"""
181)
182
183
184# It's a bit ugly that we take some HTML things, and some non-HTML things, but I
185# need to balance prettiness with time spent making things pretty.
186def GenerateHTMLPage(
187    perf_table,
188    chart_js,
189    summary_table,
190    print_table,
191    chart_divs,
192    full_table,
193    experiment_file,
194):
195    """Generates a crosperf HTML page from the given arguments.
196
197    print_table is a two-arg function called like: print_table(t, f)
198      t is one of [summary_table, print_table, full_table]; it's the table we want
199        to format.
200      f is one of ['TSV', 'HTML', 'PLAIN']; it's the type of format we want.
201    """
202    summary_table_html = _GetResultsSectionHTML(
203        print_table, "Summary Table", summary_table
204    )
205    if perf_table:
206        perf_html = _GetResultsSectionHTML(
207            print_table, "Perf Table", perf_table
208        )
209        perf_init = "switchTab('perf', 'html')"
210    else:
211        perf_html = ""
212        perf_init = ""
213
214    full_table_html = _GetResultsSectionHTML(
215        print_table, "Full Table", full_table
216    )
217    experiment_file_html = _GetExperimentFileHTML(experiment_file)
218    return _MainHTML.substitute(
219        perf_init=perf_init,
220        chart_js=chart_js,
221        summary_table=summary_table_html,
222        perf_html=perf_html,
223        chart_divs=chart_divs,
224        full_table=full_table_html,
225        experiment_file=experiment_file_html,
226    )
227