refactor(db): isolate hydraulic playback data access
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
"""Published network geometry used to build indexed hydraulic playback packets."""
|
||||
|
||||
from typing import Any
|
||||
|
||||
from psycopg import AsyncConnection
|
||||
|
||||
|
||||
class HydraulicGeometryRepository:
|
||||
@staticmethod
|
||||
async def get_rows(conn: AsyncConnection) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
|
||||
async with conn.cursor() as cursor:
|
||||
await cursor.execute("""
|
||||
SELECT n.id, ST_X(ST_Transform(g.geom,4326)) AS longitude,
|
||||
ST_Y(ST_Transform(g.geom,4326)) AS latitude
|
||||
FROM gis.network_nodes n JOIN gis.node_geometries g ON g.node_id=n.id
|
||||
WHERE g.geom IS NOT NULL AND NOT ST_IsEmpty(g.geom) ORDER BY n.id
|
||||
""")
|
||||
nodes = await cursor.fetchall()
|
||||
await cursor.execute("""
|
||||
SELECT id, start_node_id, diameter, ST_AsGeoJSON(ST_Transform(ST_LineMerge(geom),4326))::json AS geometry
|
||||
FROM gis.pipes WHERE geom IS NOT NULL AND NOT ST_IsEmpty(geom) ORDER BY id
|
||||
""")
|
||||
pipes = await cursor.fetchall()
|
||||
return nodes, pipes
|
||||
@@ -0,0 +1,29 @@
|
||||
"""Stream exact playback samples without materializing full-network JSON results."""
|
||||
|
||||
from collections.abc import AsyncIterator
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
from psycopg import AsyncConnection
|
||||
|
||||
|
||||
class HydraulicPlaybackRepository:
|
||||
@staticmethod
|
||||
async def iter_node_pressures(conn: AsyncConnection, times: list[datetime]) -> AsyncIterator[dict[str, Any]]:
|
||||
async with conn.cursor() as cursor:
|
||||
await cursor.execute("""
|
||||
SELECT time, node_id, pressure FROM realtime.node_results
|
||||
WHERE time = ANY(%s) ORDER BY time, node_id
|
||||
""", (times,))
|
||||
async for row in cursor:
|
||||
yield row
|
||||
|
||||
@staticmethod
|
||||
async def iter_pipe_metrics(conn: AsyncConnection, times: list[datetime]) -> AsyncIterator[dict[str, Any]]:
|
||||
async with conn.cursor() as cursor:
|
||||
await cursor.execute("""
|
||||
SELECT time, link_id, flow, velocity FROM realtime.link_results
|
||||
WHERE time = ANY(%s) ORDER BY time, link_id
|
||||
""", (times,))
|
||||
async for row in cursor:
|
||||
yield row
|
||||
Reference in New Issue
Block a user