Skip to content

Commit

Permalink
[plist2html] Adding severity to statistics page
Browse files Browse the repository at this point in the history
In plist2html generator the severity is added to the static HTML
statistics page.
  • Loading branch information
bruntib committed Aug 28, 2020
1 parent 0f70419 commit 0a2f8c1
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 64 deletions.
173 changes: 109 additions & 64 deletions tools/plist_to_html/plist_to_html/PlistToHtml.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


import argparse
import io
import json
import os
import plistlib
Expand Down Expand Up @@ -149,18 +150,6 @@ def create_index_html(self, output_dir):
bug to the created html file where the bug can be found.
"""

# Create table header.
table_reports = '''
<tr>
<th id="report-id">&nbsp;</th>
<th id="file-path">File</th>
<th id="severity">Severity</th>
<th id="checker-name">Checker name</th>
<th id="message">Message</th>
<th id="bug-path-length">Bug path length</th>
<th id="review-status">Review status</th>
</tr>'''

# Sort reports based on file path levels.
report_data = []
for html_file in self.generated_html_reports:
Expand All @@ -169,45 +158,59 @@ def create_index_html(self, output_dir):
report_data = sorted(report_data,
key=lambda d: d['report']['path'])

# Create table lines.
for i, data in enumerate(report_data):
html_file = data['html_file']
report = data['report']

events = report['events']
checker = report['checkerName']
severity = report['severity']

review_status = report['reviewStatus'] \
if 'reviewStatus' in report and report['reviewStatus'] else ''

table_reports += '''
<tr>
<td>{0}</td>
<td file="{3}" line="{4}">
<a href="{1}#reportHash={2}">{3} @ Line {4}</a>
</td>
<td class="severity" severity="{5}">
<i class="severity-{5}"></i>
</td>
<td>{6}</td>
<td>{7}</td>
<td class="bug-path-length">{8}</td>
<td class="review-status review-status-{9}">{10}</td>
</tr>'''.format(i + 1,
os.path.basename(html_file),
report['reportHash'],
report['path'],
events[-1]['location']['line'],
severity.lower(),
checker,
events[-1]['message'],
len(events),
review_status.lower().replace(' ', '-'),
review_status)

substitute_data = self._tag_contents
substitute_data.update({'table_reports': table_reports})
with io.StringIO() as table_reports:
# Create table header.
table_reports.write('''
<tr>
<th id="report-id">&nbsp;</th>
<th id="file-path">File</th>
<th id="severity">Severity</th>
<th id="checker-name">Checker name</th>
<th id="message">Message</th>
<th id="bug-path-length">Bug path length</th>
<th id="review-status">Review status</th>
</tr>''')

# Create table lines.
for i, data in enumerate(report_data):
html_file = data['html_file']
report = data['report']

events = report['events']
checker = report['checkerName']
severity = report['severity']

review_status = report['reviewStatus'] \
if 'reviewStatus' in report and report['reviewStatus'] \
else ''

table_reports.write('''
<tr>
<td>{0}</td>
<td file="{3}" line="{4}">
<a href="{1}#reportHash={2}">{3} @ Line {4}</a>
</td>
<td class="severity" severity="{5}">
<i class="severity-{5}" title="{5}"></i>
</td>
<td>{6}</td>
<td>{7}</td>
<td class="bug-path-length">{8}</td>
<td class="review-status review-status-{9}">{10}</td>
</tr>'''.format(i + 1,
os.path.basename(html_file),
report['reportHash'],
report['path'],
events[-1]['location']['line'],
severity.lower(),
checker,
events[-1]['message'],
len(events),
review_status.lower().replace(' ', '-'),
review_status))

substitute_data = self._tag_contents
substitute_data.update({'table_reports': table_reports.getvalue()})

content = self._index.substitute(substitute_data)
output_path = os.path.join(output_dir, 'index.html')
Expand All @@ -220,6 +223,16 @@ def create_statistics_html(self, output_dir):
Creates an statistics.html file which contains statistics information
from the HTML generation process.
"""
def severity_order(severity):
"""
This function determines in which order severities should be
printed to the output. This function can be given via "key"
attribute to sort() function.
"""
severities = ['CRITICAL', 'HIGH', 'MEDIUM', 'LOW', 'STYLE',
'UNSPECIFIED']
return severities.index(severity)

