Skip to content

Commit

Permalink
Feature/save request (#842) (#843)
Browse files Browse the repository at this point in the history
* Feature/save request (#842)

* New route for saving and getting successfully dispatched requests

Signed-off-by: Aaron Chong <[email protected]>

* Announce failed to dispatch instead of create task

Signed-off-by: Aaron Chong <[email protected]>

* Generate api-client

Signed-off-by: Aaron Chong <[email protected]>

* Use api task request instead of custom ttm pydantic model

Signed-off-by: Aaron Chong <[email protected]>

* Revert alert text change

Signed-off-by: Aaron Chong <[email protected]>

---------

Signed-off-by: Aaron Chong <[email protected]>
(cherry picked from commit 52a7d14)
Signed-off-by: Aaron Chong <[email protected]>

* Regenerating api-client

Signed-off-by: Aaron Chong <[email protected]>

* Query multiple task requests

Signed-off-by: Aaron Chong <[email protected]>

* Lint

Signed-off-by: Aaron Chong <[email protected]>

---------

Signed-off-by: Aaron Chong <[email protected]>
  • Loading branch information
aaronchongth authored Nov 28, 2023
1 parent 9c2b590 commit 7663eac
Show file tree
Hide file tree
Showing 8 changed files with 326 additions and 9 deletions.
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

0 comments on commit 7663eac

Please sign in to comment.