refactor(topology): introduce TopologyRepository protocol with DTO return types

Replace repo: BaseRepository with a structural TopologyRepository protocol
in persistence.py and allocator.py. All read methods now return typed DTOs
(TopologySummary, LANRow, DeckyRow, EdgeRow) instead of raw dicts, eliminating
silent field-shape regressions across the topology subsystem.

TopologySummary gains email_personas and language_default so api_personas.py
can continue reading those fields via attribute access. hydrate() converts
DTOs to dicts before passing to _backfill_decky_configs, keeping the mutable
working-state function dict-based at its boundary. All production callers
(router handlers, mutator, CLI, heartbeat) migrated from dict/get access to
attribute access. 134 tests pass.
This commit is contained in:
2026-04-30 23:51:41 -04:00
parent 3456d3ab45
commit fc1f0914b7
34 changed files with 231 additions and 175 deletions

View File

@@ -237,6 +237,8 @@ class TopologySummary(BaseModel):
needs_resync: bool = False
created_at: datetime
status_changed_at: Optional[datetime] = None
email_personas: str = "[]"
language_default: str = "en"
class TopologyListResponse(BaseModel):

View File

@@ -1,6 +1,8 @@
from abc import ABC, abstractmethod
from typing import Any, Optional
from decnet.web.db.models.topology import DeckyRow, EdgeRow, LANRow, TopologySummary
class BaseRepository(ABC):
"""Abstract base class for DECNET web dashboard data storage."""
@@ -699,7 +701,7 @@ class BaseRepository(ABC):
async def create_topology(self, data: dict[str, Any]) -> str:
raise NotImplementedError
async def get_topology(self, topology_id: str) -> Optional[dict[str, Any]]:
async def get_topology(self, topology_id: str) -> Optional[TopologySummary]:
raise NotImplementedError
async def list_topologies(
@@ -707,7 +709,7 @@ class BaseRepository(ABC):
status: Optional[str] = None,
limit: Optional[int] = None,
offset: Optional[int] = None,
) -> list[dict[str, Any]]:
) -> list[TopologySummary]:
raise NotImplementedError
async def count_topologies(self, status: Optional[str] = None) -> int:
@@ -732,7 +734,7 @@ class BaseRepository(ABC):
) -> bool:
raise NotImplementedError
async def list_topologies_needing_resync(self) -> list[dict[str, Any]]:
async def list_topologies_needing_resync(self) -> list[TopologySummary]:
raise NotImplementedError
async def add_lan(self, data: dict[str, Any]) -> str:
@@ -750,7 +752,7 @@ class BaseRepository(ABC):
async def list_lans_for_topology(
self, topology_id: str
) -> list[dict[str, Any]]:
) -> list[LANRow]:
raise NotImplementedError
async def add_topology_decky(self, data: dict[str, Any]) -> str:
@@ -768,7 +770,7 @@ class BaseRepository(ABC):
async def list_topology_deckies(
self, topology_id: str
) -> list[dict[str, Any]]:
) -> list[DeckyRow]:
raise NotImplementedError
async def add_topology_edge(self, data: dict[str, Any]) -> str:
@@ -776,7 +778,7 @@ class BaseRepository(ABC):
async def list_topology_edges(
self, topology_id: str
) -> list[dict[str, Any]]:
) -> list[EdgeRow]:
raise NotImplementedError
async def list_topology_status_events(

View File

@@ -8,10 +8,8 @@ from typing import Any, Optional
from sqlalchemy import desc, func, select, text
from decnet.web.db.models import Topology, TopologyStatusEvent
from decnet.web.db.sqlmodel_repo._helpers import (
_deserialize_json_fields,
_serialize_json_fields,
)
from decnet.web.db.models.topology import TopologySummary
from decnet.web.db.sqlmodel_repo._helpers import _serialize_json_fields
class TopologyCoreMixin:
@@ -32,7 +30,7 @@ class TopologyCoreMixin:
await session.refresh(row)
return row.id
async def get_topology(self, topology_id: str) -> Optional[dict[str, Any]]:
async def get_topology(self, topology_id: str) -> Optional[TopologySummary]:
async with self._session() as session:
result = await session.execute(
select(Topology).where(Topology.id == topology_id)
@@ -40,15 +38,14 @@ class TopologyCoreMixin:
row = result.scalar_one_or_none()
if not row:
return None
d = row.model_dump(mode="json")
return _deserialize_json_fields(d, ("config_snapshot",))
return TopologySummary.model_validate(row.model_dump(mode="json"))
async def list_topologies(
self,
status: Optional[str] = None,
limit: Optional[int] = None,
offset: Optional[int] = None,
) -> list[dict[str, Any]]:
) -> list[TopologySummary]:
statement = select(Topology).order_by(desc(Topology.created_at))
if status:
statement = statement.where(Topology.status == status)
@@ -59,9 +56,7 @@ class TopologyCoreMixin:
async with self._session() as session:
result = await session.execute(statement)
return [
_deserialize_json_fields(
r.model_dump(mode="json"), ("config_snapshot",)
)
TopologySummary.model_validate(r.model_dump(mode="json"))
for r in result.scalars().all()
]
@@ -140,15 +135,13 @@ class TopologyCoreMixin:
await session.commit()
return True
async def list_topologies_needing_resync(self) -> list[dict[str, Any]]:
async def list_topologies_needing_resync(self) -> list[TopologySummary]:
async with self._session() as session:
result = await session.execute(
select(Topology).where(Topology.needs_resync == True) # noqa: E712
)
return [
_deserialize_json_fields(
r.model_dump(mode="json"), ("config_snapshot",)
)
TopologySummary.model_validate(r.model_dump(mode="json"))
for r in result.scalars().all()
]

View File

@@ -8,6 +8,7 @@ from typing import Any, Optional
from sqlalchemy import asc, select, text, update
from decnet.web.db.models import TopologyDecky
from decnet.web.db.models.topology import DeckyRow
from decnet.web.db.sqlmodel_repo._helpers import (
_deserialize_json_fields,
_serialize_json_fields,
@@ -110,7 +111,7 @@ class TopologyDeckiesMixin:
async def list_topology_deckies(
self, topology_id: str
) -> list[dict[str, Any]]:
) -> list[DeckyRow]:
async with self._session() as session:
result = await session.execute(
select(TopologyDecky)
@@ -118,8 +119,10 @@ class TopologyDeckiesMixin:
.order_by(asc(TopologyDecky.name))
)
return [
_deserialize_json_fields(
r.model_dump(mode="json"), ("services", "decky_config")
DeckyRow.model_validate(
_deserialize_json_fields(
r.model_dump(mode="json"), ("services", "decky_config")
)
)
for r in result.scalars().all()
]

View File

@@ -6,6 +6,7 @@ from typing import Any, Optional
from sqlalchemy import desc, select, text
from decnet.web.db.models import TopologyEdge, TopologyStatusEvent
from decnet.web.db.models.topology import EdgeRow
class TopologyEdgesMixin:
@@ -60,12 +61,12 @@ class TopologyEdgesMixin:
async def list_topology_edges(
self, topology_id: str
) -> list[dict[str, Any]]:
) -> list[EdgeRow]:
async with self._session() as session:
result = await session.execute(
select(TopologyEdge).where(TopologyEdge.topology_id == topology_id)
)
return [r.model_dump(mode="json") for r in result.scalars().all()]
return [EdgeRow.model_validate(r.model_dump(mode="json")) for r in result.scalars().all()]
async def list_topology_status_events(
self, topology_id: str, limit: int = 100

View File

@@ -6,6 +6,7 @@ from typing import Any, Optional
from sqlalchemy import asc, select, text, update
from decnet.web.db.models import LAN, TopologyEdge
from decnet.web.db.models.topology import LANRow
class LansMixin:
@@ -117,9 +118,9 @@ class LansMixin:
async def list_lans_for_topology(
self, topology_id: str
) -> list[dict[str, Any]]:
) -> list[LANRow]:
async with self._session() as session:
result = await session.execute(
select(LAN).where(LAN.topology_id == topology_id).order_by(asc(LAN.name))
)
return [r.model_dump(mode="json") for r in result.scalars().all()]
return [LANRow.model_validate(r.model_dump(mode="json")) for r in result.scalars().all()]