Files
next-tjwater-server/app/infra/db/timescaledb/repositories/hydraulic_playback.py
T
jiang 4c441fcbf2
Generic Container CI/CD / test-build-publish (push) Successful in 4m13s
Server CI/CD v2 / build-test-publish-and-deploy (push) Successful in 4m13s
refactor(db): isolate hydraulic playback data access
2026-11-15 23:49:03 +08:00

30 lines
1.1 KiB
Python

"""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