Skip to content

Commit

Permalink
set active compute package
Browse files Browse the repository at this point in the history
  • Loading branch information
niklastheman committed Dec 6, 2023
1 parent a986fda commit 4d46ac7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
17 changes: 17 additions & 0 deletions fedn/fedn/network/api/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions fedn/fedn/network/api/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
39 changes: 37 additions & 2 deletions fedn/fedn/network/statestore/mongostatestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 4d46ac7

Please sign in to comment.