Files
next-tjwater-server/app/infra/db/postgresql/network_assets.py
T

46 lines
1.5 KiB
Python

"""Read repositories for project network assets."""
from typing import Any
from psycopg import AsyncConnection
class NetworkAssetRepository:
@staticmethod
async def get_isolation_model(conn: AsyncConnection) -> list[dict[str, Any]]:
"""Read source nodes, link state and pipe lengths in one model snapshot."""
from app.infra.db.postgresql.read_queries import read_rows
return await read_rows(conn, """
SELECT l.id, l.link_type::text AS kind,
l.start_node_id, l.end_node_id, p.length, p.diameter,
COALESCE(s.status::text, p.status::text, '') AS status
FROM network.links l
LEFT JOIN network.pipes p ON p.link_id = l.id
LEFT JOIN network.link_initial_settings s ON s.link_id = l.id
UNION ALL
SELECT n.id, n.node_type::text, NULL, NULL, NULL, NULL, ''
FROM network.nodes n
WHERE n.node_type::text IN ('reservoir', 'tank')
""")
@staticmethod
async def get_pipes_by_ids(
conn: AsyncConnection,
pipe_ids: list[str],
) -> list[dict[str, Any]]:
if not pipe_ids:
return []
async with conn.cursor() as cur:
await cur.execute(
"""
SELECT id, diameter, start_node_id AS node1,
end_node_id AS node2
FROM gis.pipes
WHERE id = ANY(%s)
ORDER BY id
""",
(pipe_ids,),
)
return await cur.fetchall()