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.
244 lines
9.0 KiB
Python
244 lines
9.0 KiB
Python
from typing import Any
|
|
|
|
from ..core.database import (
|
|
ChangeSet,
|
|
DatabaseCommand,
|
|
execute_command,
|
|
g_update_prefix,
|
|
read_all,
|
|
sql_literal,
|
|
try_read,
|
|
)
|
|
|
|
|
|
element_schema = {'type': 'str' , 'optional': True , 'readonly': False}
|
|
|
|
|
|
def get_reaction_schema(name: str) -> dict[str, dict[str, Any]]:
|
|
return { 'ORDER BULK' : element_schema,
|
|
'ORDER WALL' : element_schema,
|
|
'ORDER TANK' : element_schema,
|
|
'GLOBAL BULK' : element_schema,
|
|
'GLOBAL WALL' : element_schema,
|
|
'LIMITING POTENTIAL' : element_schema,
|
|
'ROUGHNESS CORRELATION' : element_schema }
|
|
|
|
|
|
def get_reaction(name: str) -> dict[str, Any]:
|
|
ts = read_all(name, "select key, value from network.reaction_settings")
|
|
d = {}
|
|
for e in ts:
|
|
d[e['key']] = str(e['value'])
|
|
return d
|
|
|
|
|
|
def _set_reaction(name: str, cs: ChangeSet) -> DatabaseCommand:
|
|
new = {}
|
|
|
|
new_dict = cs.operations[0]
|
|
schema = get_reaction_schema(name)
|
|
for key in schema.keys():
|
|
if key in new_dict:
|
|
new[key] = str(new_dict[key])
|
|
|
|
change = g_update_prefix | { 'type' : 'reaction' }
|
|
|
|
statement = ''
|
|
for key, value in new.items():
|
|
if statement != '':
|
|
statement += '\n'
|
|
statement += f"update network.reaction_settings set value = {sql_literal(value)} where key = {sql_literal(key)};"
|
|
change |= { key: value }
|
|
|
|
return DatabaseCommand(statement, [change])
|
|
|
|
|
|
def set_reaction(name: str, cs: ChangeSet) -> ChangeSet:
|
|
return execute_command(name, _set_reaction(name, cs))
|
|
|
|
|
|
def get_pipe_reaction_schema(name: str) -> dict[str, dict[str, Any]]:
|
|
return { 'pipe' : {'type': 'str' , 'optional': False , 'readonly': True },
|
|
'bulk' : {'type': 'float' , 'optional': True , 'readonly': False},
|
|
'wall' : {'type': 'float' , 'optional': True , 'readonly': False} }
|
|
|
|
|
|
def get_pipe_reaction(name: str, pipe: str) -> dict[str, Any]:
|
|
d = {}
|
|
d['pipe'] = pipe
|
|
pr = try_read(name, "select bulk_coefficient as bulk, wall_coefficient as wall from network.pipe_reaction_coefficients where pipe_id = %s", (pipe,))
|
|
d['bulk'] = float(pr['bulk']) if pr is not None and pr['bulk'] is not None else None
|
|
d['wall'] = float(pr['wall']) if pr is not None and pr['wall'] is not None else None
|
|
return d
|
|
|
|
|
|
class PipeReaction(object):
|
|
def __init__(self, input: dict[str, Any]) -> None:
|
|
self.type = 'pipe_reaction'
|
|
self.pipe = str(input['pipe'])
|
|
self.bulk = float(input['bulk']) if 'bulk' in input and input['bulk'] != None else None
|
|
self.wall = float(input['wall']) if 'wall' in input and input['wall'] != None else None
|
|
|
|
self.f_type = sql_literal(self.type)
|
|
self.f_pipe = sql_literal(self.pipe)
|
|
self.f_bulk = sql_literal(self.bulk)
|
|
self.f_wall = sql_literal(self.wall)
|
|
|
|
def as_dict(self) -> dict[str, Any]:
|
|
return { 'type': self.type, 'pipe': self.pipe, 'bulk': self.bulk, 'wall': self.wall }
|
|
|
|
|
|
def _set_pipe_reaction(name: str, cs: ChangeSet) -> DatabaseCommand:
|
|
raw_new = get_pipe_reaction(name, cs.operations[0]['pipe'])
|
|
|
|
new_dict = cs.operations[0]
|
|
schema = get_pipe_reaction_schema(name)
|
|
for key, value in schema.items():
|
|
if key in new_dict and not value['readonly']:
|
|
raw_new[key] = new_dict[key]
|
|
new = PipeReaction(raw_new)
|
|
|
|
statement = f"delete from network.pipe_reaction_coefficients where pipe_id = {new.f_pipe};"
|
|
if new.bulk is not None or new.wall is not None:
|
|
statement += f"\ninsert into network.pipe_reaction_coefficients (pipe_id, bulk_coefficient, wall_coefficient) values ({new.f_pipe}, {new.f_bulk}, {new.f_wall});"
|
|
|
|
change = g_update_prefix | new.as_dict()
|
|
|
|
return DatabaseCommand(statement, [change])
|
|
|
|
|
|
def set_pipe_reaction(name: str, cs: ChangeSet) -> ChangeSet:
|
|
return execute_command(name, _set_pipe_reaction(name, cs))
|
|
|
|
|
|
def get_tank_reaction_schema(name: str) -> dict[str, dict[str, Any]]:
|
|
return { 'tank' : {'type': 'str' , 'optional': False , 'readonly': True },
|
|
'value' : {'type': 'float' , 'optional': True , 'readonly': False} }
|
|
|
|
|
|
def get_tank_reaction(name: str, tank: str) -> dict[str, Any]:
|
|
d = {}
|
|
d['tank'] = tank
|
|
pr = try_read(name, "select coefficient as value from network.tank_reaction_coefficients where tank_id = %s", (tank,))
|
|
d['value'] = float(pr['value']) if pr is not None else None
|
|
return d
|
|
|
|
|
|
class TankReaction(object):
|
|
def __init__(self, input: dict[str, Any]) -> None:
|
|
self.type = 'tank_reaction'
|
|
self.tank = str(input['tank'])
|
|
self.value = float(input['value']) if 'value' in input and input['value'] != None else None
|
|
|
|
self.f_type = sql_literal(self.type)
|
|
self.f_tank = sql_literal(self.tank)
|
|
self.f_value = sql_literal(self.value)
|
|
|
|
def as_dict(self) -> dict[str, Any]:
|
|
return { 'type': self.type, 'tank': self.tank, 'value': self.value }
|
|
|
|
|
|
def _set_tank_reaction(name: str, cs: ChangeSet) -> DatabaseCommand:
|
|
raw_new = get_tank_reaction(name, cs.operations[0]['tank'])
|
|
|
|
new_dict = cs.operations[0]
|
|
schema = get_tank_reaction_schema(name)
|
|
for key, value in schema.items():
|
|
if key in new_dict and not value['readonly']:
|
|
raw_new[key] = new_dict[key]
|
|
new = TankReaction(raw_new)
|
|
|
|
statement = f"delete from network.tank_reaction_coefficients where tank_id = {new.f_tank};"
|
|
if new.value != None:
|
|
statement += f"\ninsert into network.tank_reaction_coefficients (tank_id, coefficient) values ({new.f_tank}, {new.f_value});"
|
|
|
|
change = g_update_prefix | new.as_dict()
|
|
|
|
return DatabaseCommand(statement, [change])
|
|
|
|
|
|
def set_tank_reaction(name: str, cs: ChangeSet) -> ChangeSet:
|
|
return execute_command(name, _set_tank_reaction(name, cs))
|
|
|
|
|
|
#--------------------------------------------------------------
|
|
# [EPA2][EPA3][IN][OUT]
|
|
# ORDER {BULK/WALL/TANK} value
|
|
# GLOBAL BULK coeff
|
|
# GLOBAL WALL coeff
|
|
# BULK link1 (link2) coeff
|
|
# WALL link1 (link2) coeff
|
|
# TANK node1 (node2) coeff
|
|
# LIMITING POTENTIAL value
|
|
# ROUGHNESS CORRELATION value
|
|
#--------------------------------------------------------------
|
|
|
|
|
|
def inp_in_reaction(line: str) -> str:
|
|
tokens = line.split()
|
|
token0 = tokens[0].upper()
|
|
if token0 == 'BULK' or token0 == 'WALL':
|
|
pipe = tokens[1]
|
|
key = token0.lower()
|
|
value = float(tokens[2])
|
|
column = 'bulk_coefficient' if key == 'bulk' else 'wall_coefficient'
|
|
return str(f"insert into network.pipe_reaction_coefficients (pipe_id, {column}) values ({sql_literal(pipe)}, {sql_literal(value)}) on conflict (pipe_id) do update set {column} = excluded.{column};")
|
|
|
|
elif token0 == 'TANK':
|
|
tank = tokens[1]
|
|
value = float(tokens[2])
|
|
return str(f"insert into network.tank_reaction_coefficients (tank_id, coefficient) values ({sql_literal(tank)}, {sql_literal(value)});")
|
|
|
|
else:
|
|
line = line.upper().strip()
|
|
for key in get_reaction_schema('').keys():
|
|
if line.startswith(key):
|
|
value = line.removeprefix(key).strip()
|
|
return str(f"update network.reaction_settings set value = {sql_literal(value)} where key = {sql_literal(key)};")
|
|
|
|
return str('')
|
|
|
|
|
|
def inp_out_reaction(name: str) -> list[str]:
|
|
lines = []
|
|
|
|
objs = read_all(name, "select key, value from network.reaction_settings order by key")
|
|
for obj in objs:
|
|
key = obj['key']
|
|
value = obj['value']
|
|
lines.append(f'{key} {value}')
|
|
|
|
objs = read_all(name, "select pipe_id as pipe, bulk_coefficient as value from network.pipe_reaction_coefficients where bulk_coefficient is not null order by pipe_id")
|
|
for obj in objs:
|
|
pipe = obj['pipe']
|
|
value = obj['value']
|
|
lines.append(f'BULK {pipe} {value}')
|
|
|
|
objs = read_all(name, "select pipe_id as pipe, wall_coefficient as value from network.pipe_reaction_coefficients where wall_coefficient is not null order by pipe_id")
|
|
for obj in objs:
|
|
pipe = obj['pipe']
|
|
value = obj['value']
|
|
lines.append(f'WALL {pipe} {value}')
|
|
|
|
objs = read_all(name, "select tank_id as tank, coefficient as value from network.tank_reaction_coefficients order by tank_id")
|
|
for obj in objs:
|
|
tank = obj['tank']
|
|
value = obj['value']
|
|
lines.append(f'TANK {tank} {value}')
|
|
|
|
return lines
|
|
|
|
|
|
def delete_pipe_reaction_by_pipe(name: str, pipe: str) -> ChangeSet:
|
|
row = try_read(name, "select 1 from network.pipe_reaction_coefficients where pipe_id = %s", (pipe,))
|
|
if row is None:
|
|
return ChangeSet()
|
|
return ChangeSet(g_update_prefix | {'type': 'pipe_reaction', 'pipe': pipe, 'bulk': None, 'wall': None})
|
|
|
|
|
|
def delete_tank_reaction_by_tank(name: str, tank: str) -> ChangeSet:
|
|
row = try_read(name, "select 1 from network.tank_reaction_coefficients where tank_id = %s", (tank,))
|
|
if row == None:
|
|
return ChangeSet()
|
|
return ChangeSet(g_update_prefix | {'type': 'tank_reaction', 'tank': tank, 'value': None})
|