Skip to content

Commit

Permalink
feat: fix elastic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shahargl committed Nov 10, 2024
1 parent ab54785 commit b457df3
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions keep/searchengine/searchengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from keep.api.core.tenant_configuration import TenantConfiguration
from keep.api.models.alert import AlertDto, AlertStatus
from keep.api.models.db.preset import PresetDto, PresetSearchQuery
from keep.api.models.time_stamp import TimeStampFilter
from keep.api.utils.enrichment_helpers import convert_db_alerts_to_dto_alerts
from keep.rulesengine.rulesengine import RulesEngine
from keep.api.models.time_stamp import TimeStampFilter


class SearchMode(enum.Enum):
Expand Down Expand Up @@ -52,7 +52,9 @@ def __init__(self, tenant_id):
extra={"tenant_id": self.tenant_id, "search_mode": self.search_mode},
)

def _get_last_alerts(self, limit=1000, timeframe: int = 0, time_stamp:TimeStampFilter=None) -> list[AlertDto]:
def _get_last_alerts(
self, limit=1000, timeframe: int = 0, time_stamp: TimeStampFilter = None
) -> list[AlertDto]:
"""Get the last alerts
Returns:
Expand All @@ -63,13 +65,18 @@ def _get_last_alerts(self, limit=1000, timeframe: int = 0, time_stamp:TimeStampF
upper_timestamp = time_stamp.upper_timestamp if time_stamp else None

alerts = get_last_alerts(
tenant_id=self.tenant_id, limit=limit, timeframe=timeframe,
lower_timestamp=lower_timestamp, upper_timestamp=upper_timestamp,
with_incidents=True
tenant_id=self.tenant_id,
limit=limit,
timeframe=timeframe,
lower_timestamp=lower_timestamp,
upper_timestamp=upper_timestamp,
with_incidents=True,
)
# convert the alerts to DTO
alerts_dto = convert_db_alerts_to_dto_alerts(alerts)
self.logger.info(f"Finished getting last alerts {lower_timestamp} {upper_timestamp} {time_stamp}")
self.logger.info(
f"Finished getting last alerts {lower_timestamp} {upper_timestamp} {time_stamp}"
)
return alerts_dto

def search_alerts_by_cel(
Expand Down Expand Up @@ -113,7 +120,8 @@ def _search_alerts_by_sql(
query = self._create_raw_sql(sql_query.get("sql"), sql_query.get("params"))
# get the alerts from elastic
elastic_sql_query = (
f"""select * from "{self.elastic_client.alerts_index}" where {query}"""
f"""select * from "{self.elastic_client.alerts_index}" """
+ (f"where {query}" if query else "")
)
if timeframe:
elastic_sql_query += f" and lastReceived > now() - {timeframe}s"
Expand Down Expand Up @@ -156,8 +164,7 @@ def search_alerts(self, query: PresetSearchQuery) -> list[AlertDto]:
return filtered_alerts

def search_preset_alerts(
self, presets: list[PresetDto],
time_stamp: TimeStampFilter = None
self, presets: list[PresetDto], time_stamp: TimeStampFilter = None
) -> dict[str, list[AlertDto]]:
"""Search for alerts based on a list of queries
Expand All @@ -184,6 +191,7 @@ def search_preset_alerts(
)
preset.alerts_count = len(filtered_alerts)
# update noisy

if preset.is_noisy:
firing_filtered_alerts = list(
filter(
Expand Down Expand Up @@ -220,7 +228,10 @@ def search_preset_alerts(
preset.sql_query.get("sql"), preset.sql_query.get("params")
)
# get number of alerts and number of noisy alerts
elastic_sql_query = f"""select count(*), MAX(CASE WHEN isNoisy = true AND dismissed = false AND deleted = false THEN 1 ELSE 0 END) from "{self.elastic_client.alerts_index}" where {query}"""
elastic_sql_query = (
f"""select count(*), MAX(CASE WHEN isNoisy = true AND dismissed = false AND deleted = false THEN 1 ELSE 0 END) from "{self.elastic_client.alerts_index}" """
+ (f" where {query}" if query else "")
)
results = self.elastic_client.run_query(elastic_sql_query)
if results:
preset.alerts_count = results["rows"][0][0]
Expand Down

0 comments on commit b457df3

Please sign in to comment.