From edf9f575f924ff6af781edec0e35fdad52ffe334 Mon Sep 17 00:00:00 2001 From: Niklas Date: Tue, 17 Oct 2023 13:25:41 +0200 Subject: [PATCH] get_sessions pagination v1 --- fedn/fedn/network/api/interface.py | 10 +++++++--- fedn/fedn/network/statestore/mongostatestore.py | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/fedn/fedn/network/api/interface.py b/fedn/fedn/network/api/interface.py index f886261d0..a86f0feb8 100644 --- a/fedn/fedn/network/api/interface.py +++ b/fedn/fedn/network/api/interface.py @@ -165,17 +165,21 @@ def get_all_sessions(self, limit=None, skip=None): :return: All sessions as a json response. :rtype: :class:`flask.Response` """ - sessions_objects = self.statestore.get_sessions(limit, skip) - if sessions_objects is None: + sessions_object = self.statestore.get_sessions(limit, skip) + if sessions_object is None: return ( jsonify({"success": False, "message": "No sessions found."}), 404, ) payload = {} - for object in sessions_objects: + for object in sessions_object["result"]: id = object["session_id"] info = object["session_config"][0] payload[id] = info + + result = {"result": payload, "count": sessions_object["count"]} + + return jsonify(result) return jsonify(payload) def get_session(self, session_id): diff --git a/fedn/fedn/network/statestore/mongostatestore.py b/fedn/fedn/network/statestore/mongostatestore.py index 113802a89..903d1a208 100644 --- a/fedn/fedn/network/statestore/mongostatestore.py +++ b/fedn/fedn/network/statestore/mongostatestore.py @@ -118,10 +118,22 @@ def get_sessions(self, limit=None, skip=None): :rtype: ObjectID """ + result = None + if limit is not None and skip is not None: - return self.sessions.find().limit(limit).skip(skip) + limit = int(limit) + skip = int(skip) + + result = self.sessions.find().limit(limit).skip(skip) + else: + result = self.sessions.find() - return self.sessions.find() + count = self.sessions.count_documents({}) + + return { + "result": result, + "count": count, + } def get_session(self, session_id): """Get session with id.