From 7860faadd713c6952fe75aa9498bd83f0389eda6 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Sun, 30 Oct 2022 14:19:20 +0800 Subject: [PATCH 1/3] Let set_demand be a batch command --- api/s9_demands.py | 52 +++++++++++++++++++++++++++++++++++++++-------- test_tjnetwork.py | 30 +++++++++++++++++++++++---- tjnetwork.py | 4 ++-- 3 files changed, 71 insertions(+), 15 deletions(-) diff --git a/api/s9_demands.py b/api/s9_demands.py index 77518c1..90cd815 100644 --- a/api/s9_demands.py +++ b/api/s9_demands.py @@ -1,5 +1,5 @@ from .operation import * - +from .s2_junctions import * def get_demand_schema(name: str) -> dict[str, dict[str, Any]]: return { 'junction' : {'type': 'str' , 'optional': False , 'readonly': True }, @@ -21,7 +21,15 @@ def get_demand(name: str, junction: str) -> dict[str, Any]: return { 'junction': junction, 'demands': ds } -def set_demand_cache(name: str, cs: ChangeSet) -> SqlChangeSet: +class _SqlChangeSet: + def __init__(self, redo_sql: str, undo_sql: str, redo_cs: list[dict[str, str]], undo_cs: list[dict[str, str]]) -> None: + self.redo_sql = redo_sql + self.undo_sql = undo_sql + self.redo_cs = redo_cs + self.undo_cs = undo_cs + + +def set_demand_cache(name: str, cs: ChangeSet) -> _SqlChangeSet: junction = cs.operations[0]['junction'] old = get_demand(name, junction) new = { 'junction': junction, 'demands': [] } @@ -40,7 +48,8 @@ def set_demand_cache(name: str, cs: ChangeSet) -> SqlChangeSet: redo_sql += f"\ninsert into demands (junction, demand, pattern, category) values ({f_junction}, {f_demand}, {f_pattern}, {f_category});" new['demands'].append({ 'demand': demand, 'pattern': pattern, 'category': category }) - undo_sql = f"delete from demands where junction = {f_junction};" + undo_sql = '' + _undo_sql = f"delete from demands where junction = {f_junction};" for r in old['demands']: demand = float(r['demand']) pattern = str(r['pattern']) @@ -48,13 +57,38 @@ def set_demand_cache(name: str, cs: ChangeSet) -> SqlChangeSet: f_demand = demand f_pattern = f"'{pattern}'" if pattern != None else 'null' f_category = f"'{category}'" if category != None else 'null' - undo_sql += f"\ninsert into demands (junction, demand, pattern, category) values ({f_junction}, {f_demand}, {f_pattern}, {f_category});" - - redo_cs = g_update_prefix | { 'type': 'demand' } | new - undo_cs = g_update_prefix | { 'type': 'demand' } | old + _undo_sql += f"\ninsert into demands (junction, demand, pattern, category) values ({f_junction}, {f_demand}, {f_pattern}, {f_category});" - return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs) + redo_cs = [] + redo_cs.append(g_update_prefix | { 'type': 'demand' } | new) + undo_cs = [] + undo_cs.append(g_update_prefix | { 'type': 'demand' } | old) + + cache = None | SqlChangeSet + if len(cs.operations[0]['demands']) > 0: + r = cs.operations[0]['demands'][0] + demand = float(r['demand']) + pattern = str(r['pattern']) if 'pattern' in r and r['pattern'] != None else None + cache = set_junction_cache(name, ChangeSet({'id': junction, 'demand': demand, 'pattern': pattern})) + else: + cache = set_junction_cache(name, ChangeSet({'id': junction, 'demand': None, 'pattern': None})) + + if cache != None: + redo_sql += '\n' + redo_sql += cache.redo_sql + + undo_sql += cache.undo_sql + undo_sql += '\n' + undo_sql += _undo_sql + + redo_cs.append(cache.redo_cs) + + undo_cs.append(cache.undo_cs) + undo_cs.reverse() + + return _SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs) def set_demand(name: str, cs: ChangeSet) -> ChangeSet: - return execute_command(name, set_demand_cache(name, cs)) + css = set_demand_cache(name, cs) + return execute_batch(name, css.redo_sql, css.undo_sql, css.redo_cs, css.undo_cs) diff --git a/test_tjnetwork.py b/test_tjnetwork.py index aa8e8c7..5ca0e1a 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -1323,8 +1323,12 @@ class TestApi: assert ds[1]['pattern'] == None assert ds[1]['category'] == None + assert get_junction(p, 'j1')['demand'] == 10.0 + set_demand(p, ChangeSet({'junction': 'j1', 'demands': []})) + assert get_junction(p, 'j1')['demand'] == None + d = get_demand(p, 'j1') assert d['junction'] == 'j1' assert d['demands'] == [] @@ -1339,8 +1343,9 @@ class TestApi: add_junction(p, ChangeSet({'id': 'j1', 'x': 0.0, 'y': 10.0, 'elevation': 20.0})) assert is_junction(p, 'j1') - cs = set_demand(p, ChangeSet({'junction': 'j1', 'demands': [{'demand': 10.0, 'pattern': None, 'category': 'x'}, - {'demand': 20.0, 'pattern': None, 'category': None}]})).operations[0] + result = set_demand(p, ChangeSet({'junction': 'j1', 'demands': [{'demand': 10.0, 'pattern': None, 'category': 'x'}, + {'demand': 20.0, 'pattern': None, 'category': None}]})) + cs = result.operations[0] assert cs['operation'] == API_UPDATE assert cs['type'] == 'demand' assert cs['junction'] == 'j1' @@ -1352,14 +1357,26 @@ class TestApi: assert ds[1]['demand'] == 20.0 assert ds[1]['pattern'] == None assert ds[1]['category'] == None + cs = result.operations[1] + assert cs['operation'] == API_UPDATE + assert cs['type'] == JUNCTION + assert cs['id'] == 'j1' + assert cs['demand'] == 10.0 - cs = execute_undo(p).operations[0] + result = execute_undo(p) + cs = result.operations[0] + assert cs['operation'] == API_UPDATE + assert cs['type'] == JUNCTION + assert cs['id'] == 'j1' + assert cs['demand'] == None + cs = result.operations[1] assert cs['operation'] == API_UPDATE assert cs['type'] == 'demand' assert cs['junction'] == 'j1' assert len(cs['demands']) == 0 - cs = execute_redo(p).operations[0] + result = execute_redo(p) + cs = result.operations[0] assert cs['operation'] == API_UPDATE assert cs['type'] == 'demand' assert cs['junction'] == 'j1' @@ -1371,6 +1388,11 @@ class TestApi: assert ds[1]['demand'] == 20.0 assert ds[1]['pattern'] == None assert ds[1]['category'] == None + cs = result.operations[1] + assert cs['operation'] == API_UPDATE + assert cs['type'] == JUNCTION + assert cs['id'] == 'j1' + assert cs['demand'] == 10.0 self.leave(p) diff --git a/tjnetwork.py b/tjnetwork.py index 862bb24..558e6f4 100644 --- a/tjnetwork.py +++ b/tjnetwork.py @@ -427,7 +427,7 @@ def delete_curve(name: str, cs: ChangeSet) -> ChangeSet: ############################################################ -# control 12.[CONTROLS] +# control 13.[CONTROLS] ############################################################ def get_control_schema(name: str) -> dict[str, dict[str, Any]]: @@ -442,7 +442,7 @@ def set_control(name: str, cs: ChangeSet) -> ChangeSet: ############################################################ -# rule 13.[RULES] +# rule 14.[RULES] ############################################################ def get_rule_schema(name: str) -> dict[str, dict[str, Any]]: From 23ce9d73d09e657f578b1e75671553754503555e Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Sun, 30 Oct 2022 14:43:22 +0800 Subject: [PATCH 2/3] Add batch change set for set demand --- api/operation.py | 7 +++++++ api/s9_demands.py | 14 +++----------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/api/operation.py b/api/operation.py index 7fe031b..cf0293e 100644 --- a/api/operation.py +++ b/api/operation.py @@ -49,6 +49,13 @@ class SqlChangeSet: self.redo_cs = redo_cs self.undo_cs = undo_cs +class BatchSqlChangeSet: + def __init__(self, redo_sql: str, undo_sql: str, redo_cs: list[dict[str, str]], undo_cs: list[dict[str, str]]) -> None: + self.redo_sql = redo_sql + self.undo_sql = undo_sql + self.redo_cs = redo_cs + self.undo_cs = undo_cs + def read(name: str, sql: str) -> Row: with conn[name].cursor(row_factory=dict_row) as cur: diff --git a/api/s9_demands.py b/api/s9_demands.py index 90cd815..399e77d 100644 --- a/api/s9_demands.py +++ b/api/s9_demands.py @@ -21,15 +21,7 @@ def get_demand(name: str, junction: str) -> dict[str, Any]: return { 'junction': junction, 'demands': ds } -class _SqlChangeSet: - def __init__(self, redo_sql: str, undo_sql: str, redo_cs: list[dict[str, str]], undo_cs: list[dict[str, str]]) -> None: - self.redo_sql = redo_sql - self.undo_sql = undo_sql - self.redo_cs = redo_cs - self.undo_cs = undo_cs - - -def set_demand_cache(name: str, cs: ChangeSet) -> _SqlChangeSet: +def set_demand_cache(name: str, cs: ChangeSet) -> BatchSqlChangeSet: junction = cs.operations[0]['junction'] old = get_demand(name, junction) new = { 'junction': junction, 'demands': [] } @@ -82,11 +74,11 @@ def set_demand_cache(name: str, cs: ChangeSet) -> _SqlChangeSet: undo_sql += _undo_sql redo_cs.append(cache.redo_cs) - + undo_cs.append(cache.undo_cs) undo_cs.reverse() - return _SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs) + return BatchSqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs) def set_demand(name: str, cs: ChangeSet) -> ChangeSet: From e8abbef5a67e2dfc8b13500ca57a1278ac58f3b1 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Sun, 30 Oct 2022 23:05:34 +0800 Subject: [PATCH 3/3] Support to list project --- api/__init__.py | 2 +- api/project.py | 9 +++++++++ tjnetwork.py | 3 +++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/api/__init__.py b/api/__init__.py index a37ccdd..6e6840e 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -1,4 +1,4 @@ -from .project import have_project, create_project, delete_project +from .project import list_project, have_project, create_project, delete_project from .project import is_project_open, get_project_open_count, open_project, close_project from .project import copy_project diff --git a/api/project.py b/api/project.py index 30743aa..c9cdfb0 100644 --- a/api/project.py +++ b/api/project.py @@ -1,10 +1,19 @@ import psycopg as pg +from psycopg.rows import dict_row from .connection import g_conn_dict as conn # no undo/redo _project_open_count: dict[str, int] = {} +def list_project() -> list[str]: + ps = [] + with pg.connect(conninfo="dbname=postgres host=127.0.0.1", autocommit=True) as conn: + with conn.cursor(row_factory=dict_row) as cur: + for p in cur.execute(f"select datname from pg_database where datname <> 'postgres' and datname <> 'template0' and datname <> 'template1' and datname <> 'project'"): + ps.append(p['datname']) + return ps + def have_project(name: str) -> bool: with pg.connect(conninfo="dbname=postgres host=127.0.0.1", autocommit=True) as conn: with conn.cursor() as cur: diff --git a/tjnetwork.py b/tjnetwork.py index 558e6f4..f927a05 100644 --- a/tjnetwork.py +++ b/tjnetwork.py @@ -116,6 +116,9 @@ def read_inp(name: str, inp: str) -> None: # operation ############################################################ +def list_project() -> list[str]: + return api.list_project() + def get_current_operation(name: str) -> int: return api.get_current_operation(name)