Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods to generate summary and add summary #37

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 80 additions & 1 deletion qc_baselib/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,21 @@ def load_from_file(self, xml_file_path: str, override: bool = False) -> None:
xml_text = report_xml_file.read()
self._report_results = result.CheckerResults.from_xml(xml_text)

def write_to_file(self, xml_output_file_path: str) -> None:
def write_to_file(self, xml_output_file_path: str, generate_summary=False) -> None:
"""
generate_summary : bool
Automatically generate a summary for each checker and checker bundle.
The generated summary will be appended to the current summary.
"""
if self._report_results is None:
raise RuntimeError(
"Report dump with empty report, the report needs to be loaded first"
)

if generate_summary:
self._generate_checker_bundle_summary()
self._generate_checker_summary()

with open(xml_output_file_path, "wb") as report_xml_file:
xml_text = self._report_results.to_xml(
pretty_print=True,
Expand Down Expand Up @@ -166,6 +176,75 @@ def set_result_version(self, version: str) -> None:
else:
self._report_results.version = version

def _generate_checker_bundle_summary(self) -> None:
for bundle in self._report_results.checker_bundles:
number_of_checkers = 0
number_of_completed_checkers = 0
number_of_skipped_checkers = 0
number_of_error_checkers = 0
number_of_no_status_checkers = 0

for checker in bundle.checkers:
number_of_checkers += 1
if checker.status == StatusType.COMPLETED:
number_of_completed_checkers += 1
elif checker.status == StatusType.SKIPPED:
number_of_skipped_checkers += 1
elif checker.status == StatusType.ERROR:
number_of_error_checkers += 1
else:
number_of_no_status_checkers += 1

summary = (
f"{number_of_checkers} checker(s) are executed. "
f"{number_of_completed_checkers} checker(s) are completed. {number_of_skipped_checkers} checker(s) are skipped. "
f"{number_of_error_checkers} checker(s) have internal error and {number_of_no_status_checkers} checker(s) do not contain status."
)

if bundle.summary == "":
bundle.summary = summary
else:
bundle.summary += f" {summary}"

def _generate_checker_summary(self) -> None:
for bundle in self._report_results.checker_bundles:
for checker in bundle.checkers:
number_of_issues = len(checker.issues)

summary = f"{number_of_issues} issue(s) are found."

if checker.summary == "":
checker.summary = summary
else:
checker.summary += f" {summary}"

def add_checker_bundle_summary(
self, checker_bundle_name: str, content: str
) -> None:
"""
Add content to the existing summary of a checker bundle.
The content will be appended to the current summary.
"""
bundle = self._get_checker_bundle(checker_bundle_name)
if bundle.summary == "":
bundle.summary = content
else:
bundle.summary += f" {content}"

def add_checker_summary(
self, checker_bundle_name: str, checker_id: str, content: str
) -> None:
"""
Add content to the existing summary of a checker.
The content will be appended to the current summary.
"""
bundle = self._get_checker_bundle(checker_bundle_name)
checker = self._get_checker(bundle, checker_id)
if checker.summary == "":
checker.summary = content
else:
checker.summary += f" {content}"

def _get_checker_bundle(self, checker_bundle_name: str) -> result.CheckerBundleType:
if self._report_results is None:
raise RuntimeError(
Expand Down
4 changes: 2 additions & 2 deletions tests/data/result_test_output.xqar
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<CheckerResults version="0.0.1">
<CheckerBundle build_date="2024-05-31" description="Example checker bundle" name="TestBundle" version="0.0.1" summary="Tested example checkers">
<Checker status="completed" checkerId="TestChecker" description="Test checker" summary="Executed evaluation">
<CheckerBundle build_date="2024-05-31" description="Example checker bundle" name="TestBundle" version="0.0.1" summary="Tested example checkers. Extra summary for checker bundle. 1 checker(s) are executed. 1 checker(s) are completed. 0 checker(s) are skipped. 0 checker(s) have internal error and 0 checker(s) do not contain status.">
<Checker status="completed" checkerId="TestChecker" description="Test checker" summary="Executed evaluation. Extra summary for checker. 1 issue(s) are found.">
<AddressedRule ruleUID="test.com:qc:1.0.0:qwerty.qwerty"/>
<Issue issueId="0" description="Issue found at odr" level="3" ruleUID="test.com:qc:1.0.0:qwerty.qwerty">
<Locations description="Location for issue">
Expand Down
15 changes: 12 additions & 3 deletions tests/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ def test_result_write() -> None:
build_date="2024-05-31",
description="Example checker bundle",
version="0.0.1",
summary="Tested example checkers",
summary="Tested example checkers.",
)

result.register_checker(
checker_bundle_name="TestBundle",
checker_id="TestChecker",
description="Test checker",
summary="Executed evaluation",
summary="Executed evaluation.",
)

rule_uid = result.register_rule(
Expand Down Expand Up @@ -113,7 +113,16 @@ def test_result_write() -> None:
status=StatusType.COMPLETED,
)

result.write_to_file(TEST_REPORT_OUTPUT_PATH)
result.add_checker_bundle_summary(
checker_bundle_name="TestBundle", content="Extra summary for checker bundle."
)
result.add_checker_summary(
checker_bundle_name="TestBundle",
checker_id="TestChecker",
content="Extra summary for checker.",
)

result.write_to_file(TEST_REPORT_OUTPUT_PATH, generate_summary=True)

example_xml_text = ""
output_xml_text = ""
Expand Down