From 53794eac73cf8029569811a17e254b6f28646bcc Mon Sep 17 00:00:00 2001 From: moririn2528 Date: Tue, 12 Sep 2023 18:05:51 +0900 Subject: [PATCH] fix by review --- optuna_dashboard/_app.py | 6 +++--- optuna_dashboard/_preferential_history.py | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/optuna_dashboard/_app.py b/optuna_dashboard/_app.py index a020c8b8..0249de48 100644 --- a/optuna_dashboard/_app.py +++ b/optuna_dashboard/_app.py @@ -28,8 +28,8 @@ from ._cached_extra_study_property import get_cached_extra_study_property from ._custom_plot_data import get_plotly_graph_objects from ._importance import get_param_importance_from_trials_cache from ._pareto_front import get_pareto_front_trials -from ._preferential_history import HistoryIdError from ._preferential_history import NewHistory +from ._preferential_history import PreferenceHistoryNotFound from ._preferential_history import remove_history from ._preferential_history import report_history from ._preferential_history import restore_history @@ -314,7 +314,7 @@ def create_app( def remove_preference(study_id: int, history_id: str) -> dict[str, Any]: try: remove_history(study_id, storage, history_id) - except HistoryIdError: + except PreferenceHistoryNotFound: response.status = 404 return {"reason": f"history_id={history_id} is not found"} @@ -326,7 +326,7 @@ def create_app( def restore_preference(study_id: int, history_id: str) -> dict[str, Any]: try: restore_history(study_id, storage, history_id) - except HistoryIdError: + except PreferenceHistoryNotFound: response.status = 404 return {"reason": f"history_id={history_id} is not found"} diff --git a/optuna_dashboard/_preferential_history.py b/optuna_dashboard/_preferential_history.py index 9726d27c..ef3f87f4 100644 --- a/optuna_dashboard/_preferential_history.py +++ b/optuna_dashboard/_preferential_history.py @@ -39,7 +39,7 @@ if TYPE_CHECKING: ) -class HistoryIdError(Exception): +class PreferenceHistoryNotFound(Exception): pass @@ -66,7 +66,7 @@ def report_history( else: assert False, f"Unknown data: {input_data}" - id = report_preferences( + preference_id = report_preferences( study_id=study_id, storage=storage, preferences=preferences, @@ -75,27 +75,27 @@ def report_history( if input_data.mode == "ChooseWorst": history: ChooseWorstHistory = { "mode": "ChooseWorst", - "id": id, + "id": preference_id, "timestamp": datetime.now().isoformat(), "candidates": input_data.candidates, "clicked": input_data.clicked, "preferences": preferences, } - key = _SYSTEM_ATTR_PREFIX_HISTORY + id + key = _SYSTEM_ATTR_PREFIX_HISTORY + preference_id storage.set_study_system_attr( study_id=study_id, key=key, value=json.dumps(history), ) - return id + return preference_id def remove_history(study_id: int, storage: BaseStorage, history_id: str) -> None: system_attrs = storage.get_study_system_attrs(study_id) history_key = _SYSTEM_ATTR_PREFIX_HISTORY + history_id if history_key not in system_attrs: - raise HistoryIdError + raise PreferenceHistoryNotFound storage.set_study_system_attr(study_id, _SYSTEM_ATTR_PREFIX_PREFERENCE + history_id, []) @@ -103,7 +103,7 @@ def restore_history(study_id: int, storage: BaseStorage, history_id: str) -> Non system_attrs = storage.get_study_system_attrs(study_id) history_key = _SYSTEM_ATTR_PREFIX_HISTORY + history_id if history_key not in system_attrs: - raise HistoryIdError + raise PreferenceHistoryNotFound history: History = json.loads(system_attrs.get(history_key, "")) storage.set_study_system_attr( study_id, _SYSTEM_ATTR_PREFIX_PREFERENCE + history_id, history["preferences"]