Adists: IC: Update load and save for scenario.

scenarios
Pierre-Antoine 2025-08-14 10:31:17 +02:00
parent a525d8a2b0
commit 4a0456cbce
2 changed files with 95 additions and 64 deletions

View File

@ -114,46 +114,70 @@ class InitialConditionsAdisTS(SQLSubModel):
@classmethod @classmethod
def _db_load(cls, execute, data=None): def _db_load(cls, execute, data=None):
new = [] new = []
status = data['status']
scenario = data["scenario"]
loaded = data['loaded_pid']
if scenario is None:
return new
table = execute( table = execute(
"SELECT pamhyr_id, pollutant, name, concentration, eg, em, ed, " + "SELECT pamhyr_id, pollutant, name, concentration, eg, em, ed, " +
"enabled, scenario " + "enabled, scenario " +
"FROM initial_conditions_adists" "FROM initial_conditions_adists " +
f"WHERE scenario = {scenario.id} " +
f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))})"
) )
if table is not None: if table is not None:
for row in table: for row in table:
IC_id = row[0] it = iter(row)
pollutant = row[1]
name = row[2]
concentration = row[3]
eg = row[4]
em = row[5]
ed = row[6]
enabled = (row[7] == 1)
IC = cls( pid = next(it)
id=IC_id, pollutant = next(it)
name = next(it)
concentration = next(it)
eg = next(it)
em = next(it)
ed = next(it)
enabled = (next(it) == 1)
owner_scenario = next(it)
ic = cls(
id=pid,
name=name, name=name,
status=data['status'] status=status,
owner_scenario=owner_scenario
) )
IC.pollutant = pollutant ic.pollutant = pollutant
IC.concentration = concentration ic.concentration = concentration
IC.eg = eg ic.eg = eg
IC.em = em ic.em = em
IC.ed = ed ic.ed = ed
IC.enabled = enabled ic.enabled = enabled
data['ic_default_id'] = IC_id data['ic_default_id'] = pid
IC._data = ICAdisTSSpec._db_load(execute, data) ic._data = ICAdisTSSpec._db_load(execute, data)
loaded.add(pid)
new.append(IC) new.append(IC)
data["scenario"] = scenario.parent
new += cls._db_load(execute, data)
data["scenario"] = scenario
return new return new
def _db_save(self, execute, data=None): def _db_save(self, execute, data=None):
execute(f"DELETE FROM initial_conditions_adists WHERE id = {self.id}") if not self.must_be_saved():
return True
execute(
"DELETE FROM initial_conditions_adists " +
f"WHERE pamhyr_id = {self.id} " +
f"AND scenario = {self._status.scenario_id}"
)
pollutant = -1 pollutant = -1
if self.pollutant is not None: if self.pollutant is not None:
@ -179,11 +203,12 @@ class InitialConditionsAdisTS(SQLSubModel):
"INSERT INTO " + "INSERT INTO " +
"initial_conditions_adists(" + "initial_conditions_adists(" +
"pamhyr_id, pollutant, name, concentration, " + "pamhyr_id, pollutant, name, concentration, " +
"eg, em, ed, enabled" + "eg, em, ed, enabled, scenario" +
") " + ") " +
"VALUES (" + "VALUES (" +
f"{self.id}, {pollutant}, '{self._db_format(self._name)}', " + f"{self.id}, {pollutant}, '{self._db_format(self._name)}', " +
f"{concentration}, {eg}, {em}, {ed}, {self._enabled}" + f"{concentration}, {eg}, {em}, {ed}, {self._enabled}, " +
f"{self._status.scenario_id}" +
")" ")"
) )
@ -192,7 +217,8 @@ class InitialConditionsAdisTS(SQLSubModel):
data['ic_default_id'] = self.id data['ic_default_id'] = self.id
execute( execute(
"DELETE FROM initial_conditions_spec " + "DELETE FROM initial_conditions_spec " +
f"WHERE ic_default = {self.id}" f"WHERE ic_default = {self.id} " +
f"AND scenario = {self._status.scenario_id}"
) )
for ic_spec in self._data: for ic_spec in self._data:

View File

