Skip to content

Commit 88ef44b

Browse files
authored
feat(api): add index on alert table (#2377)
1 parent 6af8a7a commit 88ef44b

File tree

3 files changed

+51
-15
lines changed

3 files changed

+51
-15
lines changed

keep/api/models/db/alert.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
# but we also want to allow it to be nullable. MySQL doesn't allow nullable fields in primary keys, so:
4848
NULL_FOR_DELETED_AT = datetime(1000, 1, 1, 0, 0)
4949

50+
5051
class AlertToIncident(SQLModel, table=True):
5152
tenant_id: str = Field(foreign_key="tenant.id")
5253
timestamp: datetime = Field(default_factory=datetime.utcnow)
@@ -61,16 +62,17 @@ class AlertToIncident(SQLModel, table=True):
6162
)
6263
alert: "Alert" = Relationship(back_populates="alert_to_incident_link")
6364
incident: "Incident" = Relationship(back_populates="alert_to_incident_link")
64-
65+
6566
is_created_by_ai: bool = Field(default=False)
6667

6768
deleted_at: datetime = Field(
6869
default_factory=None,
69-
nullable=True,
70+
nullable=True,
7071
primary_key=True,
7172
default=NULL_FOR_DELETED_AT,
7273
)
7374

75+
7476
class Incident(SQLModel, table=True):
7577
id: UUID = Field(default_factory=uuid4, primary_key=True)
7678
tenant_id: str = Field(foreign_key="tenant.id")
@@ -96,7 +98,8 @@ class Incident(SQLModel, table=True):
9698

9799
# map of attributes to values
98100
alerts: List["Alert"] = Relationship(
99-
back_populates="incidents", link_model=AlertToIncident,
101+
back_populates="incidents",
102+
link_model=AlertToIncident,
100103
# primaryjoin is used to filter out deleted links for various DB dialects
101104
sa_relationship_kwargs={
102105
"primaryjoin": f"""and_(AlertToIncident.incident_id == Incident.id,
@@ -106,14 +109,11 @@ class Incident(SQLModel, table=True):
106109
))""",
107110
"uselist": True,
108111
"overlaps": "alert,incident",
109-
}
110-
112+
},
111113
)
112114
alert_to_incident_link: List[AlertToIncident] = Relationship(
113115
back_populates="incident",
114-
sa_relationship_kwargs={
115-
"overlaps": "alerts,incidents"
116-
}
116+
sa_relationship_kwargs={"overlaps": "alerts,incidents"},
117117
)
118118

119119
is_predicted: bool = Field(default=False)
@@ -222,7 +222,7 @@ class Alert(SQLModel, table=True):
222222
)
223223

224224
incidents: List["Incident"] = Relationship(
225-
back_populates="alerts",
225+
back_populates="alerts",
226226
link_model=AlertToIncident,
227227
sa_relationship_kwargs={
228228
# primaryjoin is used to filter out deleted links for various DB dialects
@@ -233,13 +233,19 @@ class Alert(SQLModel, table=True):
233233
))""",
234234
"uselist": True,
235235
"overlaps": "alert,incident",
236-
}
236+
},
237237
)
238238
alert_to_incident_link: List[AlertToIncident] = Relationship(
239-
back_populates="alert",
240-
sa_relationship_kwargs={
241-
"overlaps": "alerts,incidents"
242-
}
239+
back_populates="alert", sa_relationship_kwargs={"overlaps": "alerts,incidents"}
240+
)
241+
242+
__table_args__ = (
243+
Index(
244+
"ix_alert_tenant_fingerprint_timestamp",
245+
"tenant_id",
246+
"fingerprint",
247+
"timestamp",
248+
),
243249
)
244250

245251
class Config:
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Adding new index on alert hash
2+
3+
Revision ID: ef0b5b0df41c
4+
Revises: 273b29f368b7
5+
Create Date: 2024-11-03 10:49:04.708264
6+
7+
"""
8+
9+
from alembic import op
10+
11+
# revision identifiers, used by Alembic.
12+
revision = "ef0b5b0df41c"
13+
down_revision = "273b29f368b7"
14+
branch_labels = None
15+
depends_on = None
16+
17+
18+
def upgrade() -> None:
19+
# Using batch operation to ensure compatibility with multiple databases
20+
with op.batch_alter_table("alert", schema=None) as batch_op:
21+
batch_op.create_index(
22+
"ix_alert_tenant_fingerprint_timestamp",
23+
["tenant_id", "fingerprint", "timestamp"],
24+
unique=False,
25+
)
26+
27+
28+
def downgrade() -> None:
29+
with op.batch_alter_table("alert", schema=None) as batch_op:
30+
batch_op.drop_index("ix_alert_tenant_fingerprint_timestamp")

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "keep"
3-
version = "0.27.8"
3+
version = "0.28.0"
44
description = "Alerting. for developers, by developers."
55
authors = ["Keep Alerting LTD"]
66
readme = "README.md"

0 commit comments

Comments
 (0)