Skip to content

Commit

Permalink
Add methods to generate summary and add summary
Browse files Browse the repository at this point in the history
Signed-off-by: hoangtungdinh <[email protected]>
  • Loading branch information
hoangtungdinh committed Sep 16, 2024
1 parent f7ea664 commit a830d1a
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
66 changes: 66 additions & 0 deletions qc_baselib/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def write_to_file(self, xml_output_file_path: str) -> None:
raise RuntimeError(
"Report dump with empty report, the report needs to be loaded first"
)

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 +167,71 @@ def set_result_version(self, version: str) -> None:
else:
self._report_results.version = version

def generate_summary(self) -> None:
self._generate_checker_bundle_summary()
self._generate_checker_summary()

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:
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:
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: 13 additions & 2 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,6 +113,17 @@ def test_result_write() -> None:
status=StatusType.COMPLETED,
)

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.generate_summary()

result.write_to_file(TEST_REPORT_OUTPUT_PATH)

example_xml_text = ""
Expand Down

0 comments on commit a830d1a

Please sign in to comment.