diff --git a/fedn/fedn/network/api/interface.py b/fedn/fedn/network/api/interface.py index 025cddecd..32a9c00ea 100644 --- a/fedn/fedn/network/api/interface.py +++ b/fedn/fedn/network/api/interface.py @@ -208,6 +208,23 @@ def get_session(self, session_id): payload[id] = info return jsonify(payload) + def set_active_compute_package(self, id: str): + + success = self.statestore.set_active_compute_package(id) + + if not success: + return ( + jsonify( + { + "success": False, + "message": "Failed to set compute package.", + } + ), + 400, + ) + + return jsonify({"success": True, "message": "Compute package set."}) + def set_compute_package(self, file, helper_type: str, name: str = None, description: str = None): """Set the compute package in the statestore. diff --git a/fedn/fedn/network/api/server.py b/fedn/fedn/network/api/server.py index 90ff019f4..cc8d1b4a9 100644 --- a/fedn/fedn/network/api/server.py +++ b/fedn/fedn/network/api/server.py @@ -182,6 +182,12 @@ def get_session(): return api.get_session(session_id) +@app.route("/set_active_package", methods=["PUT"]) +def set_active_package(): + id = request.args.get("id", None) + return api.set_active_compute_package(id) + + @app.route("/set_package", methods=["POST"]) def set_package(): """ Set the compute package in the statestore. diff --git a/fedn/fedn/network/statestore/mongostatestore.py b/fedn/fedn/network/statestore/mongostatestore.py index 6edca376c..76a23b8fb 100644 --- a/fedn/fedn/network/statestore/mongostatestore.py +++ b/fedn/fedn/network/statestore/mongostatestore.py @@ -60,8 +60,12 @@ def __init__(self, network_id, config, model_storage_config): # Storage settings self.set_storage_backend(model_storage_config) + self.init_index() self.__inited = True + def init_index(self): + self.package.create_index([("id", pymongo.DESCENDING)]) + def is_inited(self): """Check if the statestore is intialized. @@ -270,6 +274,37 @@ def get_validations(self, **kwargs): result = self.control.validations.find(kwargs) return result + def set_active_compute_package(self, id: str): + """Set the active compute package in statestore. + + :param id: The id of the compute package (not document _id). + :type id: str + :return: True if successful. + :rtype: bool + """ + + try: + + find = {"id": id} + projection = {"_id": False, "key": False} + + doc = self.control.package.find_one(find, projection) + + if doc is None: + return False + + doc["key"] = "active" + + self.control.package.replace_one( + {"key": "active"}, doc + ) + + except Exception as e: + print("ERROR: {}".format(e), flush=True) + return False + + return True + def set_compute_package(self, file_name: str, storage_file_name: str, helper_type: str, name: str = None, description: str = None): """Set the active compute package in statestore. @@ -353,8 +388,8 @@ def list_compute_packages(self, limit: int = None, skip: int = None, sort_key="c return None return { - "result": result, - "count": count, + "result": result or [], + "count": count or 0, } def set_helper(self, helper):