Skip to content

Commit 5744946

Browse files
authored
handle diff summary counts at the database level (#5712)
* WIP move diff summary counts enrichment to a query * move diff repository tests around * finish query and tests * make sure counts are not null when retrieving * fix flaky test * replace summary enrichment with query * update repository tests for counts
1 parent b453fec commit 5744946

File tree

16 files changed

+697
-302
lines changed

16 files changed

+697
-302
lines changed

backend/infrahub/core/diff/combiner.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ def _combine_relationships(
342342

343343
def _copy_node_without_parents(self, node: EnrichedDiffNode) -> EnrichedDiffNode:
344344
rels_without_parents = {replace(r, nodes=set()) for r in node.relationships}
345+
for rel in rels_without_parents:
346+
rel.reset_summaries()
345347
node_without_parents = replace(node, relationships=rels_without_parents)
346348
return deepcopy(node_without_parents)
347349

@@ -351,15 +353,11 @@ def _combine_nodes(self, node_pairs: list[NodePair]) -> set[EnrichedDiffNode]:
351353
if node_pair.earlier is None:
352354
if node_pair.later is not None:
353355
copied = self._copy_node_without_parents(node_pair.later)
354-
for rel in copied.relationships:
355-
rel.reset_summaries()
356356
combined_nodes.add(copied)
357357
continue
358358
if node_pair.later is None:
359359
if node_pair.earlier is not None:
360360
copied = self._copy_node_without_parents(node_pair.earlier)
361-
for rel in copied.relationships:
362-
rel.reset_summaries()
363361
combined_nodes.add(copied)
364362
continue
365363
combined_attributes = self._combine_attributes(

backend/infrahub/core/diff/coordinator.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
from .data_check_synchronizer import DiffDataCheckSynchronizer
3131
from .enricher.aggregated import AggregatedDiffEnricher
3232
from .enricher.labels import DiffLabelsEnricher
33-
from .enricher.summary_counts import DiffSummaryCountsEnricher
3433
from .repository.repository import DiffRepository
3534

3635

@@ -66,7 +65,6 @@ def __init__(
6665
diff_combiner: DiffCombiner,
6766
conflicts_enricher: ConflictsEnricher,
6867
labels_enricher: DiffLabelsEnricher,
69-
summary_counts_enricher: DiffSummaryCountsEnricher,
7068
data_check_synchronizer: DiffDataCheckSynchronizer,
7169
conflict_transferer: DiffConflictTransferer,
7270
) -> None:
@@ -76,7 +74,6 @@ def __init__(
7674
self.diff_combiner = diff_combiner
7775
self.conflicts_enricher = conflicts_enricher
7876
self.labels_enricher = labels_enricher
79-
self.summary_counts_enricher = summary_counts_enricher
8077
self.data_check_synchronizer = data_check_synchronizer
8178
self.conflict_transferer = conflict_transferer
8279
self.lock_registry = lock.registry
@@ -152,10 +149,6 @@ async def update_branch_diff(self, base_branch: Branch, diff_branch: Branch) ->
152149
tracking_id=tracking_id,
153150
force_branch_refresh=False,
154151
)
155-
if isinstance(enriched_diffs, EnrichedDiffs):
156-
await self.summary_counts_enricher.enrich(enriched_diff_root=enriched_diffs.base_branch_diff)
157-
await self.summary_counts_enricher.enrich(enriched_diff_root=enriched_diffs.diff_branch_diff)
158-
159152
await self.diff_repo.save(enriched_diffs=enriched_diffs)
160153
await self._update_core_data_checks(enriched_diff=enriched_diffs.diff_branch_diff)
161154
log.info(f"Branch diff update complete for {base_branch.name} - {diff_branch.name}")
@@ -183,9 +176,6 @@ async def create_or_update_arbitrary_timeframe_diff(
183176
tracking_id=tracking_id,
184177
force_branch_refresh=False,
185178
)
186-
if isinstance(enriched_diffs, EnrichedDiffs):
187-
await self.summary_counts_enricher.enrich(enriched_diff_root=enriched_diffs.base_branch_diff)
188-
await self.summary_counts_enricher.enrich(enriched_diff_root=enriched_diffs.diff_branch_diff)
189179

190180
await self.diff_repo.save(enriched_diffs=enriched_diffs)
191181
await self._update_core_data_checks(enriched_diff=enriched_diffs.diff_branch_diff)
@@ -228,8 +218,6 @@ async def recalculate(
228218
earlier=current_branch_diff, later=enriched_diffs.diff_branch_diff
229219
)
230220

231-
await self.summary_counts_enricher.enrich(enriched_diff_root=enriched_diffs.base_branch_diff)
232-
await self.summary_counts_enricher.enrich(enriched_diff_root=enriched_diffs.diff_branch_diff)
233221
await self.diff_repo.save(enriched_diffs=enriched_diffs)
234222
await self._update_core_data_checks(enriched_diff=enriched_diffs.diff_branch_diff)
235223
log.info(f"Diff recalculation complete for {base_branch.name} - {diff_branch.name}")

backend/infrahub/core/diff/enricher/summary_counts.py

Lines changed: 0 additions & 105 deletions
This file was deleted.

backend/infrahub/core/diff/query/save.py

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
3737
SET diff_root.diff_branch = diff_root_map.diff_branch
3838
SET diff_root.from_time = diff_root_map.from_time
3939
SET diff_root.to_time = diff_root_map.to_time
40-
SET diff_root.num_added = diff_root_map.num_added
41-
SET diff_root.num_updated = diff_root_map.num_updated
42-
SET diff_root.num_removed = diff_root_map.num_removed
43-
SET diff_root.num_conflicts = diff_root_map.num_conflicts
44-
SET diff_root.contains_conflict = diff_root_map.contains_conflict
4540
SET diff_root.tracking_id = diff_root_map.tracking_id
4641
RETURN diff_root
4742
}
@@ -68,11 +63,6 @@ def _build_diff_root_params(self, enriched_diffs: EnrichedDiffs | EnrichedDiffsM
6863
"to_time": enriched_diff.to_time.to_string(),
6964
"uuid": enriched_diff.uuid,
7065
"tracking_id": enriched_diff.tracking_id.serialize() if enriched_diff.tracking_id else None,
71-
"num_added": enriched_diff.num_added,
72-
"num_updated": enriched_diff.num_updated,
73-
"num_removed": enriched_diff.num_removed,
74-
"num_conflicts": enriched_diff.num_conflicts,
75-
"contains_conflict": enriched_diff.contains_conflict,
7666
}
7767
)
7868
return {"diff_root_list": diff_root_list}
@@ -96,7 +86,12 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
9686
WITH root_uuid, node_map
9787
MATCH (diff_root {uuid: root_uuid})
9888
MERGE (diff_root)-[:DIFF_HAS_NODE]->(diff_node:DiffNode {uuid: node_map.node_properties.uuid})
99-
SET diff_node = node_map.node_properties
89+
SET
90+
diff_node.kind = node_map.node_properties.kind,
91+
diff_node.label = node_map.node_properties.label,
92+
diff_node.changed_at = node_map.node_properties.changed_at,
93+
diff_node.action = node_map.node_properties.action,
94+
diff_node.path_identifier = node_map.node_properties.path_identifier
10095
// -------------------------
10196
// add/remove node-level conflict
10297
// -------------------------
@@ -324,11 +319,6 @@ def _build_diff_attribute_params(self, enriched_attribute: EnrichedDiffAttribute
324319
"changed_at": enriched_attribute.changed_at.to_string(),
325320
"action": enriched_attribute.action.value,
326321
"path_identifier": enriched_attribute.path_identifier,
327-
"num_added": enriched_attribute.num_added,
328-
"num_updated": enriched_attribute.num_updated,
329-
"num_removed": enriched_attribute.num_removed,
330-
"num_conflicts": enriched_attribute.num_conflicts,
331-
"contains_conflict": enriched_attribute.contains_conflict,
332322
},
333323
"properties": property_props,
334324
}
@@ -349,11 +339,6 @@ def _build_diff_single_relationship_params(
349339
"peer_id": enriched_single_relationship.peer_id,
350340
"peer_label": enriched_single_relationship.peer_label,
351341
"path_identifier": enriched_single_relationship.path_identifier,
352-
"num_added": enriched_single_relationship.num_added,
353-
"num_updated": enriched_single_relationship.num_updated,
354-
"num_removed": enriched_single_relationship.num_removed,
355-
"num_conflicts": enriched_single_relationship.num_conflicts,
356-
"contains_conflict": enriched_single_relationship.contains_conflict,
357342
},
358343
"conflict_params": conflict_params,
359344
"properties": property_props,
@@ -375,11 +360,6 @@ def _build_diff_relationship_params(self, enriched_relationship: EnrichedDiffRel
375360
else None,
376361
"action": enriched_relationship.action,
377362
"path_identifier": enriched_relationship.path_identifier,
378-
"num_added": enriched_relationship.num_added,
379-
"num_updated": enriched_relationship.num_updated,
380-
"num_removed": enriched_relationship.num_removed,
381-
"num_conflicts": enriched_relationship.num_conflicts,
382-
"contains_conflict": enriched_relationship.contains_conflict,
383363
},
384364
"relationships": single_relationship_props,
385365
}
@@ -402,11 +382,6 @@ def _build_diff_node_params(self, enriched_node: EnrichedDiffNode) -> dict[str,
402382
"changed_at": enriched_node.changed_at.to_string() if enriched_node.changed_at else None,
403383
"action": enriched_node.action.value,
404384
"path_identifier": enriched_node.path_identifier,
405-
"num_added": enriched_node.num_added,
406-
"num_updated": enriched_node.num_updated,
407-
"num_removed": enriched_node.num_removed,
408-
"num_conflicts": enriched_node.num_conflicts,
409-
"contains_conflict": enriched_node.contains_conflict,
410385
},
411386
"conflict_params": conflict_params,
412387
"attributes": attribute_props,

0 commit comments

Comments
 (0)