Skip to content

Commit aceca6b

Browse files
authored
[core][fix] Use id and name for descendant count (#2271)
1 parent e83ee7a commit aceca6b

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

fixcore/fixcore/model/graph_access.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -488,31 +488,39 @@ def resolve(self) -> None:
488488
def __resolve_count_descendants(self) -> None:
489489
empty_set: Set[str] = set()
490490

491-
def count_descendants_of(identifier: str, ancestor_kind: str, path: List[str]) -> Dict[str, int]:
491+
def count_descendants_of(rid: str, rname: str, ancestor_kind: str, path: List[str]) -> Dict[str, int]:
492492
result: DefaultDict[str, int] = defaultdict(int)
493-
ancestor_path = ["ancestors", ancestor_kind, "reported", "id"]
493+
rid_path = ["ancestors", ancestor_kind, "reported", "id"]
494+
rname_path = ["ancestors", ancestor_kind, "reported", "name"]
494495
for _, elem in self.g.nodes(data=True):
495-
if value_in_path(elem, ancestor_path) == identifier:
496+
if value_in_path(elem, rid_path) == rid and value_in_path(elem, rname_path) == rname:
496497
kinds_set = elem.get("kinds_set", empty_set)
497498
extracted = value_in_path(elem, path)
498499
if "phantom_resource" not in kinds_set and isinstance(extracted, str):
499500
result[extracted] += 1
500501
return result
501502

502-
for on_kind, prop in GraphResolver.count_successors.items():
503-
for _, node in self.g.nodes(data=True):
504-
kinds = node.get("kinds_set")
505-
if kinds and on_kind in kinds:
506-
if rid := value_in_path(node, NodePath.reported_id):
507-
# descendant summary
508-
summary = count_descendants_of(rid, on_kind, prop.extract_path)
503+
empty_set = set()
504+
for _, node in self.g.nodes(data=True):
505+
kinds = node.get("kinds_set", empty_set)
506+
for on_kind, prop in GraphResolver.count_successors.items():
507+
if on_kind in kinds:
508+
if (rid := value_in_path(node, NodePath.reported_id)) and (
509+
rname := value_in_path(node, NodePath.reported_name)
510+
):
511+
# Descendant summary: we need to compare id and name.
512+
# Example AWS global region: id=us-east-1, name=global
513+
summary = count_descendants_of(rid, rname, on_kind, prop.extract_path)
509514
set_value_in_path(summary, prop.to_path, node)
510515
# descendant count
511516
total = reduce(lambda left, right: left + right, summary.values(), 0)
512517
set_value_in_path(total, NodePath.descendant_count, node)
513518
# update hash
514519
node["hash"] = GraphBuilder.content_hash(
515-
node["reported"], node.get("desired"), node.get("metadata"), node.get("kinds")
520+
node["reported"],
521+
desired=node.get("desired"),
522+
metadata=node.get("metadata"),
523+
kinds=node.get("kinds"),
516524
)
517525

518526
def __resolve(self, node_id: NodeId, node: Json) -> Json:

fixcore/tests/fixcore/db/graphdb_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ def create(
354354

355355
# adding another account without org_root does not delete the old root:
356356
_, update = await graph_db.merge_graph(create("yes or no", org_root_id=None, account_id="aws_account_2"), foo_model)
357-
assert GraphUpdate(111, 1, 0, 211, 0, 0) == update
357+
assert GraphUpdate(111, 0, 0, 211, 0, 0) == update
358358

359359
assert await graph_db.by_id(NodeId("org_root")) is not None
360360

0 commit comments

Comments
 (0)