Huge refactor to api and add batch api

This commit is contained in:
WQY\qiong
2022-10-14 23:18:01 +08:00
parent 200aaaca99
commit c5480d55ca
20 changed files with 1811 additions and 1510 deletions

View File

@@ -1,127 +1,132 @@
from typing import Any
from psycopg.rows import Row
from .operation import *
from .s0_base import *
from .change_set import ChangeSet
from .s24_coordinates import *
from .utility import *
from .schema import *
OVERFLOW_YES = 'yes'
OVERFLOW_NO = 'no'
schema: dict[str, dict[str, Any]] = { \
'id' : define_property(str_type, False, True), \
'elevation' : define_property(float_type), \
'init_level': define_property(float_type), \
'min_level' : define_property(float_type), \
'max_level' : define_property(float_type), \
'diameter' : define_property(float_type), \
'min_vol' : define_property(float_type), \
'vol_curve' : define_property(str_type, True), \
'overflow' : define_property(str_type, True), \
'coord' : define_property(client_point_type), \
'links' : define_property(str_list_type, False, True)}
def get_tank_schema(name: str) -> dict[str, dict[str, Any]]:
return schema
return { 'id' : {'type': 'str' , 'optional': False , 'readonly': True },
'x' : {'type': 'float' , 'optional': False , 'readonly': False},
'y' : {'type': 'float' , 'optional': False , 'readonly': False},
'elevation' : {'type': "float" , 'optional': False , 'readonly': False},
'init_level' : {'type': "float" , 'optional': False , 'readonly': False},
'min_level' : {'type': "float" , 'optional': False , 'readonly': False},
'max_level' : {'type': "float" , 'optional': False , 'readonly': False},
'diameter' : {'type': "float" , 'optional': False , 'readonly': False},
'min_vol' : {'type': "float" , 'optional': False , 'readonly': False},
'vol_curve' : {'type': "str" , 'optional': True , 'readonly': False},
'overflow' : {'type': "str" , 'optional': True , 'readonly': False},
'links' : {'type': "str_list" , 'optional': False , 'readonly': True } }
def _query_tank(name: str, id: str) -> Row | None:
return read(name, f"select * from tanks where id = '{id}'")
def get_tank(name: str, id: str) -> dict[str, Any]:
t = read(name, f"select * from tanks where id = '{id}'")
xy = get_node_coord(name, id)
d = {}
d['id'] = str(t['id'])
d['x'] = float(xy['x'])
d['y'] = float(xy['y'])
d['elevation'] = float(t['elevation'])
d['init_level'] = float(t['init_level'])
d['min_level'] = float(t['min_level'])
d['max_level'] = float(t['max_level'])
d['diameter'] = float(t['diameter'])
d['min_vol'] = float(t['min_vol'])
d['vol_curve'] = str(t['vol_curve']) if t['vol_curve'] != None else None
d['overflow'] = str(t['overflow']) if t['overflow'] != None else None
d['links'] = get_node_links(name, id)
return d
def add_tank(name: str, id: str, x: float, y: float, elevation: float, init_level: float = 0, min_level: float = 0, max_level: float = 0, diameter: float = 0, min_vol: float = 0) -> ChangeSet:
if is_tank(name, id):
return ChangeSet()
class Tank(object):
def __init__(self, input: dict[str, Any]) -> None:
self.type = 'tank'
self.id = str(input['id'])
self.x = float(input['x'])
self.y = float(input['y'])
self.elevation = float(input['elevation'])
self.init_level = float(input['init_level'])
self.min_level = float(input['min_level'])
self.max_level = float(input['max_level'])
self.diameter = float(input['diameter'])
self.min_vol = float(input['min_vol'])
self.vol_curve = str(input['vol_curve']) if 'vol_curve' in input and input['vol_curve'] != None else None
self.overflow = str(input['overflow']) if 'overflow' in input and input['overflow'] != None else None
sql = f"insert into _node (id, type) values ('{id}', '{TANK}');"
sql += f"\ninsert into tanks (id, elevation, init_level, min_level, max_level, diameter, min_vol) values ('{id}', {elevation}, {init_level}, {min_level}, {max_level}, {diameter}, {min_vol});"
sql += f"\ninsert into coordinates (node, coord) values ('{id}', '({x}, {y})');"
self.f_type = f"'{self.type}'"
self.f_id = f"'{self.id}'"
self.f_coord = f"'({self.x}, {self.y})'"
self.f_elevation = self.elevation
self.f_init_level = self.init_level
self.f_min_level = self.min_level
self.f_max_level = self.max_level
self.f_diameter = self.diameter
self.f_min_vol = self.min_vol
self.f_vol_curve = f"'{self.vol_curve}'" if self.vol_curve != None else 'null'
self.f_overflow = f"'{self.overflow}'" if self.overflow != None else 'null'
undo = f"delete from coordinates where node = ''{id}'';"
undo += f"\ndelete from tanks where id = ''{id}'';"
undo += f"\ndelete from _node where id = ''{id}'';"
def as_dict(self) -> dict[str, Any]:
return { 'type': self.type, 'id': self.id, 'x': self.x, 'y': self.y, 'elevation': self.elevation, 'init_level': self.init_level, 'min_level': self.min_level, 'max_level': self.max_level, 'diameter': self.diameter, 'min_vol': self.min_vol, 'vol_curve': self.vol_curve, 'overflow': self.overflow }
write(name, sql)
add_operation(name, sql.replace("'", "''"), undo, 'add_tank', API_ADD, TANK, id)
return get_current_change_set(name)
def as_id_dict(self) -> dict[str, Any]:
return { 'type': self.type, 'id': self.id }
def get_tank(name: str, id: str) -> dict[str, Any] | None:
row = _query_tank(name, id)
if row == None:
return None
def set_tank(name: str, cs: ChangeSet) -> ChangeSet:
old = Tank(get_tank(name, cs.operations[0]['id']))
raw_new = get_tank(name, cs.operations[0]['id'])
ps: dict[str, str] = {}
ps['id'] = id
ps['elevation'] = float(row['elevation'])
ps['init_level'] = float(row['init_level'])
ps['min_level'] = float(row['min_level'])
ps['max_level'] = float(row['max_level'])
ps['diameter'] = float(row['diameter'])
ps['min_vol'] = float(row['min_vol'])
ps['vol_curve'] = row['vol_curve']
ps['overflow'] = row['overflow']
ps['coord'] = get_node_coord(name, id)
ps['links'] = get_node_links(name, id)
return ps
new_dict = cs.operations[0]
schema = get_tank_schema(name)
for key, value in schema.items():
if key in new_dict and not value['readonly']:
raw_new[key] = new_dict[key]
new = Tank(raw_new)
redo_sql = f"update tanks set elevation = {new.f_elevation}, init_level = {new.f_init_level}, min_level = {new.f_min_level}, max_level = {new.f_max_level}, diameter = {new.f_diameter}, min_vol = {new.f_min_vol}, vol_curve = {new.f_vol_curve}, overflow = {new.f_overflow} where id = {new.f_id};"
redo_sql += f"\nupdate coordinates set coord = {new.f_coord} where node = {new.f_id};"
undo_sql = f"update coordinates set coord = {old.f_coord} where node = {old.f_id};"
undo_sql += f"\nupdate tanks set elevation = {old.f_elevation}, init_level = {old.f_init_level}, min_level = {old.f_min_level}, max_level = {old.f_max_level}, diameter = {old.f_diameter}, min_vol = {old.f_min_vol}, vol_curve = {old.f_vol_curve}, overflow = {old.f_overflow} where id = {old.f_id};"
redo_cs = g_update_prefix | new.as_dict()
undo_cs = g_update_prefix | old.as_dict()
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
def set_tank(name: str, id: str, properties: dict[str, Any]) -> ChangeSet:
if not is_tank(name, id):
return ChangeSet()
if 'vol_curve' in properties:
if not is_curve(properties['vol_curve']):
return ChangeSet()
if 'overflow' in properties:
if properties['overflow'] != OVERFLOW_YES and properties['overflow'] != OVERFLOW_NO:
return ChangeSet()
def add_tank(name: str, cs: ChangeSet) -> ChangeSet:
new = Tank(cs.operations[0])
old = Serialize(get_tank(name, id), schema).to_storage()
redo_sql = f"insert into _node (id, type) values ({new.f_id}, {new.f_type});"
redo_sql += f"\ninsert into tanks (id, elevation, init_level, min_level, max_level, diameter, min_vol, vol_curve, overflow) values ({new.f_id}, {new.f_elevation}, {new.f_init_level}, {new.f_min_level}, {new.f_max_level}, {new.f_diameter}, {new.f_min_vol}, {new.f_vol_curve}, {new.f_overflow});"
redo_sql += f"\ninsert into coordinates (node, coord) values ({new.f_id}, {new.f_coord});"
new = get_tank(name, id)
ps: list[str] = []
for key in properties:
if key in schema and schema[key]['readonly'] == False:
new[key] = properties[key]
ps.append(key)
new = Serialize(new, schema).to_execution()
undo_sql = f"delete from coordinates where node = {new.f_id};"
undo_sql += f"\ndelete from tanks where id = {new.f_id};"
undo_sql += f"\ndelete from _node where id = {new.f_id};"
sql = f"update tanks set elevation = {new['elevation']}, \
init_level = {new['init_level']}, min_level = {new['min_level']}, max_level = {new['max_level']}, \
diameter = {new['diameter']}, min_vol = {new['min_vol']}, vol_curve = {new['vol_curve']}, overflow = {new['overflow']} where id = '{id}';"
undo = ""
if 'coord' in ps:
sql += f"\nupdate coordinates set coord = {new['coord']} where node = '{id}';"
undo = f"update coordinates set coord = {old['coord']} where node = ''{id}'';"
undo += f"\nupdate tanks set elevation = {old['elevation']}, \
init_level = {old['init_level']}, min_level = {old['min_level']}, max_level = {old['max_level']}, \
diameter = {old['diameter']}, min_vol = {old['min_vol']}, vol_curve = {old['vol_curve']}, overflow = {old['overflow']} where id = ''{id}'';"
redo_cs = g_add_prefix | new.as_dict()
undo_cs = g_delete_prefix | new.as_id_dict()
write(name, sql)
add_operation(name, sql.replace("'", "''"), undo, 'set_tank', API_UPDATE, TANK, id, ps)
return get_current_change_set(name)
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
def delete_tank(name: str, id: str) -> ChangeSet:
row = get_tank(name, id)
if row == None:
return ChangeSet()
def delete_tank(name: str, cs: ChangeSet) -> ChangeSet:
old = Tank(get_tank(name, cs.operations[0]['id']))
old = Serialize(get_tank(name, id), schema).to_storage()
redo_sql = f"delete from coordinates where node = {old.f_id};"
redo_sql += f"\ndelete from tanks where id = {old.f_id};"
redo_sql += f"\ndelete from _node where id = {old.f_id};"
sql = f"delete from coordinates where node = '{id}';"
sql += f"\ndelete from tanks where id = '{id}';"
sql += f"\ndelete from _node where id = '{id}';"
undo_sql = f"insert into _node (id, type) values ({old.f_id}, {old.f_type});"
undo_sql += f"\ninsert into tanks (id, elevation, init_level, min_level, max_level, diameter, min_vol, vol_curve, overflow) values ({old.f_id}, {old.f_elevation}, {old.f_init_level}, {old.f_min_level}, {old.f_max_level}, {old.f_diameter}, {old.f_min_vol}, {old.f_vol_curve}, {old.f_overflow});"
undo_sql += f"\ninsert into coordinates (node, coord) values ({old.f_id}, {old.f_coord});"
undo = f"insert into _node (id, type) values (''{id}'', ''{TANK}'');"
undo += f"\ninsert into tanks (id, elevation, init_level, min_level, max_level, diameter, min_vol, vol_curve, overflow) \
values (''{id}'', {old['elevation']}, {old['init_level']}, {old['min_level']}, {old['max_level']}, {old['diameter']}, {old['min_vol']}, {old['vol_curve']}, {old['overflow']});"
undo += f"\ninsert into coordinates (node, coord) values (''{id}'', {old['coord']});"
redo_cs = g_delete_prefix | old.as_id_dict()
undo_cs = g_add_prefix | old.as_dict()
write(name, sql)
add_operation(name, sql.replace("'", "''"), undo, 'delete_tank', API_DELETE, TANK, id)
return get_current_change_set(name)
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)