From 4df872b7174e5265bdcee4e271ccc17b68326fca Mon Sep 17 00:00:00 2001 From: Matthias Veit Date: Fri, 14 Jun 2024 16:13:59 +0200 Subject: [PATCH] [core][fix] Stable node ids for benchmark results (#2114) --- fixcore/fixcore/report/__init__.py | 9 ++++--- fixcore/fixcore/report/inspector_service.py | 27 ++++++++++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/fixcore/fixcore/report/__init__.py b/fixcore/fixcore/report/__init__.py index ea3354cf95..ca0549a67d 100644 --- a/fixcore/fixcore/report/__init__.py +++ b/fixcore/fixcore/report/__init__.py @@ -12,7 +12,7 @@ from fixcore.ids import ConfigId, GraphName from fixcore.model.typed_model import to_js from fixcore.types import Json -from fixcore.util import uuid_str, if_set, partition_by +from fixcore.util import if_set, partition_by log = logging.getLogger(__name__) @@ -173,7 +173,7 @@ class CheckResult: check: ReportCheck number_of_resources_failing_by_account: Dict[str, int] resources_failing_by_account: Dict[str, List[Json]] - node_id: str = field(init=False, factory=uuid_str) + node_id: str @property def number_of_resources_failing(self) -> int: @@ -213,6 +213,7 @@ def from_node(js: Json) -> CheckResult: ), number_of_resources_failing_by_account=reported.get("number_of_resources_failing_by_account", {}), resources_failing_by_account=reported.get("resources_failing_by_account", {}), + node_id=js["id"], ) @@ -223,7 +224,7 @@ class CheckCollectionResult: documentation: Optional[str] = field(default=None, kw_only=True) checks: List[CheckResult] = field(factory=list, kw_only=True) children: List[CheckCollectionResult] = field(factory=list, kw_only=True) - node_id: str = field(init=False, factory=uuid_str) + node_id: str def to_node(self) -> Json: return dict( @@ -245,6 +246,7 @@ def from_node(js: Json) -> CheckCollectionResult: title=reported["title"], description=reported["description"], documentation=reported.get("documentation"), + node_id=js["id"], ) def is_empty(self) -> bool: @@ -342,6 +344,7 @@ def from_node(js: Json) -> BenchmarkResult: accounts=reported["accounts"], only_failed=reported["only_failed"], severity=if_set(reported.get("severity"), ReportSeverity), + node_id=js["id"], ) def to_graph(self, only_checks: bool = False) -> List[Json]: diff --git a/fixcore/fixcore/report/inspector_service.py b/fixcore/fixcore/report/inspector_service.py index 46cfd899f9..0d45bcec33 100644 --- a/fixcore/fixcore/report/inspector_service.py +++ b/fixcore/fixcore/report/inspector_service.py @@ -421,24 +421,38 @@ def __to_result( results: Dict[str, SingleCheckResult], context: CheckContext, ) -> BenchmarkResult: + node_id_counter = 0 + # create a unique prefix for benchmark with provided accounts + node_id_prefix = "b" + uuid_str(benchmark.id + "".join(set(context.accounts)) if context.accounts else "")[0:8] + + def next_node_id() -> str: + nonlocal node_id_counter + node_id_counter += 1 + return f"{node_id_prefix}{node_id_counter:05d}" + def to_result(cc: CheckCollection) -> CheckCollectionResult: check_results = [] for cid in cc.checks or []: if (check := check_by_id.get(cid)) is not None: result = results.get(cid, {}) count_by_account = {uid: len(failed) for uid, failed in result.items()} - check_results.append(CheckResult(check, count_by_account, result)) + check_results.append(CheckResult(check, count_by_account, result, next_node_id())) children = [to_result(c) for c in cc.children or []] return CheckCollectionResult( - cc.title, cc.description, documentation=cc.documentation, checks=check_results, children=children + cc.title, + cc.description, + documentation=cc.documentation, + checks=check_results, + children=children, + node_id=next_node_id(), ) top = to_result(benchmark).filter_result(context.only_failed) return BenchmarkResult( - benchmark.title, - benchmark.description, - benchmark.framework, - benchmark.version, + title=benchmark.title, + description=benchmark.description, + framework=benchmark.framework, + version=benchmark.version, documentation=benchmark.documentation, checks=top.checks, children=top.children, @@ -446,6 +460,7 @@ def to_result(cc: CheckCollection) -> CheckCollectionResult: only_failed=context.only_failed, severity=context.severity, id=benchmark.id, + node_id=next_node_id(), ) async def __perform_checks( # type: ignore