Skip to content

Commit

Permalink
wrapper function for query not found
Browse files Browse the repository at this point in the history
  • Loading branch information
eriktaubeneck committed Jul 17, 2024
1 parent 73c9ad1 commit 2fd072a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
16 changes: 16 additions & 0 deletions sidecar/app/routes/http_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typing import Type

from fastapi import HTTPException

from ..query.base import Query, QueryManager


def get_query_from_query_id(
query_manager: QueryManager,
query_cls: Type[Query],
query_id: str,
) -> Query:
query = query_manager.get_from_query_id(query_cls, query_id)
if query is None:
raise HTTPException(status_code=404, detail=f"Query<{query_id}> not found")
return query
19 changes: 8 additions & 11 deletions sidecar/app/routes/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from ..query.demo_logger import DemoLoggerQuery
from ..query.ipa import GateType, IPACoordinatorQuery, IPAHelperQuery
from ..settings import get_settings
from .http_helpers import get_query_from_query_id

router = APIRouter(
prefix="/start",
Expand Down Expand Up @@ -79,7 +80,9 @@ def start_ipa_helper(
settings = get_settings()
role = settings.role
if not role or role == role.COORDINATOR:
raise IncorrectRoleError("Cannot start helper without helper role.")
raise IncorrectRoleError(
f"Cannot start helper without helper role. Currently running {role=}."
)

compiled_id = (
f"{commit_hash}_{gate_type}"
Expand Down Expand Up @@ -112,10 +115,7 @@ def get_ipa_helper_status(
query_id: str,
request: Request,
):
query_manager = request.app.state.QUERY_MANAGER
query = query_manager.get_from_query_id(Query, query_id)
if query is None:
raise HTTPException(status_code=404, detail="Query not found")
query = get_query_from_query_id(request.app.state.QUERY_MANAGER, Query, query_id)
return {"status": query.status.name}


Expand All @@ -124,11 +124,7 @@ def get_ipa_helper_log_file(
query_id: str,
request: Request,
):
query_manager = request.app.state.QUERY_MANAGER
query = query_manager.get_from_query_id(Query, query_id)
if query is None:
raise HTTPException(status_code=404, detail="Query not found")

query = get_query_from_query_id(request.app.state.QUERY_MANAGER, Query, query_id)
settings = get_settings()

def iterfile():
Expand Down Expand Up @@ -175,7 +171,8 @@ def start_ipa_query(
role = settings.role
if role != role.COORDINATOR:
raise IncorrectRoleError(
f"Sidecar {role}: Cannot start query without coordinator role."
f"Attempting to start query with {role=}: "
"Cannot start query without coordinator role."
)

paths = Paths(
Expand Down
13 changes: 4 additions & 9 deletions sidecar/app/routes/stop.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from fastapi import APIRouter, HTTPException, Request
from fastapi import APIRouter, Request

from ..query.base import Query
from ..query.status import Status
from .http_helpers import get_query_from_query_id

router = APIRouter(
prefix="/stop",
Expand All @@ -16,10 +17,7 @@ def finish(
query_id: str,
request: Request,
):
query_manager = request.app.state.QUERY_MANAGER
query = query_manager.get_from_query_id(Query, query_id)
if query is None:
raise HTTPException(status_code=404, detail="Query not found")
query = get_query_from_query_id(request.app.state.QUERY_MANAGER, Query, query_id)

query.logger.info(f"{query=}")
if query.status < Status.COMPLETE:
Expand All @@ -34,10 +32,7 @@ def kill(
query_id: str,
request: Request,
):
query_manager = request.app.state.QUERY_MANAGER
query = query_manager.get_from_query_id(Query, query_id)
if query is None:
raise HTTPException(status_code=404, detail="Query not found")
query = get_query_from_query_id(request.app.state.QUERY_MANAGER, Query, query_id)

query.logger.info(f"kill called for {query_id=}")
if query.status < Status.COMPLETE:
Expand Down
11 changes: 3 additions & 8 deletions sidecar/app/routes/websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from websockets import ConnectionClosedError, ConnectionClosedOK

from ..query.base import Query
from .http_helpers import get_query_from_query_id

router = APIRouter(
prefix="/ws",
Expand Down Expand Up @@ -51,10 +52,7 @@ async def logs_websocket(
websocket: WebSocket,
query_id: str,
):
query_manager = websocket.app.state.QUERY_MANAGER
query = query_manager.get_from_query_id(Query, query_id)
if query is None:
raise HTTPException(status_code=404, detail="Query not found")
query = get_query_from_query_id(websocket.app.state.QUERY_MANAGER, Query, query_id)

async with use_websocket(websocket) as websocket:
with open(query.log_file_path, "r", encoding="utf8") as log_file:
Expand All @@ -79,10 +77,7 @@ async def stats_websocket(
websocket: WebSocket,
query_id: str,
):
query_manager = websocket.app.state.QUERY_MANAGER
query = query_manager.get_from_query_id(Query, query_id)
if query is None:
raise HTTPException(status_code=404, detail="Query not found")
query = get_query_from_query_id(websocket.app.state.QUERY_MANAGER, Query, query_id)

async with use_websocket(websocket) as websocket:
if query.finished:
Expand Down

0 comments on commit 2fd072a

Please sign in to comment.