Skip to content

Commit

Permalink
added id property to compute packages to enable tracking active easier
Browse files Browse the repository at this point in the history
  • Loading branch information
niklastheman committed Dec 5, 2023
1 parent 4f7c256 commit a986fda
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 27 deletions.
65 changes: 40 additions & 25 deletions fedn/fedn/network/api/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def get_compute_package(self):
)

obj = {
"id": result["_id"].__str__(),
"id": result["id"] if "id" in result else "",
"file_name": result["file_name"],
"helper": result["helper"],
"committed_at": result["committed_at"],
Expand All @@ -324,28 +324,19 @@ def get_compute_package(self):

return jsonify(obj)

def list_compute_packages(self, limit: str = None, skip: str = None):
def list_compute_packages(self, limit: str = None, skip: str = None, include_active: str = None):
"""Get paginated list of compute packages from the statestore.
:return: All compute packages as a json response.
:rtype: :class:`flask.Response`
"""

if limit is None:
return (
jsonify(
{
"success": False,
"message": "No limit provided.",
}
),
404,
)

if limit is not None and skip is not None:
limit = int(limit)
skip = int(skip)

include_active: bool = include_active == "true"

result = self.statestore.list_compute_packages(limit, skip)
if result is None:
return (
Expand All @@ -354,18 +345,42 @@ def list_compute_packages(self, limit: str = None, skip: str = None):
),
404,
)
arr = []
for element in result["result"]:
obj = {
"id": element["_id"].__str__(),
"file_name": element["file_name"],
"helper": element["helper"],
"committed_at": element["committed_at"],
"storage_file_name": element["storage_file_name"] if "storage_file_name" in element else "",
"name": element["name"] if "name" in element else "",
"description": element["description"] if "description" in element else "",
}
arr.append(obj)

active_package_id: str = None

if include_active:
active_package = self.statestore.get_compute_package()

if active_package is not None:
active_package_id = active_package["id"] if "id" in active_package else ""

if include_active:
arr = [
{
"id": element["id"] if "id" in element else "",
"file_name": element["file_name"],
"helper": element["helper"],
"committed_at": element["committed_at"],
"storage_file_name": element["storage_file_name"] if "storage_file_name" in element else "",
"name": element["name"] if "name" in element else "",
"description": element["description"] if "description" in element else "",
"active": "id" in element and element["id"] == active_package_id,
}
for element in result["result"]
]
else:
arr = [
{
"id": element["id"] if "id" in element else "",
"file_name": element["file_name"],
"helper": element["helper"],
"committed_at": element["committed_at"],
"storage_file_name": element["storage_file_name"] if "storage_file_name" in element else "",
"name": element["name"] if "name" in element else "",
"description": element["description"] if "description" in element else "",
}
for element in result["result"]
]

result = {"result": arr, "count": result["count"]}
return jsonify(result)
Expand Down
2 changes: 2 additions & 0 deletions fedn/fedn/network/api/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,12 @@ def list_compute_packages():

limit = request.args.get("limit", None)
skip = request.args.get("skip", None)
include_active = request.args.get("include_active", None)

return api.list_compute_packages(
limit=limit,
skip=skip,
include_active=include_active
)


Expand Down
6 changes: 4 additions & 2 deletions fedn/fedn/network/statestore/mongostatestore.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
import uuid
from datetime import datetime

import pymongo
Expand Down Expand Up @@ -285,6 +286,7 @@ def set_compute_package(self, file_name: str, storage_file_name: str, helper_typ
"committed_at": datetime.now(),
"name": name,
"description": description,
"id": str(uuid.uuid4()),
}

self.control.package.update_one(
Expand All @@ -310,7 +312,7 @@ def get_compute_package(self):
try:

find = {"key": "active"}
projection = {"key": False}
projection = {"key": False, "_id": False}
ret = self.control.package.find_one(find, projection)
return ret
except Exception as e:
Expand All @@ -336,7 +338,7 @@ def list_compute_packages(self, limit: int = None, skip: int = None, sort_key="c
count = None

find_option = {"key": "package_trail"}
projection = {"key": False}
projection = {"key": False, "_id": False}

try:
if limit is not None and skip is not None:
Expand Down

0 comments on commit a986fda

Please sign in to comment.