Skip to content

Commit

Permalink
Proper db schema for alert request and response
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Chong <aaronchongth@gmail.com>
  • Loading branch information
aaronchongth committed Jul 5, 2024
1 parent cc9a268 commit 06e5475
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
11 changes: 9 additions & 2 deletions packages/api-server/api_server/models/alerts.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from enum import Enum
from typing import List, Optional

Expand All @@ -23,7 +24,10 @@ def from_tortoise(tortoise: ttm.AlertResponse) -> "AlertResponse":
async def save(self) -> None:
await ttm.AlertResponse.update_or_create(
{
"data": self.json(),
"response_time": datetime.fromtimestamp(
self.unix_millis_response_time / 1000
),
"response": self.response,
},
id=self.id,
)
Expand Down Expand Up @@ -53,9 +57,12 @@ def from_tortoise(tortoise: ttm.AlertRequest) -> "AlertRequest":
async def save(self) -> None:
await ttm.AlertRequest.update_or_create(
{
"data": self.json(),
"request_time": datetime.fromtimestamp(
self.unix_millis_alert_time / 1000
),
"response_expected": (len(self.responses_available) > 0),
"task_id": self.task_id,
"data": self.json(),
},
id=self.id,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from tortoise.fields import (
BooleanField,
CharField,
DatetimeField,
JSONField,
OneToOneField,
ReverseRelation,
Expand All @@ -10,15 +11,17 @@

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


class AlertRequest(Model):
id = CharField(255, pk=True)
data = JSONField()
request_time = DatetimeField(null=False, index=True)
response_expected = BooleanField(null=False, index=True)
task_id = CharField(255, null=True, index=True)
data = JSONField()
alert_response = ReverseRelation["AlertResponse"]
10 changes: 8 additions & 2 deletions packages/api-server/api_server/repositories/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ async def create_new_alert(self, alert: AlertRequest) -> AlertRequest:

await ttm.AlertRequest.create(
id=alert.id,
data=alert.json(),
request_time=datetime.fromtimestamp(alert.unix_millis_alert_time / 1000),
response_expected=(len(alert.responses_available) > 0),
task_id=alert.task_id,
data=alert.json(),
)
return alert

Expand Down Expand Up @@ -45,7 +46,12 @@ async def create_response(self, alert_id: str, response: str) -> AlertResponse:
response=response,
)
await ttm.AlertResponse.create(
id=alert_id, alert_request=alert, data=alert_response_model.json()
id=alert_id,
response_time=datetime.fromtimestamp(
alert_response_model.unix_millis_response_time / 1000
),
response=response,
alert_request=alert,
)
return alert_response_model

Expand Down

0 comments on commit 06e5475

Please sign in to comment.