Skip to content

Commit

Permalink
group clients by combiner name and count them
Browse files Browse the repository at this point in the history
  • Loading branch information
niklastheman committed Oct 27, 2023
1 parent 6f20a66 commit 384a4fd
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
25 changes: 25 additions & 0 deletions fedn/fedn/network/api/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,31 @@ def get_plot_data(self, feature=None):
}

return jsonify(result)

def list_combiners_data(self, limit=None, skip=None):
"""Get combiners data.
"""

response = self.statestore.list_combiners_data(limit, skip)

arr = []

## order list by combiner name

for element in response["result"]:

obj = {
"combiner": element["_id"],
"count": element["count"],
}

arr.append(obj)

arr.sort(key=lambda x: x["count"], reverse=True)

result = {"result": arr, "count": response["count"]}

return jsonify(result)

def start_session(
self,
Expand Down
16 changes: 16 additions & 0 deletions fedn/fedn/network/api/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,22 @@ def add_client():
return response


@app.route("/list_combiners_data", methods=["GET"])
def list_combiners_data():
"""Add a client to the network.
return: The response from control.
rtype: json
"""
limit = request.args.get("limit", None)
skip = request.args.get("skip", None)

try:
response = api.list_combiners_data(limit=limit, skip=skip)
except TypeError as e:
return jsonify({"success": False, "message": str(e)}), 400
return response


@app.route("/get_plot_data", methods=["GET"])
def get_plot_data():
"""Get plot data from the statestore.
Expand Down
36 changes: 36 additions & 0 deletions fedn/fedn/network/statestore/mongostatestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,42 @@ def list_clients(self, limit=None, skip=None, status=False, sort_key="last_seen"
"count": count,
}

def list_combiners_data(self, limit=None, skip=None):
"""List all combiner data.
:return: list of combiner data.
:rtype: list(ObjectId)
"""

result = None
count = None

pipeline = [
{"$group": {"_id": "$combiner", "count": {"$sum": 1}}}
]

if limit is not None and skip is not None:
limit = int(limit)
skip = int(skip)
pipeline.append({"$limit": limit})
pipeline.append({"$skip": skip})

result = self.clients.aggregate(pipeline)

pipeline_count = [
{"$group": {"_id": "$combiner"}},
{"$group": {"_id": None, "count": {"$sum": 1}}}
]

result_count = list(self.clients.aggregate(pipeline_count))

count = result_count[0]['count']

return {
"result": result,
"count": count,
}

def update_client_status(self, client_data, status, role):
"""Set or update client status.
Expand Down

0 comments on commit 384a4fd

Please sign in to comment.