Skip to content

Commit

Permalink
Fix adding scenario alternatives
Browse files Browse the repository at this point in the history
Scenario dock in DB editor didn't work very well due to DB locking
issues.

Re #3041
  • Loading branch information
soininen committed Jan 31, 2025
1 parent 97b6b56 commit c56e55e
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions spinetoolbox/spine_db_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,9 +985,11 @@ def get_parameter_value_list(self, db_map, id_, role=Qt.ItemDataRole.DisplayRole
]

def get_scenario_alternative_id_list(self, db_map, scen_id):
with self._db_locks[db_map]:
scen = self.get_item(db_map, "scenario", scen_id)
return scen["alternative_id_list"] if scen else []
if db_map in self._db_locks:
with self._db_locks[db_map]:
scen = self.get_item(db_map, "scenario", scen_id)
return scen["alternative_id_list"] if scen else []
return []

Check warning on line 992 in spinetoolbox/spine_db_manager.py

View check run for this annotation

Codecov / codecov/patch

spinetoolbox/spine_db_manager.py#L992

Added line #L992 was not covered by tests

def import_data(self, db_map_data, command_text="Import data"):
"""Imports the given data into given db maps using the dedicated import functions from spinedb_api.
Expand Down Expand Up @@ -1312,8 +1314,7 @@ def set_scenario_alternatives(self, db_map_data):
if any(db_map_error_log.values()):
self.error_msg.emit(db_map_error_log)

@staticmethod
def get_data_to_set_scenario_alternatives(db_map, scenarios):
def get_data_to_set_scenario_alternatives(self, db_map, scenarios):
"""Returns data to add and remove, in order to set wide scenario alternatives.
Args:
Expand All @@ -1335,23 +1336,28 @@ def _is_equal(to_add, to_rm):
scen_alts_to_add = []
scen_alt_ids_to_remove = {}
errors = []
for scen in scenarios:
current_scen = db_map.get_item("scenario", id=scen["id"])
if current_scen is None:
error = f"no scenario matching {scen} to set alternatives for"
errors.append(error)
continue
for k, alternative_id in enumerate(scen.get("alternative_id_list", ())):
item_to_add = {"scenario_id": current_scen["id"], "alternative_id": alternative_id, "rank": k + 1}
scen_alts_to_add.append(item_to_add)
for k, alternative_name in enumerate(scen.get("alternative_name_list", ())):
item_to_add = {"scenario_id": current_scen["id"], "alternative_name": alternative_name, "rank": k + 1}
scen_alts_to_add.append(item_to_add)
for alternative_name in current_scen["alternative_name_list"]:
current_scen_alt = db_map.get_item(
"scenario_alternative", scenario_name=current_scen["name"], alternative_name=alternative_name
)
scen_alt_ids_to_remove[current_scen_alt["id"]] = current_scen_alt
with self._db_locks[db_map]:
for scen in scenarios:
current_scen = db_map.get_item("scenario", id=scen["id"])
if current_scen is None:
error = f"no scenario matching {scen} to set alternatives for"
errors.append(error)
continue

Check warning on line 1345 in spinetoolbox/spine_db_manager.py

View check run for this annotation

Codecov / codecov/patch

spinetoolbox/spine_db_manager.py#L1343-L1345

Added lines #L1343 - L1345 were not covered by tests
for k, alternative_id in enumerate(scen.get("alternative_id_list", ())):
item_to_add = {"scenario_id": current_scen["id"], "alternative_id": alternative_id, "rank": k + 1}
scen_alts_to_add.append(item_to_add)
for k, alternative_name in enumerate(scen.get("alternative_name_list", ())):
item_to_add = {
"scenario_id": current_scen["id"],
"alternative_name": alternative_name,
"rank": k + 1,
}
scen_alts_to_add.append(item_to_add)
for alternative_name in current_scen["alternative_name_list"]:
current_scen_alt = db_map.get_item(
"scenario_alternative", scenario_name=current_scen["name"], alternative_name=alternative_name
)
scen_alt_ids_to_remove[current_scen_alt["id"]] = current_scen_alt
# Remove items that are both to add and to remove
for id_, to_rm in list(scen_alt_ids_to_remove.items()):
i = next((i for i, to_add in enumerate(scen_alts_to_add) if _is_equal(to_add, to_rm)), None)
Expand Down

0 comments on commit c56e55e

Please sign in to comment.