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.
175 lines
6.0 KiB
Python
175 lines
6.0 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,
|
|
)
|
|
|
|
CURVE_TYPE_PUMP = 'PUMP'
|
|
CURVE_TYPE_EFFICIENCY = 'EFFICIENCY'
|
|
CURVE_TYPE_VOLUME = 'VOLUME'
|
|
CURVE_TYPE_HEADLOSS = 'HEADLOSS'
|
|
|
|
curve_types = [CURVE_TYPE_PUMP, CURVE_TYPE_EFFICIENCY, CURVE_TYPE_VOLUME, CURVE_TYPE_HEADLOSS]
|
|
|
|
def get_curve_schema(name: str) -> dict[str, dict[str, Any]]:
|
|
return { 'id' : {'type': 'str' , 'optional': False , 'readonly': True },
|
|
'c_type' : {'type': 'str' , 'optional': False , 'readonly': False},
|
|
'coords' : {'type': 'list' , 'optional': False , 'readonly': False,
|
|
'element': { 'x' : {'type': 'float' , 'optional': False , 'readonly': False },
|
|
'y' : {'type': 'float' , 'optional': False , 'readonly': False } }}}
|
|
|
|
|
|
def get_curve(name: str, id: str) -> dict[str, Any]:
|
|
c_one = try_read(name, "select id, curve_type from network.curves where id = %s", (id,))
|
|
if c_one == None:
|
|
return {}
|
|
cus = read_all(name, "select x, y from network.curve_points where curve_id = %s order by sequence_no", (id,))
|
|
cs = []
|
|
for r in cus:
|
|
cs.append({ 'x': float(r['x']), 'y': float(r['y']) })
|
|
d = {}
|
|
d['id'] = id
|
|
d['c_type'] = c_one['curve_type']
|
|
d['coords'] = cs
|
|
return d
|
|
|
|
|
|
def _set_curve(name: str, cs: ChangeSet) -> DatabaseCommand:
|
|
id = cs.operations[0]['id']
|
|
f_id = sql_literal(id)
|
|
|
|
old = get_curve(name, id)
|
|
|
|
new = { 'id': id }
|
|
if 'coords' in cs.operations[0]:
|
|
new['coords'] = cs.operations[0]['coords']
|
|
else:
|
|
new['coords'] = old['coords']
|
|
if 'c_type' in cs.operations[0]:
|
|
new['c_type'] = cs.operations[0]['c_type']
|
|
else:
|
|
new['c_type'] = old['c_type']
|
|
new_f_type = sql_literal(new['c_type'])
|
|
|
|
statement = f"delete from network.curve_points where curve_id = {f_id};"
|
|
statement += f"\nupdate network.curves set curve_type = {new_f_type} where id = {f_id};"
|
|
for sequence_no, xy in enumerate(new['coords']):
|
|
f_x, f_y = sql_literal(xy['x']), sql_literal(xy['y'])
|
|
statement += f"\ninsert into network.curve_points (curve_id, sequence_no, x, y) values ({f_id}, {sequence_no}, {f_x}, {f_y});"
|
|
|
|
change = g_update_prefix | { 'type': 'curve' } | new
|
|
|
|
return DatabaseCommand(statement, [change])
|
|
|
|
|
|
def set_curve(name: str, cs: ChangeSet) -> ChangeSet:
|
|
if 'id' not in cs.operations[0]:
|
|
return ChangeSet()
|
|
if get_curve(name, cs.operations[0]['id']) == {}:
|
|
return ChangeSet()
|
|
return execute_command(name, _set_curve(name, cs))
|
|
|
|
|
|
def _add_curve(name: str, cs: ChangeSet) -> DatabaseCommand:
|
|
id = cs.operations[0]['id']
|
|
f_id = sql_literal(id)
|
|
|
|
new = { 'id': id, 'c_type': cs.operations[0]['c_type'], 'coords': [] }
|
|
new_f_type = sql_literal(new['c_type'])
|
|
|
|
statement = f"insert into network.curves (id, curve_type) values ({f_id}, {new_f_type});"
|
|
for sequence_no, xy in enumerate(cs.operations[0]['coords']):
|
|
x, y = float(xy['x']), float(xy['y'])
|
|
f_x, f_y = sql_literal(x), sql_literal(y)
|
|
statement += f"\ninsert into network.curve_points (curve_id, sequence_no, x, y) values ({f_id}, {sequence_no}, {f_x}, {f_y});"
|
|
new['coords'].append({ 'x': x, 'y': y })
|
|
|
|
change = g_add_prefix | { 'type': 'curve' } | new
|
|
|
|
return DatabaseCommand(statement, [change])
|
|
|
|
|
|
def add_curve(name: str, cs: ChangeSet) -> ChangeSet:
|
|
if 'id' not in cs.operations[0]:
|
|
return ChangeSet()
|
|
if get_curve(name, cs.operations[0]['id']) != {}:
|
|
return ChangeSet()
|
|
return execute_command(name, _add_curve(name, cs))
|
|
|
|
|
|
def _delete_curve(name: str, cs: ChangeSet) -> DatabaseCommand:
|
|
id = cs.operations[0]['id']
|
|
f_id = sql_literal(id)
|
|
|
|
statement = f"delete from network.curves where id = {f_id};"
|
|
|
|
change = g_delete_prefix | { 'type': 'curve' } | { 'id' : id }
|
|
|
|
return DatabaseCommand(statement, [change])
|
|
|
|
|
|
def delete_curve(name: str, cs: ChangeSet) -> ChangeSet:
|
|
if 'id' not in cs.operations[0]:
|
|
return ChangeSet()
|
|
if get_curve(name, cs.operations[0]['id']) == {}:
|
|
return ChangeSet()
|
|
return execute_command(name, _delete_curve(name, cs))
|
|
|
|
|
|
#--------------------------------------------------------------
|
|
# [EPA2][IN][OUT]
|
|
# ;type: desc
|
|
# id x y
|
|
#--------------------------------------------------------------
|
|
#--------------------------------------------------------------
|
|
# [EPA3][IN][OUT]
|
|
# id type
|
|
# id x y
|
|
#--------------------------------------------------------------
|
|
|
|
|
|
def inp_in_curve(line: str) -> str:
|
|
tokens = line.split()
|
|
curve_id = sql_literal(tokens[0])
|
|
return str(f"insert into network.curve_points (curve_id, sequence_no, x, y) values ({curve_id}, (select coalesce(max(sequence_no) + 1, 0) from network.curve_points where curve_id = {curve_id}), {sql_literal(float(tokens[1]))}, {sql_literal(float(tokens[2]))});")
|
|
|
|
|
|
def inp_out_curve(name: str) -> list[str]:
|
|
lines = []
|
|
types = read_all(name, "select id, curve_type as type from network.curves order by id")
|
|
for type in types:
|
|
id = type['id']
|
|
# ;type: desc
|
|
lines.append(f";{type['type']}:")
|
|
objs = read_all(name, "select curve_id as id, x, y from network.curve_points where curve_id = %s order by sequence_no", (id,))
|
|
for obj in objs:
|
|
id = obj['id']
|
|
x = obj['x']
|
|
y = obj['y']
|
|
lines.append(f'{id} {x} {y}')
|
|
return lines
|
|
|
|
|
|
def inp_out_curve_v3(name: str) -> list[str]:
|
|
lines = []
|
|
types = read_all(name, "select id, curve_type as type from network.curves order by id")
|
|
for type in types:
|
|
id = type['id']
|
|
# id type
|
|
lines.append(f"{id} {type['type']}")
|
|
objs = read_all(name, "select curve_id as id, x, y from network.curve_points where curve_id = %s order by sequence_no", (id,))
|
|
for obj in objs:
|
|
id = obj['id']
|
|
x = obj['x']
|
|
y = obj['y']
|
|
lines.append(f'{id} {x} {y}')
|
|
return lines
|