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,149 @@
|
||||
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,
|
||||
)
|
||||
|
||||
MIXING_MODEL_MIXED = 'MIXED'
|
||||
MIXING_MODEL_2COMP = '2COMP'
|
||||
MIXING_MODEL_FIFO = 'FIFO'
|
||||
MIXING_MODEL_LIFO = 'LIFO'
|
||||
|
||||
def get_mixing_schema(name: str) -> dict[str, dict[str, Any]]:
|
||||
return { 'tank' : {'type': 'str' , 'optional': False , 'readonly': True },
|
||||
'model' : {'type': 'str' , 'optional': False , 'readonly': False},
|
||||
'value' : {'type': 'float' , 'optional': True , 'readonly': False} }
|
||||
|
||||
|
||||
def get_mixing(name: str, tank: str) -> dict[str, Any]:
|
||||
m = try_read(name, "select tank_id as tank, model, value from network.tank_mixing where tank_id = %s", (tank,))
|
||||
if m == None:
|
||||
return {}
|
||||
d = {}
|
||||
d['tank'] = str(m['tank'])
|
||||
d['model'] = str(m['model'])
|
||||
d['value'] = float(m['value']) if m['value'] != None else None
|
||||
return d
|
||||
|
||||
|
||||
class Mixing(object):
|
||||
def __init__(self, input: dict[str, Any]) -> None:
|
||||
self.type = 'mixing'
|
||||
self.tank = str(input['tank'])
|
||||
self.model = str(input['model'])
|
||||
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_model = sql_literal(self.model)
|
||||
self.f_value = sql_literal(self.value)
|
||||
|
||||
def as_dict(self) -> dict[str, Any]:
|
||||
return { 'type': self.type, 'tank': self.tank, 'model': self.model, 'value': self.value }
|
||||
|
||||
def _set_mixing(name: str, cs: ChangeSet) -> DatabaseCommand:
|
||||
raw_new = get_mixing(name, cs.operations[0]['tank'])
|
||||
|
||||
new_dict = cs.operations[0]
|
||||
schema = get_mixing_schema(name)
|
||||
for key, value in schema.items():
|
||||
if key in new_dict and not value['readonly']:
|
||||
raw_new[key] = new_dict[key]
|
||||
new = Mixing(raw_new)
|
||||
|
||||
statement = f"update network.tank_mixing set model = {new.f_model}, value = {new.f_value} where tank_id = {new.f_tank};"
|
||||
|
||||
change = g_update_prefix | new.as_dict()
|
||||
|
||||
return DatabaseCommand(statement, [change])
|
||||
|
||||
|
||||
def set_mixing(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
if 'tank' not in cs.operations[0]:
|
||||
return ChangeSet()
|
||||
if get_mixing(name, cs.operations[0]['tank']) == {}:
|
||||
return ChangeSet()
|
||||
return execute_command(name, _set_mixing(name, cs))
|
||||
|
||||
|
||||
def _add_mixing(name: str, cs: ChangeSet) -> DatabaseCommand:
|
||||
new = Mixing(cs.operations[0])
|
||||
|
||||
statement = f"insert into network.tank_mixing (tank_id, model, value) values ({new.f_tank}, {new.f_model}, {new.f_value});"
|
||||
|
||||
change = g_add_prefix | new.as_dict()
|
||||
|
||||
return DatabaseCommand(statement, [change])
|
||||
|
||||
|
||||
def add_mixing(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
if 'tank' not in cs.operations[0]:
|
||||
return ChangeSet()
|
||||
if get_mixing(name, cs.operations[0]['tank']) != {}:
|
||||
return ChangeSet()
|
||||
return execute_command(name, _add_mixing(name, cs))
|
||||
|
||||
|
||||
def _delete_mixing(name: str, cs: ChangeSet) -> DatabaseCommand:
|
||||
tank = str(cs.operations[0]['tank'])
|
||||
f_tank = sql_literal(tank)
|
||||
|
||||
statement = f"delete from network.tank_mixing where tank_id = {f_tank};"
|
||||
|
||||
change = g_delete_prefix | {'type': 'mixing', 'tank': tank}
|
||||
|
||||
return DatabaseCommand(statement, [change])
|
||||
|
||||
|
||||
def delete_mixing(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
if 'tank' not in cs.operations[0]:
|
||||
return ChangeSet()
|
||||
if get_mixing(name, cs.operations[0]['tank']) == {}:
|
||||
return ChangeSet()
|
||||
return execute_command(name, _delete_mixing(name, cs))
|
||||
|
||||
|
||||
#--------------------------------------------------------------
|
||||
# [EPA2][EPA3][IN][OUT]
|
||||
# TankID MixModel FractVolume
|
||||
# FractVolume if type == MIX2
|
||||
#--------------------------------------------------------------
|
||||
|
||||
|
||||
def inp_in_mixing(line: str) -> str:
|
||||
tokens = line.split()
|
||||
|
||||
num = len(tokens)
|
||||
has_desc = tokens[-1].startswith(';')
|
||||
num_without_desc = (num - 1) if has_desc else num
|
||||
|
||||
tank = str(tokens[0])
|
||||
model = str(tokens[1].upper())
|
||||
value = float(tokens[3]) if num_without_desc >= 4 else None
|
||||
return str(f"insert into network.tank_mixing (tank_id, model, value) values ({sql_literal(tank)}, {sql_literal(model)}, {sql_literal(value)});")
|
||||
|
||||
|
||||
def inp_out_mixing(name: str) -> list[str]:
|
||||
lines = []
|
||||
objs = read_all(name, 'select tank_id as tank, model, value from network.tank_mixing order by tank_id')
|
||||
for obj in objs:
|
||||
tank = obj['tank']
|
||||
model = obj['model']
|
||||
value = obj['value'] if obj['value'] != None else ''
|
||||
lines.append(f'{tank} {model} {value}')
|
||||
return lines
|
||||
|
||||
|
||||
def delete_mixing_by_tank(name: str, tank: str) -> ChangeSet:
|
||||
row = try_read(name, "select 1 from network.tank_mixing where tank_id = %s", (tank,))
|
||||
if row == None:
|
||||
return ChangeSet()
|
||||
return ChangeSet(g_delete_prefix | {'type' : 'mixing', 'tank': tank})
|
||||
Reference in New Issue
Block a user