Add option api and test

This commit is contained in:
WQY\qiong
2022-10-22 15:31:22 +08:00
parent 15ed42e5ac
commit 3dae83b18d
6 changed files with 319 additions and 0 deletions

View File

@@ -52,4 +52,11 @@ from .s16_emitters import get_emitter_schema, get_emitter, set_emitter
from .s21_times import TIME_STATISTIC_NONE, TIME_STATISTIC_AVERAGED, TIME_STATISTIC_MINIMUM, TIME_STATISTIC_MAXIMUM, TIME_STATISTIC_RANGE
from .s21_times import get_time_schema, get_time, set_time
from .s23_options import OPTION_UNITS_CFS, OPTION_UNITS_GPM, OPTION_UNITS_MGD, OPTION_UNITS_IMGD, OPTION_UNITS_AFD, OPTION_UNITS_LPS, OPTION_UNITS_LPM, OPTION_UNITS_MLD, OPTION_UNITS_CMH, OPTION_UNITS_CMD
from .s23_options import OPTION_HEADLOSS_HW, OPTION_HEADLOSS_DW, OPTION_HEADLOSS_CM
from .s23_options import OPTION_UNBALANCED_STOP, OPTION_UNBALANCED_CONTINUE
from .s23_options import OPTION_DEMAND_MODEL_DDA, OPTION_DEMAND_MODEL_PDA
from .s23_options import OPTION_QUALITY_NONE, OPTION_QUALITY_CHEMICAL, OPTION_QUALITY_AGE, OPTION_QUALITY_TRACE
from .s23_options import get_option_schema, get_option, set_option
from .s24_coordinates import get_node_coord

View File

@@ -11,6 +11,7 @@ from .s11_patterns import *
from .s12_curves import *
from .s16_emitters import *
from .s21_times import *
from .s23_options import *
def execute_add_command(name: str, cs: ChangeSet) -> ChangeSet:
@@ -61,6 +62,8 @@ def execute_update_command(name: str, cs: ChangeSet) -> ChangeSet:
return set_emitter(name, cs)
elif type == 'time':
return set_time(name, cs)
elif type == 'option':
return set_option(name, cs)
return ChangeSet()

View File

101
api/s23_options.py Normal file
View File

@@ -0,0 +1,101 @@
from .operation import *
OPTION_UNITS_CFS = 'CFS'
OPTION_UNITS_GPM = 'GPM'
OPTION_UNITS_MGD = 'MGD'
OPTION_UNITS_IMGD = 'IMGD'
OPTION_UNITS_AFD = 'AFD'
OPTION_UNITS_LPS = 'LPS'
OPTION_UNITS_LPM = 'LPM'
OPTION_UNITS_MLD = 'MLD'
OPTION_UNITS_CMH = 'CMH'
OPTION_UNITS_CMD = 'CMD'
OPTION_HEADLOSS_HW = 'H-W'
OPTION_HEADLOSS_DW = 'D-W'
OPTION_HEADLOSS_CM = 'C-M'
#OPTION_HYDRAULICS_USE = 'USE'
#OPTION_HYDRAULICS_SAVE = 'SAVE'
OPTION_UNBALANCED_STOP = 'STOP'
OPTION_UNBALANCED_CONTINUE = 'CONTINUE'
OPTION_DEMAND_MODEL_DDA = 'DDA'
OPTION_DEMAND_MODEL_PDA = 'PDA'
OPTION_QUALITY_NONE = 'NONE'
OPTION_QUALITY_CHEMICAL = 'CHEMICAL'
OPTION_QUALITY_AGE = 'AGE'
OPTION_QUALITY_TRACE = 'TRACE'
element_schema = {'type': 'str' , 'optional': True , 'readonly': False}
def get_option_schema(name: str) -> dict[str, dict[str, Any]]:
return { 'UNITS' : element_schema,
'HEADLOSS' : element_schema,
#'HYDRAULICS' : element_schema,
'VISCOSITY' : element_schema,
'SPECIFIC GRAVITY' : element_schema,
'TRIALS' : element_schema,
'ACCURACY' : element_schema,
'FLOWCHANGE' : element_schema,
'HEADERROR' : element_schema,
'CHECKFREQ' : element_schema,
'MAXCHECK' : element_schema,
'DAMPLIMIT' : element_schema,
'UNBALANCED' : element_schema,
'DEMAND MODEL' : element_schema,
'MINIMUM PRESSURE' : element_schema,
'REQUIRED PRESSURE' : element_schema,
'PRESSURE EXPONENT' : element_schema,
'PATTERN' : element_schema,
'DEMAND MULTIPLIER' : element_schema,
'EMITTER EXPONENT' : element_schema,
'QUALITY' : element_schema,
'DIFFUSIVITY' : element_schema,
'TOLERANCE' : element_schema,
#'QUALITY' : element_schema,
}
def get_option(name: str) -> dict[str, Any]:
ts = read_all(name, f"select * from options")
d = {}
for e in ts:
d[e['key']] = str(e['value'])
return d
def set_option(name: str, cs: ChangeSet) -> ChangeSet:
raw_old = get_option(name)
old = {}
new = {}
new_dict = cs.operations[0]
schema = get_option_schema(name)
for key in schema.keys():
if key in new_dict:
old[key] = str(raw_old[key])
new[key] = str(new_dict[key])
redo_cs = g_update_prefix | { 'type' : 'option' }
redo_sql = ''
for key, value in new.items():
if redo_sql != '':
redo_sql += '\n'
redo_sql += f"update options set value = '{value}' where key = '{key}';"
redo_cs |= { key: value }
undo_cs = g_update_prefix | { 'type' : 'option' }
undo_sql = ''
for key, value in old.items():
if undo_sql != '':
undo_sql += '\n'
undo_sql += f"update options set value = '{value}' where key = '{key}';"
undo_cs |= { key: value }
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)