num_of_plist_files = len(self.generated_html_reports)

num_of_reports = 0
Expand All @@ -232,22 +245,50 @@ def create_statistics_html(self, output_dir):
checker = report['checkerName']
checker_statistics[checker] += 1

rows = []
checker_statistics_content = ''
for checker_name in sorted(checker_statistics):
checker_statistics_content += '''
<tr>
<td>{0}</td>
<td>{1}</td>
</tr>
'''.format(checker_name, checker_statistics[checker_name])
rows.append([checker_name, checker_statistics[checker_name]])
checker_rows = []
severity_statistics = defaultdict(int)

with io.StringIO() as content:
for checker_name in sorted(checker_statistics):
severity = self._severity_map.get(checker_name, 'UNSPECIFIED')
content.write('''
<tr>
<td>{0}</td>
<td class="severity" severity="{1}">
<i class="severity-{1}" title="{1}"></i>
</td>
<td>{2}</td>
</tr>
'''.format(checker_name, severity.lower(),
checker_statistics[checker_name]))
checker_rows.append([checker_name, severity,
checker_statistics[checker_name]])
severity_statistics[severity] += \
checker_statistics[checker_name]
checker_statistics_content = content.getvalue()

severity_rows = []

with io.StringIO() as content:
for severity in sorted(severity_statistics, key=severity_order):
num = severity_statistics[severity]
content.write('''
<tr>
<td class="severity" severity="{0}">
<i class="severity-{0}" title="{0}"></i>
</td>
<td>{1}</td>
</tr>
'''.format(severity.lower(), num))
severity_rows.append([severity, num])
severity_statistics_content = content.getvalue()

substitute_data = self._tag_contents
substitute_data.update({
'number_of_plist_files': num_of_plist_files,
'number_of_reports': num_of_reports,
'checker_statistics': checker_statistics_content})
'checker_statistics': checker_statistics_content,
'severity_statistics': severity_statistics_content})

content = self._statistics.substitute(substitute_data)

Expand All @@ -269,8 +310,12 @@ def create_statistics_html(self, output_dir):
print(twodim_to_table(statistics_rows, False))

print("\n----==== Checker Statistics ====----")
header = ["Checker name", "Number of reports"]
print(twodim_to_table([header] + rows))
header = ["Checker name", "Severity", "Number of reports"]
print(twodim_to_table([header] + checker_rows))

print("\n----==== Severity Statistics ====----")
header = ["Severity", "Number of reports"]
print(twodim_to_table([header] + severity_rows))


def get_report_data_from_plist(plist, skip_report_handler=None,
Expand Down
5 changes: 5 additions & 0 deletions tools/plist_to_html/plist_to_html/static/css/statistics.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
table {
margin-bottom: 20px;
}

#checker-statistics .severity,
#severity-statistics .severity {
text-align: center;
}
9 changes: 9 additions & 0 deletions tools/plist_to_html/plist_to_html/static/statistics.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,19 @@ <h1>Checker statistics</h1>
<table id="checker-statistics">
<tr>
<th>Checker name</th>
<th>Severity</th>
<th>Number of reports</th>
</tr>
${checker_statistics}
</table>
<h1>Severity statistics</h1>
<table id="severity-statistics">
<tr>
<th>Severity</th>
<th>Number of reports</th>
</tr>
${severity_statistics}
</table>
</div>
</body>
</html>

0 comments on commit 0a2f8c1

Please sign in to comment.