Skip to content

refactor: Refactor alert and incident property handling. #3941

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

Closed
Closed
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
6 changes: 3 additions & 3 deletions keep/api/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -1860,7 +1860,7 @@ def get_incident_for_grouping_rule(
elif incident and incident.alerts_count > 0:
enrich_incidents_with_alerts(tenant_id, [incident], session)
is_incident_expired = max(
alert.timestamp for alert in incident._alerts
alert.timestamp for alert in incident.alerts
) < datetime.utcnow() - timedelta(seconds=rule.timeframe)

# if there is no incident with the rule_fingerprint, create it or existed is already expired
Expand Down Expand Up @@ -4175,7 +4175,7 @@ def merge_incidents_to_id(
failed_incident_ids = []
for source_incident in source_incidents:
source_incident_alerts_fingerprints = [
alert.fingerprint for alert in source_incident._alerts
alert.fingerprint for alert in source_incident.alerts
]
if not source_incident_alerts_fingerprints:
logger.info(f"Source incident {source_incident.id} doesn't have alerts")
Expand All @@ -4189,7 +4189,7 @@ def merge_incidents_to_id(
remove_alerts_to_incident_by_incident_id(
tenant_id,
source_incident.id,
[alert.fingerprint for alert in source_incident._alerts],
[alert.fingerprint for alert in source_incident.alerts],
)
except OperationalError as e:
logger.error(
Expand Down
3 changes: 3 additions & 0 deletions keep/api/models/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,9 @@ def __str__(self) -> str:
model_dict = self.dict()
return json.dumps(model_dict, indent=4, default=str)

def set_alerts(self, alerts: List[AlertDto]):
self._alerts = alerts

class Config:
extra = Extra.allow
schema_extra = IncidentDtoIn.Config.schema_extra
Expand Down
17 changes: 17 additions & 0 deletions keep/api/models/db/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from sqlalchemy.dialects.mssql import DATETIME2 as MSSQL_DATETIME2
from sqlalchemy.dialects.mysql import DATETIME as MySQL_DATETIME
from sqlalchemy.engine.url import make_url
from sqlalchemy.orm import object_session
from sqlalchemy_utils import UUIDType
from sqlmodel import JSON, TEXT, Column, DateTime, Field, Index, Relationship, SQLModel

Expand Down Expand Up @@ -243,12 +244,17 @@ class Incident(SQLModel, table=True):

_alerts: List["Alert"] = PrivateAttr(default_factory=list)
_enrichments: dict = PrivateAttr(default={})
_alerts_cached: bool = PrivateAttr(default=False)

class Config:
arbitrary_types_allowed = True

@property
def alerts(self):
if not getattr(self, "_alerts_cached", False):
from keep.api.core.db import enrich_incidents_with_alerts
enrich_incidents_with_alerts(self.tenant_id, [self], session=object_session(self))
self._alerts_cached = True
return self._alerts

@property
Expand Down Expand Up @@ -297,6 +303,7 @@ class Alert(SQLModel, table=True):
)

_incidents: List[Incident] = PrivateAttr(default_factory=list)
_incidents_cached: bool = PrivateAttr(default=False)

__table_args__ = (
Index(
Expand Down Expand Up @@ -324,6 +331,16 @@ class Alert(SQLModel, table=True):
),
)

@property
def incidents(self):
if not getattr(self, "_incidents_cached", False):
from keep.api.core.db import enrich_alerts_with_incidents
enrich_alerts_with_incidents(self.tenant_id, [self], session=object_session(self))
self._incidents_cached = True
return self._incidents



class Config:
arbitrary_types_allowed = True

Expand Down
2 changes: 1 addition & 1 deletion keep/api/routes/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ def _enrich_alert(
# @tb add "and session" cuz I saw AttributeError: 'NoneType' object has no attribute 'add'"
if should_check_incidents_resolution and session:
enrich_alerts_with_incidents(tenant_id=tenant_id, alerts=alert)
for incident in alert[0]._incidents:
for incident in alert[0].incidents:
if (
incident.resolve_on == ResolveOn.ALL.value
and is_all_alerts_resolved(incident=incident, session=session)
Expand Down
2 changes: 1 addition & 1 deletion keep/api/routes/incidents.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ def change_incident_status(
# We need to do something only if status really changed
if change.status != incident.status:
if change.status in [IncidentStatus.RESOLVED, IncidentStatus.ACKNOWLEDGED]:
for alert in incident._alerts:
for alert in incident.alerts:
_enrich_alert(
EnrichAlertRequestBody(
enrichments={"status": change.status.value},
Expand Down
4 changes: 2 additions & 2 deletions keep/api/utils/enrichment_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ def convert_db_alerts_to_dto_alerts(
alert.event.update(enrichments)

if with_incidents:
if alert._incidents:
if alert.incidents:
alert.event["incident"] = ",".join(
str(incident.id) for incident in alert._incidents
str(incident.id) for incident in alert.incidents
)
try:
if alert_to_incident is not None:
Expand Down
2 changes: 1 addition & 1 deletion keep/providers/pagerduty_provider/pagerduty_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ def _get_incidents(self) -> list[IncidentDto]:
PagerdutyProvider._format_alert(alert, None, force_new_format=True)
for alert in incident_alerts
]
incident_dto._alerts = incident_alerts
incident_dto.set_alerts(incident_alerts)
incidents.append(incident_dto)
return incidents

Expand Down
2 changes: 1 addition & 1 deletion tests/test_rules_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ def test_rule_multiple_alerts(db_session, create_alert):
incident = db_session.query(Incident).first()
alert_1 = db_session.query(Alert).order_by(Alert.timestamp.desc()).first()

enrich_incidents_with_alerts(SINGLE_TENANT_UUID, [incident], db_session)
# enrich_incidents_with_alerts(SINGLE_TENANT_UUID, [incident], db_session)

assert incident.alerts_count == 1
assert len(incident.alerts) == 1
Expand Down
Loading