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