Skip to content

Commit

Permalink
remove conflicts from enriched nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
ajtmccarty committed Jul 25, 2024
1 parent 4ce76f3 commit dac7870
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 118 deletions.
23 changes: 0 additions & 23 deletions backend/infrahub/core/diff/model/path.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

from dataclasses import dataclass, field, replace
from enum import Enum
from typing import TYPE_CHECKING, Any, Optional

from infrahub.core.constants import DiffAction, RelationshipStatus
Expand All @@ -15,34 +14,13 @@
from neo4j.graph import Relationship as Neo4jRelationship


class ConflictBranchChoice(Enum):
BASE = "base"
DIFF = "diff"


@dataclass
class EnrichedDiffPropertyConflict:
uuid: str
base_branch_action: DiffAction
base_branch_value: Any
base_branch_changed_at: Timestamp
diff_branch_action: DiffAction
diff_branch_value: Any
diff_branch_changed_at: Timestamp
selected_branch: Optional[ConflictBranchChoice]

def __hash__(self) -> int:
return hash(self.uuid)


@dataclass
class EnrichedDiffProperty:
property_type: str
changed_at: Timestamp
previous_value: Any
new_value: Any
action: DiffAction
conflict: Optional[EnrichedDiffPropertyConflict]

def __hash__(self) -> int:
return hash(self.property_type)
Expand All @@ -64,7 +42,6 @@ class EnrichedDiffSingleRelationship:
changed_at: Timestamp
action: DiffAction
peer_id: str
conflict: Optional[EnrichedDiffPropertyConflict]
properties: set[EnrichedDiffProperty] = field(default_factory=set)

def __hash__(self) -> int:
Expand Down
56 changes: 9 additions & 47 deletions backend/infrahub/core/diff/repository/get_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
from infrahub.database.constants import DatabaseType

