Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions src/frequenz/sdk/microgrid/component_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1065,28 +1065,3 @@ def _validate_leaf_components(self) -> None:
raise InvalidGraphError(
f"Leaf components with graph successors: {with_successors}"
)


def _correct_graph_errors(graph: _MicrogridComponentGraph) -> None:
"""Attempt to correct errors in component graph data.

For now, this handles just the special case of graph data that is missing an
explicit grid endpoint, but has an implicit one due to one or more
components having node 0 as their parent.

Args:
graph: the graph whose data to correct (will be updated in place)
"""
# Check if there is an implicit grid endpoint with id == 0.
# This is an expected case from the API: that no explicit
# grid endpoint will be provided, but that components connected
# to the grid endpoint will have node 0 as their predecessor.
# pylint: disable=protected-access
if (
graph._graph.has_node(0)
and graph._graph.in_degree(0) == 0
and graph._graph.out_degree(0) > 0
and "type" not in graph._graph.nodes[0]
):
graph._graph.add_node(0, **asdict(Component(0, ComponentCategory.GRID)))
# pylint: enable=protected-access
3 changes: 1 addition & 2 deletions src/frequenz/sdk/microgrid/connection_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ async def _update_api(self, server_url: str) -> None:
await super()._update_api(server_url) # pylint: disable=protected-access

self._api = ApiClient(server_url)
self._metadata = await self._api.metadata()
await self._graph.refresh_from_api(self._api)
await self._initialize()

async def _initialize(self) -> None:
self._metadata = await self._api.metadata()
Expand Down
75 changes: 0 additions & 75 deletions tests/microgrid/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1426,81 +1426,6 @@ def test__validate_leaf_components(self) -> None:
graph._graph.add_edges_from([(1, 2), (1, 3), (1, 4)])
graph._validate_leaf_components()

def test_graph_correction(self) -> None:
"""Test the graph correction functionality."""
# Simple test cases for our built-in graph correction
# functionality. We test only with `refresh_from`:
# for `refresh_from_api` it suffices to test that any
# provided `correct_errors` callback gets invoked,
# which is already done in `test_refresh_from_api`.

graph = gr._MicrogridComponentGraph()
assert set(graph.components()) == set()
assert list(graph.connections()) == []

# valid graph data: no correction will be applied
graph.refresh_from(
components={
Component(1, ComponentCategory.GRID),
Component(2, ComponentCategory.METER),
},
connections={Connection(1, 2)},
correct_errors=gr._correct_graph_errors,
)
expected = {
Component(1, ComponentCategory.GRID),
Component(2, ComponentCategory.METER),
}
assert len(graph.components()) == len(expected)
assert set(graph.components()) == expected
assert list(graph.connections()) == [Connection(1, 2)]

# invalid graph data that (for now at least)
# cannot be corrected
with pytest.raises(gr.InvalidGraphError):
graph.refresh_from(
components={Component(4, ComponentCategory.METER)},
connections={Connection(3, 4)},
correct_errors=gr._correct_graph_errors,
)

# graph is still in last known good state
assert len(graph.components()) == len(expected)
assert set(graph.components()) == expected
assert list(graph.connections()) == [Connection(1, 2)]

# invalid graph data where there is no grid
# endpoint but a node has the magic value 0
# for its predecessor

# without the callback, this is identified as
# invalid
with pytest.raises(gr.InvalidGraphError):
graph.refresh_from(
components={Component(8, ComponentCategory.METER)},
connections={Connection(0, 8)},
)

# graph is still in last known good state
assert len(graph.components()) == len(expected)
assert set(graph.components()) == expected
assert list(graph.connections()) == [Connection(1, 2)]

# with the callback, this can be corrected
graph.refresh_from(
components={Component(8, ComponentCategory.METER)},
connections={Connection(0, 8)},
correct_errors=gr._correct_graph_errors,
)
expected = {
Component(8, ComponentCategory.METER),
Component(0, ComponentCategory.GRID),
}
assert len(graph.components()) == len(expected)
assert set(graph.components()) == expected

assert list(graph.connections()) == [Connection(0, 8)]


class TestComponentTypeIdentification:
"""Test the component type identification methods in the component graph."""
Expand Down
Loading