2023-03-13 18:19:22 -03:00
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
from celery.utils.log import get_task_logger
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
|
|
from apps.alerts.models import AlertGroup, AlertGroupLogRecord
|
|
|
|
|
from apps.webhooks.models import Webhook
|
|
|
|
|
from common.custom_celery_tasks import shared_dedicated_queue_retry_task
|
|
|
|
|
|
|
|
|
|
from .trigger_webhook import send_webhook_event
|
|
|
|
|
|
|
|
|
|
logger = get_task_logger(__name__)
|
|
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MAX_RETRIES = 10
|
|
|
|
|
|
2023-03-14 14:21:46 -03:00
|
|
|
ACTION_TO_TRIGGER_TYPE = {
|
|
|
|
|
AlertGroupLogRecord.TYPE_ACK: Webhook.TRIGGER_ACKNOWLEDGE,
|
|
|
|
|
AlertGroupLogRecord.TYPE_RESOLVED: Webhook.TRIGGER_RESOLVE,
|
|
|
|
|
AlertGroupLogRecord.TYPE_SILENCE: Webhook.TRIGGER_SILENCE,
|
|
|
|
|
AlertGroupLogRecord.TYPE_UN_SILENCE: Webhook.TRIGGER_UNSILENCE,
|
|
|
|
|
AlertGroupLogRecord.TYPE_UN_RESOLVED: Webhook.TRIGGER_UNRESOLVE,
|
2023-04-18 13:03:33 -06:00
|
|
|
AlertGroupLogRecord.TYPE_UN_ACK: Webhook.TRIGGER_UNACKNOWLEDGE,
|
2023-03-14 14:21:46 -03:00
|
|
|
}
|
|
|
|
|
|
2023-03-13 18:19:22 -03:00
|
|
|
|
|
|
|
|
@shared_dedicated_queue_retry_task(
|
|
|
|
|
bind=True, autoretry_for=(Exception,), retry_backoff=True, max_retries=1 if settings.DEBUG else MAX_RETRIES
|
|
|
|
|
)
|
2024-04-08 11:25:48 -03:00
|
|
|
def alert_group_created(self, alert_group_id, is_backsync=False):
|
2023-03-13 18:19:22 -03:00
|
|
|
try:
|
2023-07-18 13:48:34 +02:00
|
|
|
alert_group = AlertGroup.objects.get(pk=alert_group_id)
|
2023-03-13 18:19:22 -03:00
|
|
|
except AlertGroup.DoesNotExist:
|
|
|
|
|
return
|
|
|
|
|
|
2023-04-26 15:55:08 -06:00
|
|
|
trigger_type = Webhook.TRIGGER_ALERT_GROUP_CREATED
|
2023-03-13 18:19:22 -03:00
|
|
|
organization_id = alert_group.channel.organization_id
|
2023-05-09 06:59:01 -06:00
|
|
|
webhooks = Webhook.objects.filter(trigger_type=trigger_type, organization_id=organization_id)
|
2023-03-14 14:21:46 -03:00
|
|
|
|
2024-04-08 11:25:48 -03:00
|
|
|
if is_backsync:
|
|
|
|
|
# only consider non-connected integration webhooks for backsync events
|
|
|
|
|
webhooks = webhooks.filter(is_from_connected_integration=False)
|
|
|
|
|
|
2023-03-14 14:21:46 -03:00
|
|
|
# check if there are any webhooks before going on
|
|
|
|
|
if not webhooks:
|
|
|
|
|
return
|
|
|
|
|
|
2024-04-08 11:25:48 -03:00
|
|
|
send_webhook_event.apply_async(
|
|
|
|
|
(trigger_type, alert_group_id),
|
|
|
|
|
kwargs={"organization_id": organization_id, "is_backsync": is_backsync},
|
|
|
|
|
)
|
2023-03-13 18:19:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_dedicated_queue_retry_task(
|
|
|
|
|
bind=True, autoretry_for=(Exception,), retry_backoff=True, max_retries=1 if settings.DEBUG else MAX_RETRIES
|
|
|
|
|
)
|
2024-04-08 11:25:48 -03:00
|
|
|
def alert_group_status_change(self, action_type, alert_group_id, user_id, is_backsync=False):
|
2023-03-13 18:19:22 -03:00
|
|
|
try:
|
2023-07-18 13:48:34 +02:00
|
|
|
alert_group = AlertGroup.objects.get(pk=alert_group_id)
|
2023-06-12 18:50:33 +02:00
|
|
|
except AlertGroup.DoesNotExist:
|
2023-03-13 18:19:22 -03:00
|
|
|
return
|
|
|
|
|
|
2023-03-14 14:21:46 -03:00
|
|
|
trigger_type = ACTION_TO_TRIGGER_TYPE.get(action_type)
|
|
|
|
|
if trigger_type is None:
|
2023-03-13 18:19:22 -03:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
organization_id = alert_group.channel.organization_id
|
2024-02-19 14:12:56 -03:00
|
|
|
webhooks = Webhook.objects.filter(
|
|
|
|
|
trigger_type=trigger_type, organization_id=organization_id
|
|
|
|
|
) | Webhook.objects.filter(trigger_type=Webhook.TRIGGER_STATUS_CHANGE, organization_id=organization_id)
|
2023-03-14 14:21:46 -03:00
|
|
|
|
2024-04-08 11:25:48 -03:00
|
|
|
if is_backsync:
|
|
|
|
|
# only consider non-connected integration webhooks for backsync events
|
|
|
|
|
webhooks = webhooks.filter(is_from_connected_integration=False)
|
|
|
|
|
|
2023-03-14 14:21:46 -03:00
|
|
|
# check if there are any webhooks before going on
|
|
|
|
|
if not webhooks:
|
|
|
|
|
return
|
|
|
|
|
|
2023-03-13 18:19:22 -03:00
|
|
|
send_webhook_event.apply_async(
|
2023-03-14 14:21:46 -03:00
|
|
|
(trigger_type, alert_group_id),
|
2024-04-08 11:25:48 -03:00
|
|
|
kwargs={"organization_id": organization_id, "user_id": user_id, "is_backsync": is_backsync},
|
2023-03-13 18:19:22 -03:00
|
|
|
)
|