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.
100 lines
3.2 KiB
Python
100 lines
3.2 KiB
Python
from typing import Any
|
|
|
|
from ..core.database import (
|
|
ChangeSet,
|
|
DatabaseCommand,
|
|
execute_command,
|
|
g_update_prefix,
|
|
read_all,
|
|
sql_literal,
|
|
try_read,
|
|
)
|
|
|
|
|
|
def get_quality_schema(name: str) -> dict[str, dict[str, Any]]:
|
|
return { 'node' : {'type': 'str' , 'optional': False , 'readonly': True },
|
|
'quality' : {'type': 'float' , 'optional': True , 'readonly': False} }
|
|
|
|
|
|
def get_quality(name: str, node: str) -> dict[str, Any]:
|
|
e = try_read(name, "select node_id as node, value as quality from network.initial_quality where node_id = %s", (node,))
|
|
if e == None:
|
|
return { 'node': node, 'quality': None }
|
|
d = {}
|
|
d['node'] = str(e['node'])
|
|
d['quality'] = float(e['quality']) if e['quality'] != None else None
|
|
return d
|
|
|
|
|
|
class Quality(object):
|
|
def __init__(self, input: dict[str, Any]) -> None:
|
|
self.type = 'quality'
|
|
self.node = str(input['node'])
|
|
self.quality = float(input['quality']) if 'quality' in input and input['quality'] != None else None
|
|
|
|
self.f_type = sql_literal(self.type)
|
|
self.f_node = sql_literal(self.node)
|
|
self.f_quality = sql_literal(self.quality)
|
|
|
|
def as_dict(self) -> dict[str, Any]:
|
|
return { 'type': self.type, 'node': self.node, 'quality': self.quality }
|
|
|
|
|
|
def _set_quality(name: str, cs: ChangeSet) -> DatabaseCommand:
|
|
raw_new = get_quality(name, cs.operations[0]['node'])
|
|
|
|
new_dict = cs.operations[0]
|
|
schema = get_quality_schema(name)
|
|
for key, value in schema.items():
|
|
if key in new_dict and not value['readonly']:
|
|
raw_new[key] = new_dict[key]
|
|
new = Quality(raw_new)
|
|
|
|
statement = f"delete from network.initial_quality where node_id = {new.f_node};"
|
|
if new.quality != None:
|
|
statement += f"\ninsert into network.initial_quality (node_id, value) values ({new.f_node}, {new.f_quality});"
|
|
|
|
change = g_update_prefix | new.as_dict()
|
|
|
|
return DatabaseCommand(statement, [change])
|
|
|
|
|
|
def set_quality(name: str, cs: ChangeSet) -> ChangeSet:
|
|
return execute_command(name, _set_quality(name, cs))
|
|
|
|
|
|
#--------------------------------------------------------------
|
|
# [EPA2][EPA3][IN][OUT]
|
|
# node initqual
|
|
#--------------------------------------------------------------
|
|
|
|
|
|
def inp_in_quality(line: str) -> str:
|
|
tokens = line.split()
|
|
|
|
num = len(tokens)
|
|
has_desc = tokens[-1].startswith(';')
|
|
num_without_desc = (num - 1) if has_desc else num
|
|
|
|
node = str(tokens[0])
|
|
quality = float(tokens[1])
|
|
|
|
return str(f"insert into network.initial_quality (node_id, value) values ({sql_literal(node)}, {sql_literal(quality)});")
|
|
|
|
|
|
def inp_out_quality(name: str) -> list[str]:
|
|
lines = []
|
|
objs = read_all(name, 'select node_id as node, value as quality from network.initial_quality order by node_id')
|
|
for obj in objs:
|
|
node = obj['node']
|
|
quality = obj['quality']
|
|
lines.append(f'{node} {quality}')
|
|
return lines
|
|
|
|
|
|
def delete_quality_by_node(name: str, node: str) -> ChangeSet:
|
|
row = try_read(name, "select 1 from network.initial_quality where node_id = %s", (node,))
|
|
if row == None:
|
|
return ChangeSet()
|
|
return ChangeSet(g_update_prefix | {'type' : 'quality', 'node': node, 'quality': None})
|