Skip to content

Commit

Permalink
feat(hub): add pp and create containers EP
Browse files Browse the repository at this point in the history
  • Loading branch information
brucetony committed Mar 18, 2024
1 parent ee840ed commit 3f150dd
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 53 deletions.
11 changes: 11 additions & 0 deletions gateway/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,14 @@

CONTENT_TYPE = "Content-Type"
CONTENT_LENGTH = "Content-Length"

# Map Hub responses to what the old FLAME UI expects
analysis_container_status_map = {
"running": "running",
"starting": "running",
"started": "created",
"stopping": "running",
"stopped": "exited",
"finished": "exited",
"failed": "exited",
}
9 changes: 8 additions & 1 deletion gateway/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from httpx import ConnectError, DecodingError
from starlette.responses import Response

from gateway import post_processing
from gateway.constants import CONTENT_TYPE
# from gateway.models import GatewayFormData
from gateway.utils import unzip_form_params, unzip_body_object, create_request_data, unzip_query_params


Expand Down Expand Up @@ -89,6 +89,7 @@ def route(
dependencies: Sequence[params.Depends] | None = None,
summary: str | None = None,
description: str | None = None,
post_processing_func: str | None = None,
# params from fastapi http methods can be added here later and then added to `request_method()`
):
"""A decorator for the FastAPI router, its purpose is to make FastAPI
Expand Down Expand Up @@ -120,6 +121,8 @@ def route(
Summary of the method (usually short).
description: str | None
Longer explanation of the method.
post_processing_func: str | None
Method from the post_processing module to apply to the response. E.g. parse_something
Returns
Expand Down Expand Up @@ -203,6 +206,10 @@ async def inner(request: Request, response: Response, **kwargs):

response.status_code = status_code_from_service

if post_processing_func:
f = getattr(post_processing, post_processing_func)
resp_data = f(resp_data)

return resp_data

return wrapper
6 changes: 3 additions & 3 deletions gateway/models/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class ContainerData(BaseModel):
id: UUID
name: str
job_id: UUID
image: UUID
state: str
status: str
image: UUID | None = None # TODO remove null allowance
state: str | None = None
status: str | None = None
next_tag: str
repo: str
train_class_id: str
Expand Down
25 changes: 25 additions & 0 deletions gateway/post_processing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Post-processing methods for passing returned responses to frontend."""


def parse_containers(analysis_response: dict) -> dict:
"""Parse analysis response from the hub."""
containers = []
for hit in analysis_response["data"]:
data = {
"id": hit["id"], # id
"name": hit["analysis"]["name"],
"job_id": hit["analysis"]["id"],
"image": hit["analysis"]["master_image_id"],
"state": hit["run_status"],
"status": hit["analysis"]["result_status"],
"next_tag": "Köln", # TODO remove/replace
"repo": "/data",
"train_class_id": "choochoo",
}
containers.append(data)

return {"containers": containers}


def test_print():
print("test")
35 changes: 7 additions & 28 deletions gateway/routers/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from gateway.auth import hub_oauth2_scheme
from gateway.conf import gateway_settings
from gateway.core import route
from gateway.models.hub import Project, AllProjects, ApprovalStatus, AnalysisOrProjectNode, ListAnalysisNodes, \
ListAnalysisOrProjectNodes, AnalysisNode
from gateway.models.hub import Project, AllProjects, ApprovalStatus, AnalysisOrProjectNode, ListAnalysisOrProjectNodes, \
AnalysisNode
from gateway.models.k8s import ImageDataResponse, ContainerResponse

hub_router = APIRouter(
Expand Down Expand Up @@ -49,29 +49,6 @@ async def get_images():
return dummy_data


@hub_router.get("/hub/containers", response_model=ContainerResponse)
async def get_containers():
"""Return list of containers for the frontend."""
# TODO: replace with data from https://api.privateaim.net/analysis-nodes
# TODO: add project specific call / filter?
dummy_data = {
"containers": [
{
"id": "d730b955-c476-40db-9dd1-5ea6b1cfe5bc",
"name": "FooBar",
"job_id": "4c0e4a1a-795b-4a23-a7ef-0a2473bcb670",
"image": "4a941577-46ce-4220-8ca0-181cf45abe29",
"state": "Running",
"status": "Active",
"next_tag": "Köln",
"repo": "/data",
"train_class_id": "choochoo",
}
]
}
return dummy_data


@hub_router.get("/hub/vault/status")
async def get_vault_status():
"""Spoof vault status."""
Expand Down Expand Up @@ -201,9 +178,11 @@ async def accept_reject_project_node(
path="/analysis-nodes",
status_code=status.HTTP_200_OK,
service_url=gateway_settings.HUB_SERVICE_URL,
response_model=ListAnalysisNodes,
# response_model=ListAnalysisNodes,
response_model=ContainerResponse,
query_params=["filter_id", "filter_project_id", "filter_project_realm_id",
"filter_node_id", "filter_node_realm_id", "include"],
post_processing_func="parse_containers", # Create new EP for getting containers
)
async def list_analyses_of_nodes(
request: Request,
Expand All @@ -212,9 +191,9 @@ async def list_analyses_of_nodes(
str | None,
Query(
description="Whether to include additional data for the given parameter. Can only be 'node' or null",
pattern="^node$", # Must be "node",
pattern="^(node|analysis)$", # Must be "node",
),
] = None,
] = "analysis",
filter_id: Annotated[
uuid.UUID | None,
Query(
Expand Down
47 changes: 26 additions & 21 deletions gateway/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
from starlette import status
from starlette.middleware.cors import CORSMiddleware

from gateway.auth import realm_idp_settings
from gateway.models import HealthCheck
from gateway.routers.hub import hub_router
from gateway.routers.k8s import k8s_router
from gateway.routers.kong import kong_router
from gateway.routers.metadata import metadata_router
from gateway.routers.results import results_router

# API metadata
tags_metadata = [
Expand All @@ -20,12 +25,12 @@
openapi_tags=tags_metadata,
title="FLAME API",
description="Test API for FLAME project",
# swagger_ui_init_oauth={
# "usePkceWithAuthorizationCodeGrant": True,
# # Auth fill client ID for the docs with the below value
# "clientId": realm_idp_settings.client_id, # default client-id is Keycloak
# "clientSecret": realm_idp_settings.client_secret,
# },
swagger_ui_init_oauth={
"usePkceWithAuthorizationCodeGrant": True,
# Auth fill client ID for the docs with the below value
"clientId": realm_idp_settings.client_id, # default client-id is Keycloak
"clientSecret": realm_idp_settings.client_secret,
},
)

app.add_middleware(
Expand Down Expand Up @@ -59,21 +64,21 @@ def get_health() -> HealthCheck:
return HealthCheck(status="OK")


# app.include_router(
# k8s_router,
# )
#
# app.include_router(
# results_router,
# )
#
# app.include_router(
# metadata_router,
# )
#
# app.include_router(
# hub_router,
# )
app.include_router(
k8s_router,
)

app.include_router(
results_router,
)

app.include_router(
metadata_router,
)

app.include_router(
hub_router,
)

app.include_router(
kong_router,
Expand Down

0 comments on commit 3f150dd

Please sign in to comment.