Reorganize WNDB by responsibility and remove legacy scheme endpoints.\n\nRoute analysis and time-series access through project pools, preserve transactional realtime replacement, and refresh GIS materialized views after writes.\n\nAdd database architecture documentation, live pooling coverage, API contract updates, and executable container verification.\n\nBREAKING CHANGE: legacy scheme APIs and flat app.native.wndb module imports are removed.
253 lines
11 KiB
Python
253 lines
11 KiB
Python
from typing import Any
|
|
|
|
from ..core.database import (
|
|
ChangeSet,
|
|
DatabaseCommand,
|
|
execute_command,
|
|
g_add_prefix,
|
|
g_delete_prefix,
|
|
g_update_prefix,
|
|
read_all,
|
|
sql_literal,
|
|
try_read,
|
|
)
|
|
from ..gis.coordinates import (
|
|
get_node_coord,
|
|
sql_delete_coord,
|
|
sql_insert_coord,
|
|
sql_update_coord,
|
|
)
|
|
from .elements import get_all_node_links, get_node_links
|
|
|
|
|
|
OVERFLOW_YES = 'YES'
|
|
OVERFLOW_NO = 'NO'
|
|
|
|
|
|
def get_tank_schema(name: str) -> dict[str, dict[str, Any]]:
|
|
return { 'id' : {'type': 'str' , 'optional': False , 'readonly': True },
|
|
'x' : {'type': 'float' , 'optional': False , 'readonly': False},
|
|
'y' : {'type': 'float' , 'optional': False , 'readonly': False},
|
|
'elevation' : {'type': 'float' , 'optional': False , 'readonly': False},
|
|
'init_level' : {'type': 'float' , 'optional': False , 'readonly': False},
|
|
'min_level' : {'type': 'float' , 'optional': False , 'readonly': False},
|
|
'max_level' : {'type': 'float' , 'optional': False , 'readonly': False},
|
|
'diameter' : {'type': 'float' , 'optional': False , 'readonly': False},
|
|
'min_vol' : {'type': 'float' , 'optional': False , 'readonly': False},
|
|
'vol_curve' : {'type': 'str' , 'optional': True , 'readonly': False},
|
|
'overflow' : {'type': 'str' , 'optional': True , 'readonly': False},
|
|
'links' : {'type': 'str_list' , 'optional': False , 'readonly': True } }
|
|
|
|
|
|
def get_tank(name: str, id: str) -> dict[str, Any]:
|
|
t = try_read(name, "select node_id as id, elevation, initial_level as init_level, minimum_level as min_level, maximum_level as max_level, diameter, minimum_volume as min_vol, volume_curve_id as vol_curve, overflow from network.tanks where node_id = %s", (id,))
|
|
if t == None:
|
|
return {}
|
|
xy = get_node_coord(name, id)
|
|
d = {}
|
|
d['id'] = str(t['id'])
|
|
d['x'] = float(xy['x'])
|
|
d['y'] = float(xy['y'])
|
|
d['elevation'] = float(t['elevation'])
|
|
d['init_level'] = float(t['init_level'])
|
|
d['min_level'] = float(t['min_level'])
|
|
d['max_level'] = float(t['max_level'])
|
|
d['diameter'] = float(t['diameter'])
|
|
d['min_vol'] = float(t['min_vol'])
|
|
d['vol_curve'] = str(t['vol_curve']) if t['vol_curve'] != None else None
|
|
d['overflow'] = str(t['overflow']) if t['overflow'] != None else None
|
|
d['links'] = get_node_links(name, id)
|
|
return d
|
|
|
|
# DingZQ, 2025-03-29
|
|
def get_all_tanks(name: str) -> list[dict[str, Any]]:
|
|
rows = read_all(
|
|
name,
|
|
"SELECT id, elevation, initial_level AS init_level, "
|
|
"minimum_level AS min_level, maximum_level AS max_level, diameter, "
|
|
"minimum_volume AS min_vol, volume_curve_id AS vol_curve, overflow, "
|
|
"x, y FROM gis.tanks ORDER BY id",
|
|
)
|
|
if rows == None:
|
|
return []
|
|
|
|
links_by_node = get_all_node_links(name)
|
|
result = []
|
|
for row in rows:
|
|
d = {}
|
|
id = str(row['id'])
|
|
d['id'] = id
|
|
d['x'] = float(row['x'] or 0.0)
|
|
d['y'] = float(row['y'] or 0.0)
|
|
d['elevation'] = float(row['elevation'])
|
|
d['init_level'] = float(row['init_level'])
|
|
d['min_level'] = float(row['min_level'])
|
|
d['max_level'] = float(row['max_level'])
|
|
d['diameter'] = float(row['diameter'])
|
|
d['min_vol'] = float(row['min_vol'])
|
|
d['vol_curve'] = str(row['vol_curve']) if row['vol_curve'] != None else None
|
|
d['overflow'] = str(row['overflow']) if row['overflow'] != None else None
|
|
d['links'] = links_by_node.get(id, [])
|
|
result.append(d)
|
|
|
|
return result
|
|
|
|
class Tank(object):
|
|
def __init__(self, input: dict[str, Any]) -> None:
|
|
self.type = 'tank'
|
|
self.id = str(input['id'])
|
|
self.x = float(input['x'])
|
|
self.y = float(input['y'])
|
|
self.elevation = float(input['elevation'])
|
|
self.init_level = float(input['init_level'])
|
|
self.min_level = float(input['min_level'])
|
|
self.max_level = float(input['max_level'])
|
|
self.diameter = float(input['diameter'])
|
|
self.min_vol = float(input['min_vol'])
|
|
self.vol_curve = str(input['vol_curve']) if 'vol_curve' in input and input['vol_curve'] != None else None
|
|
self.overflow = str(input['overflow']) if 'overflow' in input and input['overflow'] != None else None
|
|
|
|
self.f_type = sql_literal(self.type)
|
|
self.f_id = sql_literal(self.id)
|
|
self.f_elevation = sql_literal(self.elevation)
|
|
self.f_init_level = sql_literal(self.init_level)
|
|
self.f_min_level = sql_literal(self.min_level)
|
|
self.f_max_level = sql_literal(self.max_level)
|
|
self.f_diameter = sql_literal(self.diameter)
|
|
self.f_min_vol = sql_literal(self.min_vol)
|
|
self.f_vol_curve = sql_literal(self.vol_curve)
|
|
self.f_overflow = sql_literal(self.overflow)
|
|
|
|
def as_dict(self) -> dict[str, Any]:
|
|
return { 'type': self.type, 'id': self.id, 'x': self.x, 'y': self.y, 'elevation': self.elevation, 'init_level': self.init_level, 'min_level': self.min_level, 'max_level': self.max_level, 'diameter': self.diameter, 'min_vol': self.min_vol, 'vol_curve': self.vol_curve, 'overflow': self.overflow }
|
|
|
|
def _set_tank(name: str, cs: ChangeSet) -> DatabaseCommand:
|
|
raw_new = get_tank(name, cs.operations[0]['id'])
|
|
|
|
new_dict = cs.operations[0]
|
|
schema = get_tank_schema(name)
|
|
for key, value in schema.items():
|
|
if key in new_dict and not value['readonly']:
|
|
raw_new[key] = new_dict[key]
|
|
new = Tank(raw_new)
|
|
|
|
statement = f"update network.tanks set elevation = {new.f_elevation}, initial_level = {new.f_init_level}, minimum_level = {new.f_min_level}, maximum_level = {new.f_max_level}, diameter = {new.f_diameter}, minimum_volume = {new.f_min_vol}, volume_curve_id = {new.f_vol_curve}, overflow = {new.f_overflow} where node_id = {new.f_id};"
|
|
statement += f"\n{sql_update_coord(new.id, new.x, new.y)}"
|
|
|
|
change = g_update_prefix | new.as_dict()
|
|
|
|
return DatabaseCommand(statement, [change])
|
|
|
|
|
|
def set_tank(name: str, cs: ChangeSet) -> ChangeSet:
|
|
if 'id' not in cs.operations[0]:
|
|
return ChangeSet()
|
|
if get_tank(name, cs.operations[0]['id']) == {}:
|
|
return ChangeSet()
|
|
return execute_command(name, _set_tank(name, cs))
|
|
|
|
|
|
def _add_tank(name: str, cs: ChangeSet) -> DatabaseCommand:
|
|
new = Tank(cs.operations[0])
|
|
|
|
statement = f"insert into network.nodes (id, node_type) values ({new.f_id}, {new.f_type});"
|
|
statement += f"\ninsert into network.tanks (node_id, elevation, initial_level, minimum_level, maximum_level, diameter, minimum_volume, volume_curve_id, overflow) values ({new.f_id}, {new.f_elevation}, {new.f_init_level}, {new.f_min_level}, {new.f_max_level}, {new.f_diameter}, {new.f_min_vol}, {new.f_vol_curve}, {new.f_overflow});"
|
|
statement += f"\n{sql_insert_coord(new.id, new.x, new.y)}"
|
|
|
|
change = g_add_prefix | new.as_dict()
|
|
|
|
return DatabaseCommand(statement, [change])
|
|
|
|
|
|
def add_tank(name: str, cs: ChangeSet) -> ChangeSet:
|
|
if 'id' not in cs.operations[0]:
|
|
return ChangeSet()
|
|
if get_tank(name, cs.operations[0]['id']) != {}:
|
|
return ChangeSet()
|
|
return execute_command(name, _add_tank(name, cs))
|
|
|
|
|
|
def _delete_tank(name: str, cs: ChangeSet) -> DatabaseCommand:
|
|
element_id = str(cs.operations[0]['id'])
|
|
f_id = sql_literal(element_id)
|
|
|
|
statement = sql_delete_coord(element_id)
|
|
statement += f"\ndelete from network.nodes where id = {f_id};"
|
|
|
|
change = g_delete_prefix | {'type': 'tank', 'id': element_id}
|
|
|
|
return DatabaseCommand(statement, [change])
|
|
|
|
|
|
def delete_tank(name: str, cs: ChangeSet) -> ChangeSet:
|
|
if 'id' not in cs.operations[0]:
|
|
return ChangeSet()
|
|
if get_tank(name, cs.operations[0]['id']) == {}:
|
|
return ChangeSet()
|
|
return execute_command(name, _delete_tank(name, cs))
|
|
|
|
|
|
#--------------------------------------------------------------
|
|
# [EPA2]
|
|
# [IN]
|
|
# id elev initlevel minlevel maxlevel diam (minvol vcurve overflow) ;desc
|
|
# xxx
|
|
# * YES
|
|
# [OUT]
|
|
# id elev initlevel minlevel maxlevel diam minvol (vcurve overflow) ;desc
|
|
#--------------------------------------------------------------
|
|
# [EPA3]
|
|
# id elev initlevel minlevel maxlevel diam minvol (vcurve)
|
|
#--------------------------------------------------------------
|
|
|
|
|
|
def inp_in_tank(line: str) -> str:
|
|
tokens = line.split()
|
|
|
|
num = len(tokens)
|
|
has_desc = tokens[-1].startswith(';')
|
|
num_without_desc = (num - 1) if has_desc else num
|
|
|
|
id = str(tokens[0])
|
|
elevation = float(tokens[1])
|
|
init_level = float(tokens[2])
|
|
min_level = float(tokens[3])
|
|
max_level = float(tokens[4])
|
|
diameter = float(tokens[5])
|
|
min_vol = float(tokens[6]) if num_without_desc >= 7 else 0.0
|
|
vol_curve = str(tokens[7]) if num_without_desc >= 8 and tokens[7] != '*' else None
|
|
overflow = str(tokens[8].upper()) if num_without_desc >= 9 else None
|
|
desc = str(tokens[-1]) if has_desc else None
|
|
|
|
return str(f"insert into network.nodes (id, node_type) values ({sql_literal(id)}, 'tank');insert into network.tanks (node_id, elevation, initial_level, minimum_level, maximum_level, diameter, minimum_volume, volume_curve_id, overflow) values ({sql_literal(id)}, {sql_literal(elevation)}, {sql_literal(init_level)}, {sql_literal(min_level)}, {sql_literal(max_level)}, {sql_literal(diameter)}, {sql_literal(min_vol)}, {sql_literal(vol_curve)}, {sql_literal(overflow)});")
|
|
|
|
|
|
def inp_out_tank(name: str) -> list[str]:
|
|
lines = []
|
|
objs = read_all(name, 'select node_id as id, elevation, initial_level as init_level, minimum_level as min_level, maximum_level as max_level, diameter, minimum_volume as min_vol, volume_curve_id as vol_curve, overflow from network.tanks order by node_id')
|
|
for obj in objs:
|
|
id = obj['id']
|
|
elevation = obj['elevation']
|
|
init_level = obj['init_level']
|
|
min_level = obj['min_level']
|
|
max_level = obj['max_level']
|
|
diameter = obj['diameter']
|
|
min_vol = obj['min_vol']
|
|
vol_curve = obj['vol_curve'] if obj['vol_curve'] != None else ''
|
|
overflow = obj['overflow'] if obj['overflow'] != None else ''
|
|
if vol_curve == '' and overflow != '':
|
|
vol_curve = '*'
|
|
desc = ';'
|
|
lines.append(f'{id} {elevation} {init_level} {min_level} {max_level} {diameter} {min_vol} {vol_curve} {overflow} {desc}')
|
|
return lines
|
|
|
|
|
|
def unset_tank_by_curve(name: str, curve: str) -> ChangeSet:
|
|
cs = ChangeSet()
|
|
|
|
rows = read_all(name, "select node_id as id from network.tanks where volume_curve_id = %s", (curve,))
|
|
for row in rows:
|
|
cs.append(g_update_prefix | {'type': 'tank', 'id': row['id'], 'vol_curve': None})
|
|
|
|
return cs
|