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

feat(api): add index on alert table #2377

Merged
merged 2 commits into from
Nov 3, 2024
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
34 changes: 20 additions & 14 deletions keep/api/models/db/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
# but we also want to allow it to be nullable. MySQL doesn't allow nullable fields in primary keys, so:
NULL_FOR_DELETED_AT = datetime(1000, 1, 1, 0, 0)


class AlertToIncident(SQLModel, table=True):
tenant_id: str = Field(foreign_key="tenant.id")
timestamp: datetime = Field(default_factory=datetime.utcnow)
Expand All @@ -61,16 +62,17 @@ class AlertToIncident(SQLModel, table=True):
)
alert: "Alert" = Relationship(back_populates="alert_to_incident_link")
incident: "Incident" = Relationship(back_populates="alert_to_incident_link")

is_created_by_ai: bool = Field(default=False)

deleted_at: datetime = Field(
default_factory=None,
nullable=True,
nullable=True,
primary_key=True,
default=NULL_FOR_DELETED_AT,
)


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

# map of attributes to values
alerts: List["Alert"] = Relationship(
back_populates="incidents", link_model=AlertToIncident,
back_populates="incidents",
link_model=AlertToIncident,
# primaryjoin is used to filter out deleted links for various DB dialects
sa_relationship_kwargs={
"primaryjoin": f"""and_(AlertToIncident.incident_id == Incident.id,
Expand All @@ -106,14 +109,11 @@ class Incident(SQLModel, table=True):
))""",
"uselist": True,
"overlaps": "alert,incident",
}

},
)
alert_to_incident_link: List[AlertToIncident] = Relationship(
back_populates="incident",
sa_relationship_kwargs={
"overlaps": "alerts,incidents"
}
sa_relationship_kwargs={"overlaps": "alerts,incidents"},
)

is_predicted: bool = Field(default=False)
Expand Down Expand Up @@ -222,7 +222,7 @@ class Alert(SQLModel, table=True):
)

incidents: List["Incident"] = Relationship(
back_populates="alerts",
back_populates="alerts",
link_model=AlertToIncident,
sa_relationship_kwargs={
# primaryjoin is used to filter out deleted links for various DB dialects
Expand All @@ -233,13 +233,19 @@ class Alert(SQLModel, table=True):
))""",
"uselist": True,
"overlaps": "alert,incident",
}
},
)
alert_to_incident_link: List[AlertToIncident] = Relationship(
back_populates="alert",
sa_relationship_kwargs={
"overlaps": "alerts,incidents"
}
back_populates="alert", sa_relationship_kwargs={"overlaps": "alerts,incidents"}
)

__table_args__ = (
Index(
"ix_alert_tenant_fingerprint_timestamp",
"tenant_id",
"fingerprint",
"timestamp",
),
)

class Config:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Adding new index on alert hash

Revision ID: ef0b5b0df41c
Revises: 273b29f368b7
Create Date: 2024-11-03 10:49:04.708264

"""

from alembic import op

# revision identifiers, used by Alembic.
revision = "ef0b5b0df41c"
down_revision = "273b29f368b7"
branch_labels = None
depends_on = None


def upgrade() -> None:
# Using batch operation to ensure compatibility with multiple databases
with op.batch_alter_table("alert", schema=None) as batch_op:
batch_op.create_index(
"ix_alert_tenant_fingerprint_timestamp",
["tenant_id", "fingerprint", "timestamp"],
unique=False,
)


def downgrade() -> None:
with op.batch_alter_table("alert", schema=None) as batch_op:
batch_op.drop_index("ix_alert_tenant_fingerprint_timestamp")
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "keep"
version = "0.27.8"
version = "0.28.0"
description = "Alerting. for developers, by developers."
authors = ["Keep Alerting LTD"]
readme = "README.md"
Expand Down
Loading