Add quality api and test

This commit is contained in:
WQY\qiong
2022-11-04 23:08:58 +08:00
parent 5de5f2f9dc
commit 03ac80a285
5 changed files with 136 additions and 2 deletions

View File

@@ -60,6 +60,8 @@ from .s15_energy import get_pump_energy_schema, get_pump_energy, set_pump_energy
from .s16_emitters import get_emitter_schema, get_emitter, set_emitter
from .s17_quality import get_quality_schema, get_quality, set_quality
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

59
api/s17_quality.py Normal file
View File

@@ -0,0 +1,59 @@
from .operation import *
def get_quality_schema(name: str) -> dict[str, dict[str, Any]]:
return { 'node' : {'type': 'str' , 'optional': False , 'readonly': True },
'quality' : {'type': 'float' , 'optional': True , 'readonly': False} }
def get_quality(name: str, node: str) -> dict[str, Any]:
e = try_read(name, f"select * from quality where node = '{node}'")
if e == None:
return { 'node': node, 'quality': None }
d = {}
d['node'] = str(e['node'])
d['quality'] = float(e['quality']) if e['quality'] != None else None
return d
class Quality(object):
def __init__(self, input: dict[str, Any]) -> None:
self.type = 'quality'
self.node = str(input['node'])
self.quality = float(input['quality']) if 'quality' in input and input['quality'] != None else None
self.f_type = f"'{self.type}'"
self.f_node = f"'{self.node}'"
self.f_quality = self.quality if self.quality != None else 'null'
def as_dict(self) -> dict[str, Any]:
return { 'type': self.type, 'node': self.node, 'quality': self.quality }
def set_quality_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
old = Quality(get_quality(name, cs.operations[0]['node']))
raw_new = get_quality(name, cs.operations[0]['node'])
new_dict = cs.operations[0]
schema = get_quality_schema(name)
for key, value in schema.items():
if key in new_dict and not value['readonly']:
raw_new[key] = new_dict[key]
new = Quality(raw_new)
redo_sql = f"delete from quality where node = {new.f_node};"
if new.quality != None:
redo_sql += f"\ninsert into quality (node, quality) values ({new.f_node}, {new.f_quality});"
undo_sql = f"delete from quality where node = {old.f_node};"
if old.quality != None:
undo_sql += f"\ninsert into quality (node, quality) values ({old.f_node}, {old.f_quality});"
redo_cs = g_update_prefix | new.as_dict()
undo_cs = g_update_prefix | old.as_dict()
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
def set_quality(name: str, cs: ChangeSet) -> ChangeSet:
return execute_command(name, set_quality_cache(name, cs))