Files
next-tjwater-server/app/infra/epanet/model_snapshot.py
T

52 lines
2.0 KiB
Python

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