Skip to content

Commit

Permalink
Route for unresponded alerts
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Chong <[email protected]>
  • Loading branch information
aaronchongth committed May 23, 2024
1 parent 4d75109 commit d2a7d58
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 53 deletions.
1 change: 0 additions & 1 deletion packages/api-server/api_server/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,4 @@
from .rmf_api.task_state_update import TaskStateUpdate
from .rmf_api.undo_skip_phase_request import UndoPhaseSkipRequest
from .rmf_api.undo_skip_phase_response import UndoPhaseSkipResponse
from .tasks import *
from .user import *
7 changes: 0 additions & 7 deletions packages/api-server/api_server/models/tasks.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from .lift_state import LiftState
from .log import LogMixin
from .scheduled_task import *
from .tasks import ( # TaskLocationCheckIn,
from .tasks import (
TaskEventLog,
TaskEventLogLog,
TaskEventLogPhases,
Expand Down
17 changes: 4 additions & 13 deletions packages/api-server/api_server/models/tortoise_models/alerts.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
from tortoise.fields import CharField, ForeignKeyField, JSONField, ReverseRelation
from tortoise.fields import BooleanField, CharField, JSONField, OneToOneField
from tortoise.models import Model


class AlertResponse(Model):
id = CharField(255, pk=True)
alert_request = ForeignKeyField(
"models.AlertRequest", null=True, related_name="alert_response"
alert_request = OneToOneField(
"models.AlertRequest", null=False, related_name="alert_response"
)
data = JSONField()


class AlertRequest(Model):
id = CharField(255, pk=True)
data = JSONField()
response_expected = BooleanField(null=False, index=True)
task_id = CharField(255, null=True, index=True)
alert_response = ReverseRelation["FleetAlertResponse"]


# how to let backend know that the robot is ready for handling
# how does the backend save this information such that the smart cart API server can query it
# new location and destination model
# when an alert comes in with task id, and with alert parameters
# reached: xx, we update new model for task, so SCAS queries and can update

# how do we change destinations?
43 changes: 30 additions & 13 deletions packages/api-server/api_server/routes/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ async def create_new_alert(alert: AlertRequest):
if exists:
raise HTTPException(409, f"Alert with ID {alert.id} already exists")

await ttm.AlertRequest.create(id=alert.id, data=alert.json(), task_id=alert.task_id)
await ttm.AlertRequest.create(
id=alert.id,
data=alert.json(),
response_expected=(len(alert.responses_available) > 0 and alert.display),
task_id=alert.task_id,
)

if alert.display:
alert_events.alert_requests.on_next(alert)
Expand All @@ -48,12 +53,13 @@ async def respond_to_alert(alert_id: str, response: str):
"""
alert = await ttm.AlertRequest.get_or_none(id=alert_id)
if alert is None:
raise HTTPException(404, f"Alert with ID {alert.id} does not exists")
raise HTTPException(404, f"Alert with ID {alert_id} does not exists")

alert_model = AlertRequest(**alert.data)
if response not in alert_model.responses_available:
raise HTTPException(
422, f"Alert with ID {alert.id} does not have allow response of {response}"
422,
f"Alert with ID {alert_model.id} does not have allow response of {response}",
)

alert_response_model = AlertResponse(
Expand Down Expand Up @@ -104,15 +110,26 @@ async def get_alerts_of_task(task_id: str, unresponded: bool = True):
Returns all the alerts associated to a task ID. Provides the option to only
return alerts that have not been responded to yet.
"""
task_id_alerts = await ttm.AlertRequest.filter(task_id=task_id)

alert_models = []
for alert in task_id_alerts:
alert_model = AlertRequest(**alert.data)
if unresponded:
task_id_alerts = await ttm.AlertRequest.filter(
response_expected=True,
task_id=task_id,
alert_response=None,
)
else:
task_id_alerts = await ttm.AlertRequest.filter(task_id=task_id)

if unresponded:
response_exists = await ttm.AlertResponse.get_or_none(id=alert_model.id)
if response_exists:
continue
alert_models.append(alert_model)
alert_models = [AlertRequest(**alert.data) for alert in task_id_alerts]
return alert_models


@router.get("/unresponded", response_model=List[AlertRequest])
async def get_unresponded_alert_ids():
"""
Returns the list of alert IDs that have yet to be responded to, while a
response was required.
"""
unresponded_alerts = await ttm.AlertRequest.filter(
alert_response=None, response_expected=True
)
return [AlertRequest(**alert.data) for alert in unresponded_alerts]
18 changes: 0 additions & 18 deletions packages/api-server/api_server/routes/tasks/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,21 +350,3 @@ async def post_undo_skip_phase(
request: mdl.UndoPhaseSkipRequest = Body(...),
):
return RawJSONResponse(await tasks_service().call(request.json(exclude_none=True)))


# @router.post("/location/check_in")
# async def location_check_in(
# task_id: str,
# location: str
# ):
# """
# Allows tagging a named location to a task. This is a way to inform any downstream
# applications the whereabouts
# """
# # TODO: check if the location is in buildingmap
# # TODO: check if task is still an ongoing task
# DbTaskLocationCheckIn.create(
# task_id=task_id,
# unix_millis_check_in_time=round(datetime.now().timestamp() * 1000),
# location=location
# )

0 comments on commit d2a7d58

Please sign in to comment.