Skip to content

Commit

Permalink
Make __update_client_on_incident_change and __run_workflows public
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirFilonov committed Dec 11, 2024
1 parent e7f025c commit 399f83a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 33 deletions.
42 changes: 11 additions & 31 deletions keep/api/bl/incidents_bl.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ def create_incident(
"Incident DTO created",
extra={"incident_id": new_incident_dto.id, "tenant_id": self.tenant_id},
)
self.__update_client_on_incident_change()
self.update_client_on_incident_change()
self.logger.info(
"Client updated on incident change",
extra={"incident_id": new_incident_dto.id, "tenant_id": self.tenant_id},
)
self.__run_workflows(new_incident_dto, "created")
self.send_workflow_event(new_incident_dto, "created")
self.logger.info(
"Workflows run on incident",
extra={"incident_id": new_incident_dto.id, "tenant_id": self.tenant_id},
Expand Down Expand Up @@ -113,13 +113,13 @@ async def add_alerts_to_incident(
"Alerts pushed to elastic",
extra={"incident_id": incident_id, "alert_fingerprints": alert_fingerprints},
)
self.__update_client_on_incident_change(incident_id)
self.update_client_on_incident_change(incident_id)
self.logger.info(
"Client updated on incident change",
extra={"incident_id": incident_id, "alert_fingerprints": alert_fingerprints},
)
incident_dto = IncidentDto.from_db_incident(incident)
self.__run_workflows(incident_dto, "updated")
self.send_workflow_event(incident_dto, "updated")
self.logger.info(
"Workflows run on incident",
extra={"incident_id": incident_id, "alert_fingerprints": alert_fingerprints},
Expand All @@ -146,7 +146,7 @@ def __update_elastic(self, incident_id: UUID, alert_fingerprints: List[str]):
except Exception:
self.logger.exception("Failed to push alert to elasticsearch")

def __update_client_on_incident_change(self, incident_id: Optional[UUID] = None):
def update_client_on_incident_change(self, incident_id: Optional[UUID] = None):
if self.pusher_client is not None:
self.logger.info(
"Pushing incident change to client",
Expand All @@ -162,7 +162,7 @@ def __update_client_on_incident_change(self, incident_id: Optional[UUID] = None)
extra={"incident_id": incident_id, "tenant_id": self.tenant_id},
)

def __run_workflows(self, incident_dto: IncidentDto, action: str):
def send_workflow_event(self, incident_dto: IncidentDto, action: str) -> None:
try:
workflow_manager = WorkflowManager.get_instance()
workflow_manager.insert_incident(self.tenant_id, incident_dto, action)
Expand Down Expand Up @@ -238,17 +238,8 @@ def delete_incident(self, incident_id: UUID) -> None:
)
if not deleted:
raise HTTPException(status_code=404, detail="Incident not found")
self.__update_client_on_incident_change()
try:
workflow_manager = WorkflowManager.get_instance()
self.logger.info("Adding incident to the workflow manager queue")
workflow_manager.insert_incident(self.tenant_id, incident_dto, "deleted")
self.logger.info("Added incident to the workflow manager queue")
except Exception:
self.logger.exception(
"Failed to run workflows based on incident",
extra={"incident_id": incident_dto.id, "tenant_id": self.tenant_id},
)
self.update_client_on_incident_change()
self.send_workflow_event(incident_dto, "deleted")

def update_incident(
self,
Expand All @@ -269,17 +260,6 @@ def update_incident(
if not incident:
raise HTTPException(status_code=404, detail="Incident not found")

new_incident_dto = IncidentDto.from_db_incident(incident)
try:
workflow_manager = WorkflowManager.get_instance()
self.logger.info("Adding incident to the workflow manager queue")
workflow_manager.insert_incident(
self.tenant_id, new_incident_dto, "updated"
)
self.logger.info("Added incident to the workflow manager queue")
except Exception:
self.logger.exception(
"Failed to run workflows based on incident",
extra={"incident_id": new_incident_dto.id, "tenant_id": self.tenant_id},
)
return new_incident_dto
incident_dto = IncidentDto.from_db_incident(incident)
self.send_workflow_event(incident_dto, "updated")
return incident_dto
4 changes: 2 additions & 2 deletions keep/api/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -3291,7 +3291,7 @@ def create_incident_from_dict(

def update_incident_from_dto_by_id(
tenant_id: str,
incident_id: str,
incident_id: str | UUID,
updated_incident_dto: IncidentDtoIn | IncidentDto,
generated_by_ai: bool = False,
) -> Optional[Incident]:
Expand Down Expand Up @@ -3650,7 +3650,7 @@ def add_alerts_to_incident(
return incident


def get_incident_unique_fingerprint_count(tenant_id: str, incident_id: str) -> int:
def get_incident_unique_fingerprint_count(tenant_id: str, incident_id: str | UUID) -> int:
with Session(engine) as session:
return session.execute(
select(func.count(1))
Expand Down

0 comments on commit 399f83a

Please sign in to comment.