Skip to content

Commit

Permalink
Switch to last alert and fingerprints
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirFilonov committed Dec 7, 2024
1 parent ece5f4e commit cc5e770
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
11 changes: 5 additions & 6 deletions keep/api/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -4265,15 +4265,15 @@ def get_workflow_executions_for_incident_or_alert(
return results, total_count

def is_all_alerts_resolved(
alert_ids: Optional[List[str | UUID]] = None,
fingerprints: Optional[List[str]] = None,
incident: Optional[Incident] = None,
session: Optional[Session] = None
):
return is_all_alerts_in_status(alert_ids, incident, AlertStatus.RESOLVED, session)
return is_all_alerts_in_status(fingerprints, incident, AlertStatus.RESOLVED, session)


def is_all_alerts_in_status(
alert_ids: Optional[List[str | UUID]] = None,
fingerprints: Optional[List[str]] = None,
incident: Optional[Incident] = None,
status: AlertStatus = AlertStatus.RESOLVED,
session: Optional[Session] = None
Expand Down Expand Up @@ -4302,12 +4302,11 @@ def is_all_alerts_in_status(
Alert.fingerprint == AlertEnrichment.alert_fingerprint
),
)
.group_by(Alert.fingerprint)
.having(func.max(Alert.timestamp))
)

if alert_ids:
subquery = subquery.where(Alert.id.in_(alert_ids))
if fingerprints:
subquery = subquery.where(LastAlert.fingerprint.in_(fingerprints))

if incident:
subquery = (
Expand Down
4 changes: 2 additions & 2 deletions keep/api/models/db/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ def is_all_conditions_met(self, rule_groups: List[str]):
for condition in rule_groups
])

def add_alert(self, condition, alert_id):
def add_alert(self, condition, fingerprint):
self.state.setdefault(condition, [])
self.state[condition].append(alert_id)
self.state[condition].append(fingerprint)
flag_modified(self, "state")

def get_all_alerts(self):
Expand Down
10 changes: 5 additions & 5 deletions keep/rulesengine/rulesengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def run_rules(
session=session,
)
incident = assign_alert_to_incident(
alert_id=event.event_id,
fingerprint=event.fingerprint,
incident=incident,
tenant_id=self.tenant_id,
session=session,
Expand All @@ -120,12 +120,12 @@ def run_rules(
)

rule_group = self._get_rule_group(rule, session)
rule_group.add_alert(sub_rule, event.event_id)
rule_group.add_alert(sub_rule, event.fingerprint)

alert_ids = rule_group.get_all_alerts()
fingerprints = rule_group.get_all_alerts()

if rule_group.is_all_conditions_met(rule_groups) and is_all_alerts_in_status(
alert_ids=alert_ids, status=AlertStatus.FIRING, session=session
fingerprints=fingerprints, status=AlertStatus.FIRING, session=session
):

self.logger.info(
Expand All @@ -139,7 +139,7 @@ def run_rules(
session=session,
)

incident = add_alerts_to_incident(self.tenant_id, incident, alert_ids, session=session)
incident = add_alerts_to_incident(self.tenant_id, incident, fingerprints, session=session)

session.delete(rule_group)
session.commit()
Expand Down

0 comments on commit cc5e770

Please sign in to comment.