diff --git a/keep/api/core/db.py b/keep/api/core/db.py index d4f49763f..87ced07d4 100644 --- a/keep/api/core/db.py +++ b/keep/api/core/db.py @@ -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: @@ -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: diff --git a/keep/api/models/db/workflow.py b/keep/api/models/db/workflow.py index 7290af5f4..3f7a093b9 100644 --- a/keep/api/models/db/workflow.py +++ b/keep/api/models/db/workflow.py @@ -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" ) diff --git a/keep/throttles/one_until_resolved_throttle.py b/keep/throttles/one_until_resolved_throttle.py index b7f1df0fd..52ff984cd 100644 --- a/keep/throttles/one_until_resolved_throttle.py +++ b/keep/throttles/one_until_resolved_throttle.py @@ -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 @@ -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