Skip to content

Commit

Permalink
Feature/SK-916 | Get active model id (#645)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklastheman authored Jun 24, 2024
1 parent 308d66a commit 81576fa
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
35 changes: 35 additions & 0 deletions fedn/network/api/v1/model_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ def get_ancestors(id: str):


@bp.route("/<string:id>/download", methods=["GET"])
@jwt_auth_required(role="admin")
def download(id: str):
"""Download
Downloads the model file of the provided id.
Expand Down Expand Up @@ -646,6 +647,7 @@ def download(id: str):


@bp.route("/<string:id>/parameters", methods=["GET"])
@jwt_auth_required(role="admin")
def get_parameters(id: str):
"""Download
Downloads parameters of the model of the provided id.
Expand Down Expand Up @@ -707,3 +709,36 @@ def get_parameters(id: str):
return jsonify({"message": f"Entity with id: {id} not found"}), 404
except Exception:
return jsonify({"message": "An unexpected error occurred"}), 500


@bp.route("/active", methods=["GET"])
@jwt_auth_required(role="admin")
def get_active_model():
"""Get active model
Retrieves the active model (id).
---
tags:
- Models
responses:
200:
description: The active model id
schema:
type: string
500:
description: An error occurred
schema:
type: object
properties:
message:
type: string
"""
try:
active_model = model_store.get_active()

response = active_model

return jsonify(response), 200
except EntityNotFound:
return jsonify({"message": "No active model found"}), 404
except Exception:
return jsonify({"message": "An unexpected error occurred"}), 500
11 changes: 11 additions & 0 deletions fedn/network/storage/statestore/stores/model_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,14 @@ def count(self, **kwargs) -> int:
"""
kwargs["key"] = "models"
return super().count(**kwargs)

def get_active(self) -> str:
"""Get the active model
return: The active model id (str)
"""
active_model = self.database[self.collection].find_one({"key": "current_model"})

if active_model is None:
raise EntityNotFound("Active model not found")

return active_model["model"]

0 comments on commit 81576fa

Please sign in to comment.