@ -28,9 +28,7 @@ logger = logging.getLogger()
class ICAdisTSSpec(SQLSubModel): class ICAdisTSSpec(SQLSubModel):
_sub_classes = [ _sub_classes = []
]
_id_cnt = 0
def __init__(self, id: int = -1, name: str = "", def __init__(self, id: int = -1, name: str = "",
status=None): status=None):
@ -38,11 +36,6 @@ class ICAdisTSSpec(SQLSubModel):
self._status = status self._status = status
if id == -1:
self.id = ICAdisTSSpec._id_cnt
else:
self.id = id
self._name_section = name self._name_section = name
self._reach = None self._reach = None
self._start_rk = None self._start_rk = None
@ -54,8 +47,6 @@ class ICAdisTSSpec(SQLSubModel):
self._rate = None self._rate = None
self._enabled = True self._enabled = True
ICAdisTSSpec._id_cnt = max(ICAdisTSSpec._id_cnt + 1, self.id)
@classmethod @classmethod
def _db_create(cls, execute, ext=""): def _db_create(cls, execute, ext=""):
execute(f""" execute(f"""
@ -89,7 +80,7 @@ class ICAdisTSSpec(SQLSubModel):
if int(release) < 6: if int(release) < 6:
cls._db_create(execute) cls._db_create(execute)
if major == "0" and int(minor) < 2: elif major == "0" and int(minor) < 2:
cls._db_update_to_0_2_0(execute, data) cls._db_update_to_0_2_0(execute, data)
return True return True
@ -121,35 +112,44 @@ class ICAdisTSSpec(SQLSubModel):
@classmethod @classmethod
def _db_load(cls, execute, data=None): def _db_load(cls, execute, data=None):
new = [] new = []
status = data['status']
reachs = data["edges"]
scenario = data["scenario"]
loaded = data['loaded_pid']
if scenario is None:
return new
table = execute( table = execute(
"SELECT id, ic_default, name, reach, start_rk, end_rk, " + "SELECT id, name, reach, start_rk, end_rk, " +
"concentration, eg, em, ed, rate, enabled " + "concentration, eg, em, ed, rate, enabled, scenario " +
"FROM initial_conditions_spec " + "FROM initial_conditions_spec " +
f"WHERE ic_default = {data['ic_default_id']} " f"WHERE ic_default = {data['ic_default_id']} " +
f"AND scenario = {scenario.id} " +
f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))}) "
) )
if table is not None: if table is not None:
for row in table: for row in table:
id = row[0] it = iter(row)
name = row[2]
reach = row[3]
start_rk = row[4]
end_rk = row[5]
concentration = row[6]
eg = row[7]
em = row[8]
ed = row[9]
rate = row[10]
enabled = (row[11] == 1)
# new_spec = [name, reach, start_rk, end_rk, id = next(it)
# concentration, eg, em, ed, rate, enabled] name = next(it)
reach = next(it)
start_rk = next(it)
end_rk = next(it)
concentration = next(it)
eg = next(it)
em = next(it)
ed = next(it)
rate = next(it)
enabled = (next(it) == 1)
owner_scenario = next(it)
new_spec = cls( new_spec = cls(
id=id, id=id, name=name,
name=name, status=status,
status=data['status'] owner_scenario=owner_scenario
) )
new_spec.reach = reach new_spec.reach = reach
@ -162,30 +162,35 @@ class ICAdisTSSpec(SQLSubModel):
new_spec.rate = rate new_spec.rate = rate
new_spec.enabled = enabled new_spec.enabled = enabled
loaded.add(pid)
new.append(new_spec) new.append(new_spec)
data["scenario"] = scenario.parent
new += cls._db_load(execute, data)
data["scenario"] = scenario
return new return new
def _db_save(self, execute, data=None): def _db_save(self, execute, data=None):
if not self.must_be_saved():
return True
ic_default = data['ic_default_id'] ic_default = data['ic_default_id']
sql = ( sql = (
"INSERT INTO " + "INSERT INTO " +
"initial_conditions_spec(id, ic_default, name, reach, " + "initial_conditions_spec(id, ic_default, name, reach, " +
"start_rk, end_rk, concentration, eg, em, ed, rate, enabled) " + "start_rk, end_rk, concentration, eg, em, ed, rate, " +
"enabled, scenario) " +
"VALUES (" + "VALUES (" +
f"{self.id}, " + f"{self.id}, {ic_default}, " +
f"{ic_default}, " +
f"'{self._db_format(self._name_section)}', " + f"'{self._db_format(self._name_section)}', " +
f"{self._reach}, " + f"{self._reach}, " +
f"{self._start_rk}, " + f"{self._start_rk}, {self._end_rk}, " +
f"{self._end_rk}, " +
f"{self._concentration}, " + f"{self._concentration}, " +
f"{self._eg}, " + f"{self._eg}, {self._em}, {self._ed}, " +
f"{self._em}, " + f"{self._rate}, {self._enabled}, " +
f"{self._ed}, " + f"{self._status.scenario_id}" +
f"{self._rate}, " +
f"{self._enabled}" +
")" ")"
) )
execute(sql) execute(sql)