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

fix: slow lastalert query #2958

Merged
merged 1 commit into from
Jan 1, 2025
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
112 changes: 0 additions & 112 deletions docs/deployment/getting-started.mdx

This file was deleted.

1 change: 0 additions & 1 deletion docs/mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@
{
"group": "Deployment",
"pages": [
"deployment/getting-started",
"deployment/configuration",
"deployment/monitoring",
{
Expand Down
6 changes: 3 additions & 3 deletions keep/api/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,8 @@ def get_last_alerts(
select(Alert, LastAlert.first_timestamp.label("startedAt"))
.select_from(LastAlert)
.join(Alert, LastAlert.alert_id == Alert.id)
.where(LastAlert.tenant_id == tenant_id)
.where(Alert.tenant_id == tenant_id)
)

if timeframe:
Expand All @@ -1296,9 +1298,7 @@ def get_last_alerts(
stmt = stmt.where(*filter_conditions)

# Main query for alerts
stmt = stmt.where(Alert.tenant_id == tenant_id).options(
subqueryload(Alert.alert_enrichment)
)
stmt = stmt.options(subqueryload(Alert.alert_enrichment))

if with_incidents:
if dialect_name == "sqlite":
Expand Down
8 changes: 6 additions & 2 deletions keep/api/middlewares.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import logging
import os
import jwt
import time
import logging
from importlib import metadata

import jwt
from fastapi import Request
from starlette.middleware.base import BaseHTTPMiddleware

Expand Down Expand Up @@ -56,5 +56,9 @@ async def dispatch(self, request: Request, call_next):
end_time = time.time()
logger.info(
f"Request finished: {request.method} {request.url.path} {response.status_code} in {end_time - start_time:.2f}s",
extra={
"tenant_id": identity,
"status_code": response.status_code,
},
)
return response
30 changes: 30 additions & 0 deletions keep/api/models/db/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ class LastAlert(SQLModel, table=True):
first_timestamp: datetime = Field(nullable=False, index=True)
alert_hash: str | None = Field(nullable=True, index=True)

__table_args__ = (
# Original indexes from MySQL
Index("idx_lastalert_tenant_timestamp", "tenant_id", "first_timestamp"),
Index("idx_lastalert_tenant_timestamp_new", "tenant_id", "timestamp"),
Index(
"idx_lastalert_tenant_ordering",
"tenant_id",
"first_timestamp",
"alert_id",
"fingerprint",
),
{},
)


class LastAlertToIncident(SQLModel, table=True):
tenant_id: str = Field(foreign_key="tenant.id", nullable=False, primary_key=True)
Expand Down Expand Up @@ -109,6 +123,15 @@ class LastAlertToIncident(SQLModel, table=True):
["tenant_id", "fingerprint"],
["lastalert.tenant_id", "lastalert.fingerprint"],
),
Index(
"idx_lastalerttoincident_tenant_fingerprint",
"tenant_id",
"fingerprint",
"deleted_at",
),
Index(
"idx_tenant_deleted_fingerprint", "tenant_id", "deleted_at", "fingerprint"
),
{},
)

Expand Down Expand Up @@ -254,6 +277,13 @@ class Alert(SQLModel, table=True):
"fingerprint",
"timestamp",
),
Index("idx_fingerprint_timestamp", "fingerprint", "timestamp"),
Index(
"idx_alert_tenant_timestamp_fingerprint",
"tenant_id",
"timestamp",
"fingerprint",
),
)

class Config:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""Few more indexes

Revision ID: dcb7f88a04da
Revises: 7297ae99cd21
Create Date: 2025-01-01 09:59:13.393588

"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "dcb7f88a04da"
down_revision = "7297ae99cd21"
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("alert", schema=None) as batch_op:
batch_op.create_index(
"idx_alert_tenant_timestamp_fingerprint",
["tenant_id", "timestamp", "fingerprint"],
unique=False,
)
batch_op.create_index(
"idx_fingerprint_timestamp", ["fingerprint", "timestamp"], unique=False
)

with op.batch_alter_table("lastalert", schema=None) as batch_op:
batch_op.alter_column(
"first_timestamp", existing_type=sa.DATETIME(), nullable=False
)
batch_op.create_index(
"idx_lastalert_tenant_ordering",
["tenant_id", "first_timestamp", "alert_id", "fingerprint"],
unique=False,
)
batch_op.create_index(
"idx_lastalert_tenant_timestamp",
["tenant_id", "first_timestamp"],
unique=False,
)
batch_op.create_index(
"idx_lastalert_tenant_timestamp_new",
["tenant_id", "timestamp"],
unique=False,
)

with op.batch_alter_table("lastalerttoincident", schema=None) as batch_op:
batch_op.create_index(
"idx_lastalerttoincident_tenant_fingerprint",
["tenant_id", "fingerprint", "deleted_at"],
unique=False,
)
batch_op.create_index(
"idx_tenant_deleted_fingerprint",
["tenant_id", "deleted_at", "fingerprint"],
unique=False,
)

# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###

with op.batch_alter_table("lastalerttoincident", schema=None) as batch_op:
batch_op.drop_index("idx_tenant_deleted_fingerprint")
batch_op.drop_index("idx_lastalerttoincident_tenant_fingerprint")

with op.batch_alter_table("lastalert", schema=None) as batch_op:
batch_op.drop_index("idx_lastalert_tenant_timestamp_new")
batch_op.drop_index("idx_lastalert_tenant_timestamp")
batch_op.drop_index("idx_lastalert_tenant_ordering")
batch_op.alter_column(
"first_timestamp", existing_type=sa.DATETIME(), nullable=True
)

with op.batch_alter_table("alert", schema=None) as batch_op:
batch_op.drop_index("idx_fingerprint_timestamp")
batch_op.drop_index("idx_alert_tenant_timestamp_fingerprint")

# ### end Alembic commands ###
Loading