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/save request (#842) #843

Merged
merged 4 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
180 changes: 175 additions & 5 deletions packages/api-client/lib/openapi/api.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/api-client/lib/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { version as rmfModelVer } from 'rmf-models';

export const version = {
rmfModels: rmfModelVer,
rmfServer: '9c6122b74319a51ceff1ca27a493e1cbaf35aa42',
rmfServer: '1970313b2544aa6fb1ee65c9356f2eface9b174b',
openapiGenerator: '6.2.1',
};
77 changes: 74 additions & 3 deletions packages/api-client/schema/index.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
TaskEventLogPhasesLog,
TaskFavorite,
TaskFavoritePydantic,
TaskRequest,
TaskState,
)
from .user import *
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
from .log import LogMixin


class TaskRequest(Model):
id_ = CharField(255, pk=True, source_field="id")
request = JSONField()


class TaskState(Model):
id_ = CharField(255, pk=True, source_field="id")
data = JSONField()
Expand Down
20 changes: 20 additions & 0 deletions packages/api-server/api_server/repositories/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
Pagination,
Phases,
TaskEventLog,
TaskRequest,
TaskState,
User,
)
from api_server.models import tortoise_models as ttm
from api_server.models.rmf_api.log_entry import Tier
from api_server.models.rmf_api.task_state import Category, Id, Phase
from api_server.models.tortoise_models import TaskRequest as DbTaskRequest
from api_server.models.tortoise_models import TaskState as DbTaskState
from api_server.query import add_pagination
from api_server.rmf_io import task_events
Expand All @@ -30,6 +32,24 @@ class TaskRepository:
def __init__(self, user: User):
self.user = user

async def save_task_request(self, task_id: str, task_request: TaskRequest) -> None:
await DbTaskRequest.update_or_create(
{"request": task_request.json()}, id_=task_id
)

async def get_task_request(self, task_id: str) -> Optional[TaskRequest]:
result = await DbTaskRequest.get_or_none(id_=task_id)
if result is None:
return None
return TaskRequest(**result.request)

async def query_task_requests(self, task_ids: List[str]) -> List[DbTaskRequest]:
filters = {"id___in": task_ids}
try:
return await DbTaskRequest.filter(**filters)
except FieldError as e:
raise HTTPException(422, str(e)) from e

async def save_task_state(self, task_state: TaskState) -> None:
db_task_state = await DbTaskState.get_or_none(id_=task_state.booking.id)
if db_task_state is not None:
Expand Down
37 changes: 37 additions & 0 deletions packages/api-server/api_server/routes/tasks/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,42 @@
router = FastIORouter(tags=["Tasks"])


@router.get("/{task_id}/request", response_model=mdl.TaskRequest)
async def get_task_request(
task_repo: TaskRepository = Depends(task_repo_dep),
task_id: str = Path(..., description="task_id"),
):
result = await task_repo.get_task_request(task_id)
if result is None:
raise HTTPException(status_code=404)
return result


@router.get("/requests", response_model=List[Optional[mdl.TaskRequest]])
async def query_task_requests(
task_repo: TaskRepository = Depends(task_repo_dep),
task_ids: Optional[str] = Query(
None, description="comma separated list of task ids"
),
):
task_id_splits = []
if task_ids is not None:
task_id_splits = task_ids.split(",")
valid_task_requests = await task_repo.query_task_requests(task_id_splits)

valid_task_request_map = {}
for valid_req in valid_task_requests:
valid_task_request_map[valid_req.id_] = mdl.TaskRequest(**valid_req.request)

return_requests = []
for id_query in task_id_splits:
if id_query in valid_task_request_map:
return_requests.append(valid_task_request_map[id_query])
else:
return_requests.append(None)
return return_requests


@router.get("", response_model=List[mdl.TaskState])
async def query_task_states(
task_repo: TaskRepository = Depends(task_repo_dep),
Expand Down Expand Up @@ -149,6 +185,7 @@ async def post_dispatch_task(
if task_warn_time is not None:
new_state.unix_millis_warn_time = task_warn_time
await task_repo.save_task_state(new_state)
await task_repo.save_task_request(new_state.booking.id, request.request)
return resp.__root__


Expand Down
13 changes: 13 additions & 0 deletions packages/api-server/api_server/routes/tasks/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,19 @@ def test_success(self):
self.assertEqual(200, resp.status_code, resp.content)
self.assertEqual(task_id, resp.json()["booking"]["id"])

def test_task_request_exist(self):
task_id = str(uuid4())
with patch.object(tasks_service(), "call") as mock:
mock.return_value = f'{{ "success": true, "state": {{ "booking": {{ "id": "{task_id}" }} }} }}'
resp = self.post_task_request()
self.assertEqual(200, resp.status_code, resp.content)

# check that the task request is in the database
resp = self.client.get(f"/tasks/{task_id}/request")
self.assertEqual(200, resp.status_code, resp.content)
self.assertEqual("test", resp.json()["category"])
self.assertEqual("description", resp.json()["description"])

def test_fail_with_multiple_errors(self):
# fails with multiple errors
with patch.object(tasks_service(), "call") as mock:
Expand Down
Loading