Skip to content

Commit

Permalink
feat: implement throttle mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
pehlicd committed Apr 11, 2024
1 parent 272c3fc commit f551299
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
13 changes: 12 additions & 1 deletion keep/api/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def create_db_and_tables():
logger.info("Migrating WorkflowToAlertExecution table")
# get the foreign key constraint name
results = session.exec(
f"SELECT CONSTRAINT_NAME FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = '{engine.url.database}' AND TABLE_NAME = 'workflowtoalertexecution' AND COLUMN_NAME = 'alert_fingerprint' AND COLUMN_NAME = 'event_id';"
f"SELECT CONSTRAINT_NAME FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = '{engine.url.database}' AND TABLE_NAME = 'workflowtoalertexecution' AND COLUMN_NAME = 'alert_fingerprint';"
)
# now remove it
for row in results:
Expand All @@ -153,6 +153,17 @@ def create_db_and_tables():
f"ALTER TABLE workflowtoalertexecution DROP FOREIGN KEY {constraint_name};"
)
logger.info(f"Dropped constraint {constraint_name}")
# now add the new column
try:
session.exec("ALTER TABLE workflowtoalertexecution ADD COLUMN event_id VARCHAR(255);")
except Exception as e:
# that's ok
if "Duplicate column name" in str(e):
pass
# else, log
else:
logger.exception("Failed to migrate rule table")
pass
# also add grouping_criteria to the workflow table
logger.info("Migrating Rule table")
try:
Expand Down
2 changes: 1 addition & 1 deletion keep/api/models/db/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class WorkflowToAlertExecution(SQLModel, table=True):
id: Optional[int] = Field(primary_key=True, default=None)
workflow_execution_id: str = Field(foreign_key="workflowexecution.id")
alert_fingerprint: str
event_id: str
event_id: str | None
workflow_execution: WorkflowExecution = Relationship(
back_populates="workflow_to_alert_execution"
)
Expand Down
5 changes: 4 additions & 1 deletion keep/throttles/one_until_resolved_throttle.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import logging

from keep.api.core.db import get_alert_by_fingerprint_and_event_id, \
get_workflow_to_alert_execution_by_workflow_execution_id
from keep.api.models.alert import AlertStatus
from keep.throttles.base_throttle import BaseThrottle
from keep.contextmanager.contextmanager import ContextManager

Expand Down Expand Up @@ -31,7 +34,7 @@ def check_throttling(self, action_name, workflow_id, event_id, **kwargs) -> bool
return False

# if the last time the alert were triggered it was in resolved status, return false
if alert.event.get("status").lower() == "resolved":
if AlertStatus(alert.event.get("status")) == AlertStatus.RESOLVED:
return False

# else, return true because its already firing
Expand Down

0 comments on commit f551299

Please sign in to comment.