from ..model.path import (
ConflictBranchChoice,
EnrichedDiffAttribute,
EnrichedDiffNode,
EnrichedDiffProperty,
EnrichedDiffPropertyConflict,
EnrichedDiffRelationship,
EnrichedDiffRoot,
EnrichedDiffSingleRelationship,
Expand Down Expand Up @@ -127,12 +125,10 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
OPTIONAL MATCH (diff_node)-[:DIFF_HAS_ATTRIBUTE]->(diff_attribute:DiffAttribute)
WITH diff_attribute
OPTIONAL MATCH (diff_attribute)-[:DIFF_HAS_PROPERTY]->(diff_attr_property:DiffProperty)
WITH diff_attribute, diff_attr_property
OPTIONAL MATCH (diff_attr_property)-[:DIFF_HAS_CONFLICT]->(diff_attr_conflict:DiffConflict)
RETURN diff_attribute, diff_attr_property, diff_attr_conflict
RETURN diff_attribute, diff_attr_property
ORDER BY diff_attribute.name, diff_attr_property.property_type
}
WITH diff_root, parent_node_uuid, parent_rel_name, diff_node, collect([diff_attribute, diff_attr_property, diff_attr_conflict]) as diff_attributes
WITH diff_root, parent_node_uuid, parent_rel_name, diff_node, collect([diff_attribute, diff_attr_property]) as diff_attributes
// relationships
CALL {
Expand All @@ -141,12 +137,9 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
WITH diff_relationship
OPTIONAL MATCH (diff_relationship)-[:DIFF_HAS_ELEMENT]->(diff_rel_element:DiffRelationshipElement)
WITH diff_relationship, diff_rel_element
OPTIONAL MATCH (diff_rel_element)-[:DIFF_HAS_CONFLICT]->(diff_rel_element_conflict:DiffConflict)
WITH diff_relationship, diff_rel_element, diff_rel_element_conflict
OPTIONAL MATCH (diff_rel_element)-[:DIFF_HAS_PROPERTY]->(diff_rel_property:DiffProperty)
WITH diff_relationship, diff_rel_element, diff_rel_element_conflict, diff_rel_property
OPTIONAL MATCH (diff_rel_property)-[:DIFF_HAS_CONFLICT]->(diff_rel_conflict:DiffConflict)
RETURN diff_relationship, diff_rel_element, diff_rel_element_conflict, diff_rel_property, diff_rel_conflict
WITH diff_relationship, diff_rel_element, diff_rel_property
RETURN diff_relationship, diff_rel_element, diff_rel_property
ORDER BY diff_relationship.name, diff_rel_element.peer_id, diff_rel_property.property_type
}
WITH
Expand All @@ -155,7 +148,7 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
parent_rel_name,
diff_node,
diff_attributes,
collect([diff_relationship, diff_rel_element, diff_rel_element_conflict, diff_rel_property, diff_rel_conflict]) AS diff_relationships
collect([diff_relationship, diff_rel_element, diff_rel_property]) AS diff_relationships
"""
self.add_to_query(query=query_2)

Expand Down Expand Up @@ -205,49 +198,42 @@ def _deserialize_attributes(
self, result: QueryResult, enriched_root: EnrichedDiffRoot, enriched_node: EnrichedDiffNode
) -> None:
for attribute_result in result.get_nested_node_collection("diff_attributes"):
diff_attr_node, diff_attr_property_node, diff_attr_conflict_node = attribute_result
diff_attr_node, diff_attr_property_node = attribute_result
if diff_attr_node is None or diff_attr_property_node is None:
continue
enriched_attribute = self._deserialize_diff_attr(
diff_attr_node=diff_attr_node, enriched_root=enriched_root, enriched_node=enriched_node
)
enriched_property = self._deserialize_diff_attr_property(
self._deserialize_diff_attr_property(
diff_attr_property_node=diff_attr_property_node,
enriched_attr=enriched_attribute,
enriched_node=enriched_node,
enriched_root=enriched_root,
)
if diff_attr_conflict_node:
enriched_conflict = self._conflict_node_to_enriched_conflict(conflict_node=diff_attr_conflict_node)
enriched_property.conflict = enriched_conflict

def _deserialize_relationships(
self, result: QueryResult, enriched_root: EnrichedDiffRoot, enriched_node: EnrichedDiffNode
) -> None:
for relationship_result in result.get_nested_node_collection("diff_relationships"):
group_node, element_node, element_conflict, property_node, conflict_node = relationship_result
group_node, element_node, property_node = relationship_result
if group_node is None or element_node is None or property_node is None:
continue
enriched_relationship_group = self._deserialize_diff_relationship_group(
relationship_group_node=group_node, enriched_root=enriched_root, enriched_node=enriched_node
)
enriched_relationship_element = self._deserialize_diff_relationship_element(
relationship_element_node=element_node,
relationship_element_conflict_node=element_conflict,
enriched_relationship_group=enriched_relationship_group,
enriched_node=enriched_node,
enriched_root=enriched_root,
)
enriched_property = self._deserialize_diff_relationship_element_property(
self._deserialize_diff_relationship_element_property(
relationship_element_property_node=property_node,
enriched_relationship_element=enriched_relationship_element,
enriched_relationship_group=enriched_relationship_group,
enriched_node=enriched_node,
enriched_root=enriched_root,
)
if conflict_node:
enriched_conflict = self._conflict_node_to_enriched_conflict(conflict_node=conflict_node)
enriched_property.conflict = enriched_conflict

def _track_child_nodes(
self, result: QueryResult, enriched_root: EnrichedDiffRoot, enriched_node: EnrichedDiffNode
Expand Down Expand Up @@ -341,7 +327,6 @@ def _deserialize_diff_relationship_group(
def _deserialize_diff_relationship_element(
self,
relationship_element_node: Neo4jNode,
relationship_element_conflict_node: Neo4jNode | None,
enriched_relationship_group: EnrichedDiffRelationship,
enriched_node: EnrichedDiffNode,
enriched_root: EnrichedDiffRoot,
Expand All @@ -360,12 +345,7 @@ def _deserialize_diff_relationship_element(
changed_at=Timestamp(str(relationship_element_node.get("changed_at"))),
action=DiffAction(str(relationship_element_node.get("action"))),
peer_id=diff_element_peer_id,
conflict=None,
)
if relationship_element_conflict_node is not None:
enriched_rel_element.conflict = self._conflict_node_to_enriched_conflict(
conflict_node=relationship_element_conflict_node
)
enriched_relationship_group.relationships.add(enriched_rel_element)
self._diff_node_rel_element_map[rel_element_key] = enriched_rel_element
return enriched_rel_element
Expand All @@ -379,7 +359,6 @@ def _property_node_to_enriched_property(self, property_node: Neo4jNode) -> Enric
previous_value=previous_value,
new_value=new_value,
action=DiffAction(str(property_node.get("action"))),
conflict=None,
)

def _deserialize_diff_attr_property(
Expand Down Expand Up @@ -422,20 +401,3 @@ def _deserialize_diff_relationship_element_property(
self._diff_prop_map[rel_property_key] = enriched_property
enriched_relationship_element.properties.add(enriched_property)
return enriched_property

def _conflict_node_to_enriched_conflict(self, conflict_node: Neo4jNode) -> EnrichedDiffPropertyConflict:
base_value = self._get_str_or_none_property_value(node=conflict_node, property_name="base_branch_value")
branch_value = self._get_str_or_none_property_value(node=conflict_node, property_name="diff_branch_value")
selected_branch_value = self._get_str_or_none_property_value(
node=conflict_node, property_name="selected_branch"
)
return EnrichedDiffPropertyConflict(
uuid=str(conflict_node.get("uuid")),
base_branch_action=DiffAction(str(conflict_node.get("base_branch_action"))),
base_branch_value=base_value,
base_branch_changed_at=Timestamp(str(conflict_node.get("base_branch_changed_at"))),
diff_branch_action=DiffAction(str(conflict_node.get("diff_branch_action"))),
diff_branch_value=branch_value,
diff_branch_changed_at=Timestamp(str(conflict_node.get("diff_branch_changed_at"))),
selected_branch=ConflictBranchChoice(selected_branch_value) if selected_branch_value else None,
)
44 changes: 0 additions & 44 deletions backend/infrahub/core/diff/repository/save_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
EnrichedDiffAttribute,
EnrichedDiffNode,
EnrichedDiffProperty,
EnrichedDiffPropertyConflict,
EnrichedDiffRelationship,
EnrichedDiffRoot,
EnrichedDiffSingleRelationship,
Expand Down Expand Up @@ -51,12 +50,6 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
UNWIND node_attribute.properties AS attr_property
CREATE (diff_attribute)-[:DIFF_HAS_PROPERTY]->(diff_attr_prop:DiffProperty)
SET diff_attr_prop = attr_property.node_properties
// node attribute property conflicts
WITH attr_property, diff_attr_prop
FOREACH (i in CASE WHEN attr_property.conflict IS NOT NULL THEN [1] ELSE [] END |
CREATE (diff_attr_prop)-[:DIFF_HAS_CONFLICT]->(diff_attr_conflict:DiffConflict)
SET diff_attr_conflict = attr_property.conflict.node_properties
)
}
// relationships
Expand All @@ -72,23 +65,12 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
UNWIND node_relationship.relationships as node_single_relationship
CREATE (diff_relationship)-[:DIFF_HAS_ELEMENT]->(diff_relationship_element:DiffRelationshipElement)
SET diff_relationship_element = node_single_relationship.node_properties
WITH diff_relationship_element, node_single_relationship
FOREACH (i in CASE WHEN node_single_relationship.conflict IS NOT NULL THEN [1] ELSE [] END |
CREATE (diff_relationship_element)-[:DIFF_HAS_CONFLICT]->(diff_rel_elem_conflict:DiffConflict)
SET diff_rel_elem_conflict = node_single_relationship.conflict.node_properties
)
// node relationship properties
WITH diff_relationship_element, node_single_relationship
UNWIND node_single_relationship.properties as node_relationship_property
CREATE (diff_relationship_element)-[:DIFF_HAS_PROPERTY]->(diff_relationship_property:DiffProperty)
SET diff_relationship_property = node_relationship_property.node_properties
// node relationship property conflicts
WITH diff_relationship_property, node_relationship_property
FOREACH (i in CASE WHEN node_relationship_property.conflict IS NOT NULL THEN [1] ELSE [] END |
CREATE (diff_relationship_property)-[:DIFF_HAS_CONFLICT]->(diff_rel_conflict:DiffConflict)
SET diff_rel_conflict = node_relationship_property.conflict.node_properties
)
}
WITH diff_root
UNWIND $node_parent_links AS node_parent_link
Expand All @@ -102,28 +84,7 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
self.add_to_query(query=query)
self.return_labels = ["diff_root.uuid"]

def _build_diff_conflict_params(self, enriched_conflict: EnrichedDiffPropertyConflict) -> dict[str, Any]:
if enriched_conflict.selected_branch is None:
selected_branch = None
else:
selected_branch = enriched_conflict.selected_branch.value
return {
"node_properties": {
"uuid": enriched_conflict.uuid,
"base_branch_action": enriched_conflict.base_branch_action.value,
"base_branch_value": enriched_conflict.base_branch_value,
"base_branch_changed_at": enriched_conflict.base_branch_changed_at.to_string(),
"diff_branch_action": enriched_conflict.diff_branch_action.value,
"diff_branch_value": enriched_conflict.diff_branch_value,
"diff_branch_changed_at": enriched_conflict.diff_branch_changed_at.to_string(),
"selected_branch": selected_branch,
}
}

def _build_diff_property_params(self, enriched_property: EnrichedDiffProperty) -> dict[str, Any]:
conflict_props = None
if enriched_property.conflict:
conflict_props = self._build_diff_conflict_params(enriched_conflict=enriched_property.conflict)
return {
"node_properties": {
"property_type": enriched_property.property_type,
Expand All @@ -132,7 +93,6 @@ def _build_diff_property_params(self, enriched_property: EnrichedDiffProperty) -
"new_value": enriched_property.new_value,
"action": enriched_property.action,
},
"conflict": conflict_props,
}

def _build_diff_attribute_params(self, enriched_attribute: EnrichedDiffAttribute) -> dict[str, Any]:
Expand All @@ -154,16 +114,12 @@ def _build_diff_single_relationship_params(
property_props = [
self._build_diff_property_params(enriched_property=prop) for prop in enriched_single_relationship.properties
]
conflict_props = None
if enriched_single_relationship.conflict:
conflict_props = self._build_diff_conflict_params(enriched_conflict=enriched_single_relationship.conflict)
return {
"node_properties": {
"changed_at": enriched_single_relationship.changed_at.to_string(),
"action": enriched_single_relationship.action,
"peer_id": enriched_single_relationship.peer_id,
},
"conflict": conflict_props,
"properties": property_props,
}

Expand Down
4 changes: 0 additions & 4 deletions backend/tests/unit/core/diff/test_diff_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
EnrichedDiffAttribute,
EnrichedDiffNode,
EnrichedDiffProperty,
EnrichedDiffPropertyConflict,
EnrichedDiffRelationship,
EnrichedDiffRoot,
EnrichedDiffSingleRelationship,
Expand All @@ -24,9 +23,6 @@
from infrahub.database import InfrahubDatabase


class ConflictFactory(DataclassFactory[EnrichedDiffPropertyConflict]): ...


class PropertyFactory(DataclassFactory[EnrichedDiffProperty]): ...


Expand Down

0 comments on commit dac7870

Please sign in to comment.