Skip to content

Commit

Permalink
Merge pull request #828 from chaos-genius/develop
Browse files Browse the repository at this point in the history
v0.5.0 release
  • Loading branch information
Samyak2 committed Mar 15, 2022
2 parents 7f36c5c + d65006e commit d64d025
Show file tree
Hide file tree
Showing 100 changed files with 4,119 additions and 3,076 deletions.
3 changes: 2 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ CHAOSGENIUS_WEBAPP_URL=http://localhost:8080/

# Alert configuration
## to enable event alerts
REACT_APP_EVENT_ALERT=false
REACT_APP_EVENT_ALERT=true

# Docker volume mounts
CG_DB_DOCKER_MOUNT=cg_db
Expand Down Expand Up @@ -148,6 +148,7 @@ MAX_FILTER_SUBGROUPS_ANOMALY=250
MAX_DEEPDRILLS_SLACK_DAYS=14
MAX_ANOMALY_SLACK_DAYS=14
DAYS_OFFSET_FOR_ANALTYICS=2
HOURS_OFFSET_FOR_ANALTYICS=0
DEEPDRILLS_HTABLE_MAX_PARENTS=5
DEEPDRILLS_HTABLE_MAX_CHILDREN=5
DEEPDRILLS_HTABLE_MAX_DEPTH=3
Expand Down
82 changes: 82 additions & 0 deletions chaos_genius/alerts/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import logging
from datetime import date
from typing import List, Tuple

from chaos_genius.alerts.anomaly_alerts import AnomalyAlertController
from chaos_genius.alerts.event_alerts import StaticEventAlertController
from chaos_genius.alerts.static_kpi_alerts import StaticKpiAlertController
from chaos_genius.databases.models.alert_model import Alert
from chaos_genius.databases.models.data_source_model import DataSource
from chaos_genius.databases.models.kpi_model import Kpi

logger = logging.getLogger()


def check_and_trigger_alert(alert_id):
"""Check the alert and trigger the notification if found
Args:
alert_id (int): alert id
Raises:
Exception: Raise if the alert record not found
Returns:
bool: status of the alert trigger
"""
alert_info = Alert.get_by_id(alert_id)
if not alert_info:
raise Exception("Alert doesn't exist")

if not (alert_info.active and alert_info.alert_status):
print("Alert isn't active. Please activate the alert.")
return True

if alert_info.alert_type == "Event Alert":

data_source_id = alert_info.data_source
data_source_obj = DataSource.get_by_id(data_source_id)
static_alert_obj = StaticEventAlertController(
alert_info.as_dict, data_source_obj.as_dict
)
static_alert_obj.check_and_prepare_alert()
elif (
alert_info.alert_type == "KPI Alert" and alert_info.kpi_alert_type == "Anomaly"
):
anomaly_obj = AnomalyAlertController(alert_info.as_dict)
return anomaly_obj.check_and_prepare_alert()
elif alert_info.alert_type == "KPI Alert" and alert_info.kpi_alert_type == "Static":
static_kpi_alert = StaticKpiAlertController(alert_info.as_dict)

return True


def trigger_anomaly_alerts_for_kpi(
kpi_obj: Kpi, end_date: date
) -> Tuple[List[int], List[int]]:
"""Triggers anomaly alerts starting from end_date.
Args:
kpi_obj (Kpi): Object of kpi for which alerts are to be triggered
end_date (dateimte.datetime): Datetime object containing the upper bound of anomaly date values
Returns:
List[int]: List of alert IDs for which alert messages were successfully sent
List[int]: List of alert IDs for which alert failed
"""
success_alerts = []
errors = []
alerts = Alert.query.filter(
Alert.kpi == kpi_obj.id, Alert.active == True, Alert.alert_status == True
).all()
for alert in alerts:
try:
anomaly_obj = AnomalyAlertController(
alert.as_dict, anomaly_end_date=end_date
)
anomaly_obj.check_and_prepare_alert()
success_alerts.append(alert.id)
except Exception as e:
logger.error(f"Error running alert for Alert ID: {alert.id}", exc_info=e)
errors.append(alert.id)
return success_alerts, errors
21 changes: 9 additions & 12 deletions chaos_genius/alerts/alert_channel_creds.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ def get_email_creds(name):
if config_obj is None:
return "", "", "", "", ""

configs = config_obj.as_dict.get('config_setting', {})
configs = config_obj.as_dict.get("config_setting", {})
return (
configs.get('server', ''),
configs.get('port', ''),
configs.get('username', ''),
configs.get('password', ''),
configs.get('sender_email', '')
configs.get("server", ""),
configs.get("port", ""),
configs.get("username", ""),
configs.get("password", ""),
configs.get("sender_email", ""),
)


Expand All @@ -25,11 +25,8 @@ def get_slack_creds(name):
if config_obj is None:
return ""

configs = config_obj.as_dict.get('config_setting', {})
return configs.get('webhook_url', '')
configs = config_obj.as_dict.get("config_setting", {})
return configs.get("webhook_url", "")


HELPER_FUNC_DICT = {
"email": get_email_creds,
"slack": get_slack_creds
}
HELPER_FUNC_DICT = {"email": get_email_creds, "slack": get_slack_creds}
16 changes: 5 additions & 11 deletions chaos_genius/alerts/alert_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,25 @@
"port": True,
"sender_email": True,
"server": True,
"username": False
"username": False,
}

SLACK_CONFIGS_EXPOSE = {
"webhook_url": False,
"channel_name": True
}
SLACK_CONFIGS_EXPOSE = {"webhook_url": False, "channel_name": True}

ORGANISATION_SETTINGS_EXPOSE = {
"account": True,
"metrics": True
}
ORGANISATION_SETTINGS_EXPOSE = {"account": True, "metrics": True}

ALERT_DIGEST_SETTINGS_EXPOSE = {
"active": True,
"daily_digest": True,
"scheduled_time": True,
"weekly_digest": True
"weekly_digest": True,
}

helper_objects = {
"email": EMAIL_CONFIGS_EXPOSE,
"slack": SLACK_CONFIGS_EXPOSE,
"organisation_settings": ORGANISATION_SETTINGS_EXPOSE,
"alert_digest_settings": ALERT_DIGEST_SETTINGS_EXPOSE
"alert_digest_settings": ALERT_DIGEST_SETTINGS_EXPOSE,
}

# TODO: Duplicate logic in the alert_cofig model too
Expand Down
30 changes: 0 additions & 30 deletions chaos_genius/alerts/anomaly_alert_config.py

This file was deleted.

Loading

0 comments on commit d64d025

Please sign in to comment.