Skip to content

Commit

Permalink
[core][fix] Stable node ids for benchmark results (#2114)
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamatthias authored Jun 14, 2024
1 parent c362e70 commit 4df872b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
9 changes: 6 additions & 3 deletions fixcore/fixcore/report/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"],
)


Expand All @@ -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(
Expand All @@ -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:
Expand Down Expand Up @@ -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]:
Expand Down
27 changes: 21 additions & 6 deletions fixcore/fixcore/report/inspector_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,31 +421,46 @@ 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,
accounts=context.accounts,
only_failed=context.only_failed,
severity=context.severity,
id=benchmark.id,
node_id=next_node_id(),
)

async def __perform_checks( # type: ignore
Expand Down

0 comments on commit 4df872b

Please sign in to comment.