feat(replay): execute periodic diagnosis and hydraulic checks

This commit is contained in:
2026-11-16 15:13:22 +08:00
parent 4c441fcbf2
commit 389892cb3f
22 changed files with 1934 additions and 12 deletions
+96
View File
@@ -139,6 +139,102 @@ class ScadaReplayRepository:
)
return [r["event"] for r in await cur.fetchall()]
async def queue_available(self) -> bool:
cursor = await self.connection.execute("SELECT to_regclass('workbench.calculation_jobs') IS NOT NULL AS ready")
return bool((await cursor.fetchone())["ready"])
async def periodic_events(self, replay_id, start, end, through_sequence: int) -> list[dict]:
cursor = await self.connection.execute(
"SELECT e.value AS event FROM peripheral_sample.scada_replay_batches b "
"CROSS JOIN LATERAL jsonb_array_elements(COALESCE(b.document->'events','[]'::jsonb)) e "
"WHERE b.project_id=%s AND b.replay_id=%s AND b.stage='detected' "
"AND b.sequence>0 AND b.sequence<=%s "
"AND (b.document->>'at')::timestamptz>=%s::timestamptz "
"AND (b.document->>'at')::timestamptz<=%s::timestamptz ORDER BY b.sequence",
(self.project_id, str(replay_id), through_sequence, start, end),
)
return [row["event"] for row in await cursor.fetchall()]
async def batch(self, replay_id, sequence: int) -> dict | None:
cursor = await self.connection.execute(
"SELECT document FROM peripheral_sample.scada_replay_batches WHERE project_id=%s AND replay_id=%s AND sequence=%s",
(self.project_id, str(replay_id), sequence),
)
row = await cursor.fetchone()
return row["document"] if row else None
async def periodic_tasks(self, replay_id, offset=0, limit=100) -> list[dict]:
cursor = await self.connection.execute(
"SELECT t.value AS execution FROM peripheral_sample.scada_replay_batches b "
"CROSS JOIN LATERAL jsonb_each(COALESCE(b.document->'scheduled_tasks','{}'::jsonb)) t "
"WHERE b.project_id=%s AND b.replay_id=%s AND b.stage='detected' "
"ORDER BY b.sequence DESC,t.value->>'task_key' LIMIT %s OFFSET %s",
(self.project_id, str(replay_id), limit, offset),
)
return [row["execution"] for row in await cursor.fetchall()]
async def periodic_task(self, replay_id, execution_id) -> dict | None:
cursor = await self.connection.execute(
"SELECT document->'scheduled_tasks'->%s AS execution FROM peripheral_sample.scada_replay_batches "
"WHERE project_id=%s AND replay_id=%s AND stage='detected' "
"AND document->'scheduled_tasks' ? %s LIMIT 1",
(str(execution_id), self.project_id, str(replay_id), str(execution_id)),
)
row = await cursor.fetchone()
return row["execution"] if row else None
async def previous_simulations(self, replay_id, before, limit=2) -> list[dict]:
cursor = await self.connection.execute(
"SELECT t.value AS execution FROM peripheral_sample.scada_replay_batches b "
"CROSS JOIN LATERAL jsonb_each(COALESCE(b.document->'scheduled_tasks','{}'::jsonb)) t "
"WHERE b.project_id=%s AND b.replay_id=%s AND b.stage='detected' "
"AND t.value->>'task_key'='network-simulation' AND t.value->>'status'='completed' "
"AND (t.value->>'period_end')::timestamptz<%s::timestamptz "
"ORDER BY (t.value->>'period_end')::timestamptz DESC LIMIT %s",
(self.project_id, str(replay_id), before, limit),
)
return [row["execution"] for row in await cursor.fetchall()]
async def reference_simulations(self, replay_id, period_ends: list[datetime]) -> list[dict]:
cursor = await self.connection.execute(
"SELECT t.value AS execution FROM peripheral_sample.scada_replay_batches b "
"CROSS JOIN LATERAL jsonb_each(COALESCE(b.document->'scheduled_tasks','{}'::jsonb)) t "
"WHERE b.project_id=%s AND b.replay_id=%s AND b.stage='detected' "
"AND t.value->>'task_key'='network-simulation' AND t.value->>'status'='completed' "
"AND (t.value->>'period_end')::timestamptz=ANY(%s::timestamptz[]) "
"ORDER BY (t.value->>'period_end')::timestamptz DESC LIMIT 7",
(self.project_id, str(replay_id), period_ends),
)
return [row["execution"] for row in await cursor.fetchall()]
async def save_retry_receipt(self, replay_id, sequence, request_id, execution_id) -> None:
await self.connection.execute(
"UPDATE peripheral_sample.scada_replay_batches SET document=jsonb_set(document,'{periodic_retry_receipts}',"
"COALESCE(document->'periodic_retry_receipts','{}'::jsonb) || jsonb_build_object(%s::text,%s::text)) "
"WHERE project_id=%s AND replay_id=%s AND sequence=%s",
(str(request_id), str(execution_id), self.project_id, str(replay_id), sequence),
)
async def save_periodic_task(self, replay_id, sequence: int, execution: dict) -> None:
cursor = await self.connection.execute(
"UPDATE peripheral_sample.scada_replay_batches SET document=jsonb_set(document,'{scheduled_tasks}',"
"COALESCE(document->'scheduled_tasks','{}'::jsonb) || jsonb_build_object(%s::text,%s::jsonb)) "
"WHERE project_id=%s AND replay_id=%s AND sequence=%s AND stage='detected' RETURNING sequence",
(str(execution["execution_id"]), Jsonb(execution), self.project_id, str(replay_id), sequence),
)
if await cursor.fetchone() is None:
raise ValueError("PERIOD_BATCH_UNAVAILABLE")
async def simulation_state(self, replay_id, execution: dict) -> None:
state = {"execution_id": str(execution["execution_id"]), "status": execution["status"],
"error": execution["summary"] if execution["status"] == "failed" else None}
await self.connection.execute(
"UPDATE peripheral_sample.scada_replays SET document=jsonb_set(document,'{last_scheduled_simulation}',%s),updated_at=now() "
"WHERE project_id=%s AND replay_id=%s "
"AND (document->'last_monitoring'->>'period_end')::timestamptz=%s::timestamptz",
(Jsonb(state), self.project_id, str(replay_id), execution["period_end"]),
)
async def monitoring_outputs(
self,
replay_id,
+51
View File
@@ -0,0 +1,51 @@
"""Read one consistent model without modifying the online network."""
from pathlib import Path
from tempfile import TemporaryDirectory
import base64
import gzip
from io import BytesIO
from app.native.wndb.core.connection import project_transaction
from app.native.wndb.inp.exporter import dump_inp
MAX_MODEL_BYTES = 64 * 1024 * 1024
MAX_COMPRESSED_BYTES = 8 * 1024 * 1024
class ModelSnapshotBudgetExceeded(ValueError):
pass
def pack_model_input(content: str) -> str:
raw = content.encode("utf-8")
if len(raw) > MAX_MODEL_BYTES:
raise ModelSnapshotBudgetExceeded("MODEL_SNAPSHOT_BUDGET_EXCEEDED")
compressed = gzip.compress(raw, compresslevel=6, mtime=0)
if len(compressed) > MAX_COMPRESSED_BYTES:
raise ModelSnapshotBudgetExceeded("MODEL_SNAPSHOT_BUDGET_EXCEEDED")
return base64.b64encode(compressed).decode("ascii")
def unpack_model_input(content: str) -> str:
if len(content) > (MAX_COMPRESSED_BYTES + 2) // 3 * 4:
raise ModelSnapshotBudgetExceeded("MODEL_SNAPSHOT_BUDGET_EXCEEDED")
compressed = base64.b64decode(content, validate=True)
with gzip.GzipFile(fileobj=BytesIO(compressed)) as stream:
raw = stream.read(MAX_MODEL_BYTES + 1)
if len(raw) > MAX_MODEL_BYTES:
raise ModelSnapshotBudgetExceeded("MODEL_SNAPSHOT_BUDGET_EXCEEDED")
return raw.decode("utf-8")
def capture_model_input(project: str) -> str:
with TemporaryDirectory(prefix="tjwater-model-snapshot-") as directory:
path = Path(directory) / "network.inp"
with project_transaction(project) as connection:
connection.execute("SET TRANSACTION ISOLATION LEVEL REPEATABLE READ, READ ONLY")
dump_inp(project, str(path), "2")
if path.stat().st_size > MAX_MODEL_BYTES:
raise ModelSnapshotBudgetExceeded("MODEL_SNAPSHOT_BUDGET_EXCEEDED")
content = path.read_text(encoding="utf-8")
if not content.strip():
raise ValueError("MODEL_SNAPSHOT_UNAVAILABLE")
return content