def create(self, site_url: Optional[str] = None, *args: Any, **kwargs: Any): with transaction.atomic(): if kwargs.get("elements"): if kwargs.get("team"): kwargs["elements_hash"] = ElementGroup.objects.create( team=kwargs["team"], elements=kwargs.pop("elements")).hash else: kwargs["elements_hash"] = ElementGroup.objects.create( team_id=kwargs["team_id"], elements=kwargs.pop("elements")).hash event = super().create(*args, **kwargs) should_post_to_slack = False relations = [] for action in event.actions: relations.append( action.events.through(action_id=action.pk, event=event)) if action.post_to_slack: should_post_to_slack = True Action.events.through.objects.bulk_create(relations, ignore_conflicts=True) if (should_post_to_slack and event.team and event.team.slack_incoming_webhook): post_event_to_slack.delay(event.id, site_url) return event
def create(self, site_url: Optional[str] = None, *args: Any, **kwargs: Any): with transaction.atomic(): if kwargs.get("elements"): if kwargs.get("team"): kwargs["elements_hash"] = ElementGroup.objects.create( team=kwargs["team"], elements=kwargs.pop("elements") ).hash else: kwargs["elements_hash"] = ElementGroup.objects.create( team_id=kwargs["team_id"], elements=kwargs.pop("elements") ).hash event = super().create(*args, **kwargs) # Matching actions to events can get very expensive to do as events are streaming in # In a few cases we have had it OOM Postgres with the query it is running # Short term solution is to have this be configurable to be run in batch if not settings.ASYNC_EVENT_ACTION_MAPPING: should_post_to_slack = False relations = [] for action in event.actions: relations.append(action.events.through(action_id=action.pk, event_id=event.pk)) if action.post_to_slack: should_post_to_slack = True Action.events.through.objects.bulk_create(relations, ignore_conflicts=True) team = kwargs.get("team", event.team) if should_post_to_slack and team and team.slack_incoming_webhook: post_event_to_slack.delay(event.pk, site_url) return event