Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/SK-1106 | Added GET "/" and POST "/list" endpoints to prediction_routes #730

Merged
merged 24 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
3d92b77
statestore refactor + ModelInference
Wrede Oct 2, 2024
659fe8b
fix missing updated_at key
Wrede Oct 3, 2024
eb74947
fix floating imports
Wrede Oct 3, 2024
4fb372e
fix client_store objects missing combiner name
Wrede Oct 3, 2024
92b07bd
fix
Wrede Oct 3, 2024
3450880
fix
Wrede Oct 3, 2024
db425d6
fix inconsistant naming of collections
Wrede Oct 4, 2024
399f85e
Replaced 'inference' with 'prediction'
benjaminastrand Oct 9, 2024
eac749a
Replace "inference" with "prediction" across entire repo (except Stat…
benjaminastrand Oct 10, 2024
a699720
Regenerated protos
benjaminastrand Oct 10, 2024
07eb5ad
fix
Wrede Oct 11, 2024
185dafe
fix if no models have been created
Wrede Oct 14, 2024
ca95237
fix multiple comibiners in statestore
Wrede Oct 14, 2024
df9d5f7
Updates StatusType and ClientStateToString
benjaminastrand Oct 14, 2024
197dd96
add logging for when client disconnect from TaskStream
Wrede Oct 16, 2024
339787c
Merge branch 'master' into feature/SK-1081
Wrede Oct 16, 2024
c45477b
test server keepalive settings
Wrede Oct 17, 2024
2bd2cac
add keepalive to client
Wrede Oct 18, 2024
a8aeeac
Fix session_id -> prediction_id
benjaminastrand Oct 21, 2024
d2c0b3e
Added GET "/" and POST "/list" endpoints
benjaminastrand Oct 21, 2024
0a52a19
Added docs for GET "/" and POST "/list" endpoints
benjaminastrand Oct 21, 2024
08ec754
Merge branch 'master' into feature/SK-1106
benjaminastrand Oct 23, 2024
fd53fed
Fix linting
benjaminastrand Oct 23, 2024
41399ea
Fix exception
benjaminastrand Oct 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
233 changes: 232 additions & 1 deletion fedn/network/api/v1/prediction_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from fedn.network.api.auth import jwt_auth_required
from fedn.network.api.shared import control
from fedn.network.api.v1.shared import api_version, mdb
from fedn.network.api.v1.shared import api_version, mdb, get_typed_list_headers, get_post_data_to_kwargs
from fedn.network.storage.statestore.stores.model_store import ModelStore
from fedn.network.storage.statestore.stores.prediction_store import PredictionStore
from fedn.network.storage.statestore.stores.shared import EntityNotFound
Expand Down Expand Up @@ -49,3 +49,234 @@ def start_session():
return jsonify({"message": "Prediction session started"}), 200
except Exception:
return jsonify({"message": "Failed to start prediction session"}), 500


@bp.route("/", methods=["GET"])
@jwt_auth_required(role="admin")
def get_predictions():
"""Get predictions
Retrieves a list of predictions based on the provided parameters.
By specifying a parameter in the url, you can filter the predictions based on that parameter,
and the response will contain only the predictions that match the filter.
---
tags:
- Predictions
parameters:
- name: sender.name
in: query
required: false
type: string
description: Name of the sender
- name: sender.role
in: query
required: false
type: string
description: Role of the sender
- name: receiver.name
in: query
required: false
type: string
description: Name of the receiver
- name: receiver.role
in: query
required: false
type: string
description: Role of the receiver
- name: prediction_id
in: query
required: false
type: string
description: Prediction id of the prediction
- name: model_id
in: query
required: false
type: string
- name: correlation_id
in: query
required: false
type: string
description: Correlation id of the prediction
- name: X-Limit
in: header
required: false
type: integer
description: The maximum number of predictions to retrieve
- name: X-Skip
in: header
required: false
type: integer
description: The number of predictions to skip
- name: X-Sort-Key
in: header
required: false
type: string
description: The key to sort the predictions by
- name: X-Sort-Order
in: header
required: false
type: string
description: The order to sort the predictions in ('asc' or 'desc')
definitions:
Prediction:
type: object
properties:
id:
type: string
correlation_id:
type: string
prediction_id:
type: string
model_id:
type: string
timestamp:
type: object
format: date-time
data:
type: string
meta:
type: string
sender:
type: object
properties:
name:
type: string
role:
type: string
receiver:
type: object
properties:
name:
type: string
role:
type: string
responses:
200:
description: A list of predictions and the total count.
schema:
type: object
properties:
count:
type: integer
result:
type: array
items:
$ref: '#/definitions/Prediction'
500:
description: An error occurred
schema:
type: object
properties:
message:
type: string
"""
try:
limit, skip, sort_key, sort_order, use_typing = get_typed_list_headers(request.headers)
kwargs = request.args.to_dict()

predictions = prediction_store.list(limit, skip, sort_key, sort_order, use_typing=use_typing, **kwargs)

result = [prediction.__dict__ for prediction in predictions["result"]] if use_typing else predictions["result"]

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

return jsonify(response), 200
except Exception:
return jsonify({"message": "An unexpected error occurred"}), 500


@bp.route("/list", methods=["POST"])
@jwt_auth_required(role="admin")
def list_predictions():
"""List predictions
Retrieves a list of predictions based on the provided parameters.
Works much like the GET predictions method, but allows for a more complex query.
By specifying a parameter in the body, you can filter the predictions based on that parameter,
and the response will contain only the predictions that match the filter. If the parameter value contains a comma,
the filter will be an "in" query, meaning that the predictions will be returned if the specified field contains any of the values in the parameter.
---
tags:
- Predictions
parameters:
- name: prediction
in: body
required: false
schema:
type: object
properties:
sender.name:
type: string
description: Name of the sender
sender.role:
required: false
type: string
description: Role of the sender
receiver.name:
type: string
description: Name of the receiver
receiver.role:
required: false
type: string
description: Role of the receiver
prediction_id:
required: false
type: string
model_id:
required: false
type: string
correlation_id:
required: false
type: string
description: Correlation id of the status
- name: X-Limit
in: header
required: false
type: integer
description: The maximum number of predictions to retrieve
- name: X-Skip
in: header
required: false
type: integer
description: The number of predictions to skip
- name: X-Sort-Key
in: header
required: false
type: string
description: The key to sort the predictions by
- name: X-Sort-Order
in: header
required: false
type: string
description: The order to sort the predictions in ('asc' or 'desc')
responses:
200:
description: A list of predictions and the total count.
schema:
type: object
properties:
count:
type: integer
result:
type: array
items:
$ref: '#/definitions/Prediction'
500:
description: An error occurred
schema:
type: object
properties:
message:
type: string
"""
try:
limit, skip, sort_key, sort_order, use_typing = get_typed_list_headers(request.headers)
kwargs = get_post_data_to_kwargs(request)

predictions = prediction_store.list(limit, skip, sort_key, sort_order, use_typing=use_typing, **kwargs)

result = [prediction.__dict__ for prediction in predictions["result"]] if use_typing else predictions["result"]

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

return jsonify(response), 200
except Exception:
return jsonify({"message": "An unexpected error occurred"}), 500
2 changes: 1 addition & 1 deletion fedn/network/storage/statestore/stores/prediction_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def from_dict(data: dict) -> "Prediction":
data=data["data"] if "data" in data else None,
correlation_id=data["correlationId"] if "correlationId" in data else None,
timestamp=data["timestamp"] if "timestamp" in data else None,
session_id=data["sessionId"] if "sessionId" in data else None,
prediction_id=data["predictionId"] if "predictionId" in data else None,
meta=data["meta"] if "meta" in data else None,
sender=data["sender"] if "sender" in data else None,
receiver=data["receiver"] if "receiver" in data else None,
Expand Down
Loading