Support "TOTAL DURATION"

This commit is contained in:
WQY\qiong
2023-06-17 10:37:13 +08:00
parent 80fc13b74e
commit 6a35a41f51

View File

@@ -1,107 +1,112 @@
from .database import * from .database import *
TIME_STATISTIC_NONE = 'NONE' TIME_STATISTIC_NONE = 'NONE'
TIME_STATISTIC_AVERAGED = 'AVERAGED' TIME_STATISTIC_AVERAGED = 'AVERAGED'
TIME_STATISTIC_MINIMUM = 'MINIMUM' TIME_STATISTIC_MINIMUM = 'MINIMUM'
TIME_STATISTIC_MAXIMUM = 'MAXIMUM' TIME_STATISTIC_MAXIMUM = 'MAXIMUM'
TIME_STATISTIC_RANGE = 'RANGE' TIME_STATISTIC_RANGE = 'RANGE'
element_schema = {'type': 'str' , 'optional': True , 'readonly': False} element_schema = {'type': 'str' , 'optional': True , 'readonly': False}
def get_time_schema(name: str) -> dict[str, dict[str, Any]]: def get_time_schema(name: str) -> dict[str, dict[str, Any]]:
return { 'DURATION' : element_schema, return { 'DURATION' : element_schema,
'HYDRAULIC TIMESTEP' : element_schema, 'HYDRAULIC TIMESTEP' : element_schema,
'QUALITY TIMESTEP' : element_schema, 'QUALITY TIMESTEP' : element_schema,
'RULE TIMESTEP' : element_schema, 'RULE TIMESTEP' : element_schema,
'PATTERN TIMESTEP' : element_schema, 'PATTERN TIMESTEP' : element_schema,
'PATTERN START' : element_schema, 'PATTERN START' : element_schema,
'REPORT TIMESTEP' : element_schema, 'REPORT TIMESTEP' : element_schema,
'REPORT START' : element_schema, 'REPORT START' : element_schema,
'START CLOCKTIME' : element_schema, 'START CLOCKTIME' : element_schema,
'STATISTIC' : element_schema} 'STATISTIC' : element_schema}
def get_time(name: str) -> dict[str, Any]: def get_time(name: str) -> dict[str, Any]:
ts = read_all(name, f"select * from times") ts = read_all(name, f"select * from times")
d = {} d = {}
for e in ts: for e in ts:
d[e['key']] = str(e['value']) d[e['key']] = str(e['value'])
return d return d
def _set_time(name: str, cs: ChangeSet) -> DbChangeSet: def _set_time(name: str, cs: ChangeSet) -> DbChangeSet:
raw_old = get_time(name) raw_old = get_time(name)
old = {} old = {}
new = {} new = {}
new_dict = cs.operations[0] new_dict = cs.operations[0]
schema = get_time_schema(name) schema = get_time_schema(name)
for key in schema.keys(): for key in schema.keys():
if key in new_dict: if key in new_dict:
old[key] = str(raw_old[key]) old[key] = str(raw_old[key])
new[key] = str(new_dict[key]) new[key] = str(new_dict[key])
redo_cs = g_update_prefix | { 'type' : 'time' } redo_cs = g_update_prefix | { 'type' : 'time' }
redo_sql = '' redo_sql = ''
for key, value in new.items(): for key, value in new.items():
if redo_sql != '': if redo_sql != '':
redo_sql += '\n' redo_sql += '\n'
redo_sql += f"update times set value = '{value}' where key = '{key}';" redo_sql += f"update times set value = '{value}' where key = '{key}';"
redo_cs |= { key: value } redo_cs |= { key: value }
undo_cs = g_update_prefix | { 'type' : 'time' } undo_cs = g_update_prefix | { 'type' : 'time' }
undo_sql = '' undo_sql = ''
for key, value in old.items(): for key, value in old.items():
if undo_sql != '': if undo_sql != '':
undo_sql += '\n' undo_sql += '\n'
undo_sql += f"update times set value = '{value}' where key = '{key}';" undo_sql += f"update times set value = '{value}' where key = '{key}';"
undo_cs |= { key: value } undo_cs |= { key: value }
return DbChangeSet(redo_sql, undo_sql, [redo_cs], [undo_cs]) return DbChangeSet(redo_sql, undo_sql, [redo_cs], [undo_cs])
def set_time(name: str, cs: ChangeSet) -> ChangeSet: def set_time(name: str, cs: ChangeSet) -> ChangeSet:
return execute_command(name, _set_time(name, cs)) return execute_command(name, _set_time(name, cs))
#-------------------------------------------------------------- #--------------------------------------------------------------
# [EPA2][EPA3] # [EPA2][EPA3]
# STATISTIC {NONE/AVERAGE/MIN/MAX/RANGE} # STATISTIC {NONE/AVERAGE/MIN/MAX/RANGE}
# DURATION value (units) # DURATION value (units)
# HYDRAULIC TIMESTEP value (units) # HYDRAULIC TIMESTEP value (units)
# QUALITY TIMESTEP value (units) # QUALITY TIMESTEP value (units)
# RULE TIMESTEP value (units) # RULE TIMESTEP value (units)
# PATTERN TIMESTEP value (units) # PATTERN TIMESTEP value (units)
# PATTERN START value (units) # PATTERN START value (units)
# REPORT TIMESTEP value (units) # REPORT TIMESTEP value (units)
# REPORT START value (units) # REPORT START value (units)
# START CLOCKTIME value (AM PM) # START CLOCKTIME value (AM PM)
# [EPA3] supports [EPA2] keyword # [EPA3] supports [EPA2] keyword
#-------------------------------------------------------------- #--------------------------------------------------------------
def inp_in_time(section: list[str]) -> str: def inp_in_time(section: list[str]) -> str:
sql = '' sql = ''
for s in section: for s in section:
if s.startswith(';'): if s.startswith(';'):
continue continue
line = s.upper().strip() line = s.upper().strip()
for key in get_time_schema('').keys():
if line.startswith(key): # TOTAL DURATION => DURATION
value = line.removeprefix(key).strip() if line.startswith('TOTAL DURATION'):
sql += f"update times set value = '{value}' where key = '{key}';" line = line.replace('TOTAL DURATION', 'DURATION')
return sql
for key in get_time_schema('').keys():
if line.startswith(key):
def inp_out_time(name: str) -> list[str]: value = line.removeprefix(key).strip()
lines = [] sql += f"update times set value = '{value}' where key = '{key}';"
objs = read_all(name, f"select * from times") return sql
for obj in objs:
key = obj['key']
value = obj['value'] def inp_out_time(name: str) -> list[str]:
lines.append(f'{key} {value}') lines = []
return lines objs = read_all(name, f"select * from times")
for obj in objs:
key = obj['key']
value = obj['value']
lines.append(f'{key} {value}')
return lines