refactor(db)!: adopt project-routed pooled databases
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.
This commit is contained in:
@@ -0,0 +1,200 @@
|
||||
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,
|
||||
)
|
||||
|
||||
|
||||
VALVES_TYPE_PRV = 'PRV'
|
||||
VALVES_TYPE_PSV = 'PSV'
|
||||
VALVES_TYPE_PBV = 'PBV'
|
||||
VALVES_TYPE_FCV = 'FCV'
|
||||
VALVES_TYPE_TCV = 'TCV'
|
||||
VALVES_TYPE_GPV = 'GPV'
|
||||
|
||||
|
||||
def get_valve_schema(name: str) -> dict[str, dict[str, Any]]:
|
||||
return { 'id' : {'type': 'str' , 'optional': False , 'readonly': True },
|
||||
'node1' : {'type': 'str' , 'optional': False , 'readonly': False},
|
||||
'node2' : {'type': 'str' , 'optional': False , 'readonly': False},
|
||||
'diameter' : {'type': 'float' , 'optional': False , 'readonly': False},
|
||||
'v_type' : {'type': 'str' , 'optional': False , 'readonly': False},
|
||||
'setting' : {'type': 'str' , 'optional': False , 'readonly': False},
|
||||
'minor_loss' : {'type': 'float' , 'optional': False , 'readonly': False} }
|
||||
|
||||
|
||||
def get_valve(name: str, id: str) -> dict[str, Any]:
|
||||
p = try_read(name, "select l.id, l.start_node_id as node1, l.end_node_id as node2, v.diameter, v.valve_type as v_type, v.setting, v.minor_loss from network.links l join network.valves v on v.link_id = l.id where l.id = %s", (id,))
|
||||
if p == None:
|
||||
return {}
|
||||
d = {}
|
||||
d['id'] = str(p['id'])
|
||||
d['node1'] = str(p['node1'])
|
||||
d['node2'] = str(p['node2'])
|
||||
d['diameter'] = float(p['diameter'])
|
||||
d['v_type'] = str(p['v_type'])
|
||||
d['setting'] = str(p['setting'])
|
||||
d['minor_loss'] = float(p['minor_loss'])
|
||||
return d
|
||||
|
||||
def get_all_valves(name: str) -> list[dict[str, Any]]:
|
||||
rows = read_all(
|
||||
name,
|
||||
"SELECT id, start_node_id AS node1, end_node_id AS node2, diameter, "
|
||||
"valve_type AS v_type, setting, minor_loss FROM gis.valves ORDER BY id",
|
||||
)
|
||||
if rows == None:
|
||||
return []
|
||||
|
||||
result = []
|
||||
for row in rows:
|
||||
d = {}
|
||||
d['id'] = str(row['id'])
|
||||
d['node1'] = str(row['node1'])
|
||||
d['node2'] = str(row['node2'])
|
||||
d['diameter'] = float(row['diameter'])
|
||||
d['v_type'] = str(row['v_type'])
|
||||
d['setting'] = str(row['setting'])
|
||||
d['minor_loss'] = float(row['minor_loss'])
|
||||
result.append(d)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
|
||||
|
||||
class Valve(object):
|
||||
def __init__(self, input: dict[str, Any]) -> None:
|
||||
self.type = 'valve'
|
||||
self.id = str(input['id'])
|
||||
self.node1 = str(input['node1'])
|
||||
self.node2 = str(input['node2'])
|
||||
self.diameter = float(input['diameter'])
|
||||
self.v_type = str(input['v_type'])
|
||||
self.setting = str(input['setting'])
|
||||
self.minor_loss = float(input['minor_loss'])
|
||||
|
||||
self.f_type = sql_literal(self.type)
|
||||
self.f_id = sql_literal(self.id)
|
||||
self.f_node1 = sql_literal(self.node1)
|
||||
self.f_node2 = sql_literal(self.node2)
|
||||
self.f_diameter = sql_literal(self.diameter)
|
||||
self.f_v_type = sql_literal(self.v_type)
|
||||
self.f_setting = sql_literal(self.setting)
|
||||
self.f_minor_loss = sql_literal(self.minor_loss)
|
||||
|
||||
def as_dict(self) -> dict[str, Any]:
|
||||
return { 'type': self.type, 'id': self.id, 'node1': self.node1, 'node2': self.node2, 'diameter': self.diameter, 'v_type': self.v_type, 'setting': self.setting, 'minor_loss': self.minor_loss }
|
||||
|
||||
def _set_valve(name: str, cs: ChangeSet) -> DatabaseCommand:
|
||||
raw_new = get_valve(name, cs.operations[0]['id'])
|
||||
|
||||
new_dict = cs.operations[0]
|
||||
schema = get_valve_schema(name)
|
||||
for key, value in schema.items():
|
||||
if key in new_dict and not value['readonly']:
|
||||
raw_new[key] = new_dict[key]
|
||||
new = Valve(raw_new)
|
||||
|
||||
statement = f"update network.links set start_node_id = {new.f_node1}, end_node_id = {new.f_node2} where id = {new.f_id};"
|
||||
statement += f"\nupdate network.valves set diameter = {new.f_diameter}, valve_type = {new.f_v_type}, setting = {new.f_setting}, minor_loss = {new.f_minor_loss} where link_id = {new.f_id};"
|
||||
change = g_update_prefix | new.as_dict()
|
||||
|
||||
return DatabaseCommand(statement, [change])
|
||||
|
||||
|
||||
def set_valve(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
if 'id' not in cs.operations[0]:
|
||||
return ChangeSet()
|
||||
if get_valve(name, cs.operations[0]['id']) == {}:
|
||||
return ChangeSet()
|
||||
return execute_command(name, _set_valve(name, cs))
|
||||
|
||||
|
||||
def _add_valve(name: str, cs: ChangeSet) -> DatabaseCommand:
|
||||
new = Valve(cs.operations[0])
|
||||
|
||||
statement = f"insert into network.links (id, link_type, start_node_id, end_node_id) values ({new.f_id}, {new.f_type}, {new.f_node1}, {new.f_node2});"
|
||||
statement += f"\ninsert into network.valves (link_id, diameter, valve_type, setting, minor_loss) values ({new.f_id}, {new.f_diameter}, {new.f_v_type}, {new.f_setting}, {new.f_minor_loss});"
|
||||
|
||||
change = g_add_prefix | new.as_dict()
|
||||
|
||||
return DatabaseCommand(statement, [change])
|
||||
|
||||
|
||||
def add_valve(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
if 'id' not in cs.operations[0]:
|
||||
return ChangeSet()
|
||||
if get_valve(name, cs.operations[0]['id']) != {}:
|
||||
return ChangeSet()
|
||||
return execute_command(name, _add_valve(name, cs))
|
||||
|
||||
|
||||
def _delete_valve(name: str, cs: ChangeSet) -> DatabaseCommand:
|
||||
element_id = str(cs.operations[0]['id'])
|
||||
f_id = sql_literal(element_id)
|
||||
|
||||
statement = f"delete from network.links where id = {f_id};"
|
||||
|
||||
change = g_delete_prefix | {'type': 'valve', 'id': element_id}
|
||||
|
||||
return DatabaseCommand(statement, [change])
|
||||
|
||||
|
||||
def delete_valve(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
if 'id' not in cs.operations[0]:
|
||||
return ChangeSet()
|
||||
if get_valve(name, cs.operations[0]['id']) == {}:
|
||||
return ChangeSet()
|
||||
return execute_command(name, _delete_valve(name, cs))
|
||||
|
||||
|
||||
#--------------------------------------------------------------
|
||||
# [EPA2][EPA3][IN][OUT]
|
||||
# id node1 node2 diam type setting (lcoeff lcurve)
|
||||
# for GPV, setting is string = head curve id
|
||||
# [NOT SUPPORT] for PCV, add loss curve if present
|
||||
#--------------------------------------------------------------
|
||||
|
||||
|
||||
def inp_in_valve(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])
|
||||
node1 = str(tokens[1])
|
||||
node2 = str(tokens[2])
|
||||
diameter = float(tokens[3])
|
||||
v_type = str(tokens[4].upper())
|
||||
setting = str(tokens[5])
|
||||
minor_loss = float(tokens[6]) if len(tokens) >= 7 else 0.0
|
||||
desc = str(tokens[-1]) if has_desc else None
|
||||
|
||||
return str(f"insert into network.links (id, link_type, start_node_id, end_node_id) values ({sql_literal(id)}, 'valve', {sql_literal(node1)}, {sql_literal(node2)});insert into network.valves (link_id, diameter, valve_type, setting, minor_loss) values ({sql_literal(id)}, {sql_literal(diameter)}, {sql_literal(v_type)}, {sql_literal(setting)}, {sql_literal(minor_loss)});")
|
||||
|
||||
|
||||
def inp_out_valve(name: str) -> list[str]:
|
||||
lines = []
|
||||
objs = read_all(name, 'select l.id, l.start_node_id as node1, l.end_node_id as node2, v.diameter, v.valve_type as v_type, v.setting, v.minor_loss from network.links l join network.valves v on v.link_id = l.id order by l.id')
|
||||
for obj in objs:
|
||||
id = obj['id']
|
||||
node1 = obj['node1']
|
||||
node2 = obj['node2']
|
||||
diameter = obj['diameter']
|
||||
v_type = obj['v_type']
|
||||
setting = obj['setting']
|
||||
minor_loss = obj['minor_loss']
|
||||
desc = ';'
|
||||
lines.append(f'{id} {node1} {node2} {diameter} {v_type} {setting} {minor_loss} {desc}')
|
||||
return lines
|
||||
Reference in New Issue
Block a user