Huge refactor to api and add batch api
This commit is contained in:
195
api/s5_pipes.py
195
api/s5_pipes.py
@@ -1,10 +1,5 @@
|
||||
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 *
|
||||
|
||||
|
||||
PIPE_STATUS_OPEN = 'open'
|
||||
@@ -12,125 +7,105 @@ PIPE_STATUS_CLOSED = 'closed'
|
||||
PIPE_STATUS_CV = 'cv'
|
||||
|
||||
|
||||
schema: dict[str, dict[str, Any]] = { \
|
||||
'id' : define_property(str_type, False, True), \
|
||||
'node1' : define_property(str_type), \
|
||||
'node2' : define_property(str_type), \
|
||||
'length' : define_property(float_type), \
|
||||
'diameter' : define_property(float_type), \
|
||||
'roughness' : define_property(float_type), \
|
||||
'minor_loss': define_property(float_type), \
|
||||
'status' : define_property(str_type)}
|
||||
|
||||
|
||||
def get_pipe_schema(name: str) -> dict[str, dict[str, Any]]:
|
||||
return schema
|
||||
return { 'id' : {'type': 'str' , 'optional': False , 'readonly': True },
|
||||
'node1' : {'type': "str" , 'optional': False , 'readonly': False},
|
||||
'node2' : {'type': "str" , 'optional': False , 'readonly': False},
|
||||
'length' : {'type': "float" , 'optional': False , 'readonly': False},
|
||||
'diameter' : {'type': "float" , 'optional': False , 'readonly': False},
|
||||
'roughness' : {'type': "float" , 'optional': False , 'readonly': False},
|
||||
'minor_loss' : {'type': "float" , 'optional': False , 'readonly': False},
|
||||
'status' : {'type': "str" , 'optional': False , 'readonly': False} }
|
||||
|
||||
|
||||
def _query_pipe(name: str, id: str) -> Row | None:
|
||||
return read(name, f"select * from pipes where id = '{id}'")
|
||||
def get_pipe(name: str, id: str) -> dict[str, Any]:
|
||||
p = read(name, f"select * from pipes where id = '{id}'")
|
||||
d = {}
|
||||
d['id'] = str(p['id'])
|
||||
d['node1'] = str(p['node1'])
|
||||
d['node2'] = str(p['node2'])
|
||||
d['length'] = float(p['length'])
|
||||
d['diameter'] = float(p['diameter'])
|
||||
d['roughness'] = float(p['roughness'])
|
||||
d['minor_loss'] = float(p['minor_loss'])
|
||||
d['status'] = str(p['status'])
|
||||
return d
|
||||
|
||||
|
||||
def _get_pipe_node1(name: str, id: str) -> str | None:
|
||||
row = _query_pipe(name, id)
|
||||
return row['node1'] if row != None else None
|
||||
class Pipe(object):
|
||||
def __init__(self, input: dict[str, Any]) -> None:
|
||||
self.type = 'pipe'
|
||||
self.id = str(input['id'])
|
||||
self.node1 = str(input['node1'])
|
||||
self.node2 = str(input['node2'])
|
||||
self.length = float(input['length'])
|
||||
self.diameter = float(input['diameter'])
|
||||
self.roughness = float(input['roughness'])
|
||||
self.minor_loss = float(input['minor_loss'])
|
||||
self.status = str(input['status'])
|
||||
|
||||
self.f_type = f"'{self.type}'"
|
||||
self.f_id = f"'{self.id}'"
|
||||
self.f_node1 = f"'{self.node1}'"
|
||||
self.f_node2 = f"'{self.node2}'"
|
||||
self.f_length = self.length
|
||||
self.f_diameter = self.diameter
|
||||
self.f_roughness = self.roughness
|
||||
self.f_minor_loss = self.minor_loss
|
||||
self.f_status = f"'{self.status}'"
|
||||
|
||||
def as_dict(self) -> dict[str, Any]:
|
||||
return { 'type': self.type, 'id': self.id, 'node1': self.node1, 'node2': self.node2, 'length': self.length, 'diameter': self.diameter, 'roughness': self.roughness, 'minor_loss': self.minor_loss, 'status': self.status }
|
||||
|
||||
def as_id_dict(self) -> dict[str, Any]:
|
||||
return { 'type': self.type, 'id': self.id }
|
||||
|
||||
|
||||
def _get_pipe_node2(name: str, id: str) -> str | None:
|
||||
row = _query_pipe(name, id)
|
||||
return row['node2'] if row != None else None
|
||||
def set_pipe(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
old = Pipe(get_pipe(name, cs.operations[0]['id']))
|
||||
raw_new = get_pipe(name, cs.operations[0]['id'])
|
||||
|
||||
new_dict = cs.operations[0]
|
||||
schema = get_pipe_schema(name)
|
||||
for key, value in schema.items():
|
||||
if key in new_dict and not value['readonly']:
|
||||
raw_new[key] = new_dict[key]
|
||||
new = Pipe(raw_new)
|
||||
|
||||
redo_sql = f"update pipes set node1 = {new.f_node1}, node2 = {new.f_node2}, length = {new.f_length}, diameter = {new.f_diameter}, roughness = {new.f_roughness}, minor_loss = {new.f_minor_loss}, status = {new.f_status} where id = {new.f_id};"
|
||||
undo_sql = f"update pipes set node1 = {old.f_node1}, node2 = {old.f_node2}, length = {old.f_length}, diameter = {old.f_diameter}, roughness = {old.f_roughness}, minor_loss = {old.f_minor_loss}, status = {old.f_status} 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 add_pipe(name: str, id: str, node1: str, node2: str, length: float = 0, diameter: float = 0, roughness: float = 0, minor_loss: float = 0, status: str = PIPE_STATUS_OPEN) -> ChangeSet:
|
||||
if is_pipe(name, id):
|
||||
return ChangeSet()
|
||||
if not is_node(name, node1):
|
||||
return ChangeSet()
|
||||
if not is_node(name, node2):
|
||||
return ChangeSet()
|
||||
if node1 == node2:
|
||||
return ChangeSet()
|
||||
if status != PIPE_STATUS_OPEN and status != PIPE_STATUS_CLOSED and status != PIPE_STATUS_CV:
|
||||
return ChangeSet()
|
||||
def add_pipe(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
new = Pipe(cs.operations[0])
|
||||
|
||||
sql = f"insert into _link (id, type) values ('{id}', '{PIPE}');"
|
||||
sql += f"\ninsert into pipes (id, node1, node2, length, diameter, roughness, minor_loss, status) values ('{id}', '{node1}', '{node2}', {length}, {diameter}, {roughness}, {minor_loss}, '{status}');"
|
||||
redo_sql = f"insert into _link (id, type) values ({new.f_id}, {new.f_type});"
|
||||
redo_sql += f"\ninsert into pipes (id, node1, node2, length, diameter, roughness, minor_loss, status) values ({new.f_id}, {new.f_node1}, {new.f_node2}, {new.f_length}, {new.f_diameter}, {new.f_roughness}, {new.f_minor_loss}, {new.f_status});"
|
||||
|
||||
undo = f"delete from pipes where id = ''{id}'';"
|
||||
undo += f"\ndelete from _link where id = ''{id}'';"
|
||||
undo_sql = f"delete from pipes where id = {new.f_id};"
|
||||
undo_sql += f"\ndelete from _link where id = {new.f_id};"
|
||||
|
||||
write(name, sql)
|
||||
add_operation(name, sql.replace("'", "''"), undo, 'add_pipe', API_ADD, PIPE, id)
|
||||
return get_current_change_set(name)
|
||||
redo_cs = g_add_prefix | new.as_dict()
|
||||
undo_cs = g_delete_prefix | new.as_id_dict()
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def get_pipe(name: str, id: str) -> dict[str, Any] | None:
|
||||
row = _query_pipe(name, id)
|
||||
if row == None:
|
||||
return None
|
||||
def delete_pipe(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
old = Pipe(get_pipe(name, cs.operations[0]['id']))
|
||||
|
||||
ps: dict[str, str] = {}
|
||||
ps['id'] = id
|
||||
ps['node1'] = row['node1']
|
||||
ps['node2'] = row['node2']
|
||||
ps['length'] = float(row['length'])
|
||||
ps['diameter'] = float(row['diameter'])
|
||||
ps['roughness'] = float(row['roughness'])
|
||||
ps['minor_loss'] = float(row['minor_loss'])
|
||||
ps['status'] = row['status']
|
||||
return ps
|
||||
redo_sql = f"delete from pipes where id = {old.f_id};"
|
||||
redo_sql += f"\ndelete from _link where id = {old.f_id};"
|
||||
|
||||
undo_sql = f"insert into _link (id, type) values ({old.f_id}, {old.f_type});"
|
||||
undo_sql += f"\ninsert into pipes (id, node1, node2, length, diameter, roughness, minor_loss, status) values ({old.f_id}, {old.f_node1}, {old.f_node2}, {old.f_length}, {old.f_diameter}, {old.f_roughness}, {old.f_minor_loss}, {old.f_status});"
|
||||
|
||||
def set_pipe(name: str, id: str, properties: dict[str, Any]) -> ChangeSet:
|
||||
if not is_pipe(name, id):
|
||||
return ChangeSet()
|
||||
if 'node1' in properties:
|
||||
if not is_node(name, properties['node1']) or _get_pipe_node2(name, id) == properties['node1']:
|
||||
return ChangeSet()
|
||||
if 'node2' in properties:
|
||||
if not is_node(name, properties['node2']) or _get_pipe_node1(name, id) == properties['node2']:
|
||||
return ChangeSet()
|
||||
if 'node1' in properties and 'node2' in properties:
|
||||
if properties['node1'] == properties['node2']:
|
||||
return ChangeSet()
|
||||
if 'status' in properties:
|
||||
if properties['status'] != PIPE_STATUS_OPEN and properties['status'] != PIPE_STATUS_CLOSED and properties['status'] != PIPE_STATUS_CV:
|
||||
return ChangeSet()
|
||||
redo_cs = g_delete_prefix | old.as_id_dict()
|
||||
undo_cs = g_add_prefix | old.as_dict()
|
||||
|
||||
old = Serialize(get_pipe(name, id), schema).to_storage()
|
||||
|
||||
new = get_pipe(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()
|
||||
|
||||
sql = f"update pipes set node1 = {new['node1']}, node2 = {new['node2']}, \
|
||||
length = {new['length']}, diameter = {new['diameter']}, roughness = {new['roughness']}, minor_loss = {new['minor_loss']}, status = {new['status']} where id = '{id}';"
|
||||
undo = f"update pipes set node1 = {old['node1']}, node2 = {old['node2']}, \
|
||||
length = {old['length']}, diameter = {old['diameter']}, roughness = {old['roughness']}, minor_loss = {old['minor_loss']}, status = {old['status']} where id = ''{id}'';"
|
||||
|
||||
write(name, sql)
|
||||
add_operation(name, sql.replace("'", "''"), undo, 'set_pipe', API_UPDATE, PIPE, id, ps)
|
||||
return get_current_change_set(name)
|
||||
|
||||
|
||||
def delete_pipe(name: str, id: str) -> ChangeSet:
|
||||
row = get_pipe(name, id)
|
||||
if row == None:
|
||||
return ChangeSet()
|
||||
|
||||
old = Serialize(get_pipe(name, id), schema).to_storage()
|
||||
|
||||
sql = f"delete from pipes where id = '{id}';"
|
||||
sql += f"\ndelete from _link where id = '{id}';"
|
||||
|
||||
undo = f"insert into _link (id, type) values (''{id}'', ''{PIPE}'');"
|
||||
undo += f"\ninsert into pipes (id, node1, node2, length, diameter, roughness, minor_loss, status) \
|
||||
values (''{id}'', {old['node1']}, {old['node2']}, {old['length']}, {old['diameter']}, {old['roughness']}, {old['minor_loss']}, {old['status']});"
|
||||
|
||||
write(name, sql)
|
||||
add_operation(name, sql.replace("'", "''"), undo, 'delete_pipe', API_DELETE, PIPE, id)
|
||||
return get_current_change_set(name)
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
Reference in New Issue
Block a user