:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
from typing import Any
from django.db import DatabaseError
import structlog
from celery import shared_task
from celery.app.task import Task
from posthog.celery_queues import CeleryQueue
from products.event_definitions.backend.logic.placeholder import (
PlaceholderEventDefinition,
create_placeholder_event_definitions,
)
from products.wizard.backend.facade.enums import WizardSessionRunPhase
from products.wizard.backend.models import WizardSession
logger = structlog.get_logger(__name__)
MAX_PLANNED_EVENT_DEFINITIONS = 50
MAX_PLANNED_EVENT_CANDIDATES = 500
MAX_EVENT_NAME_LENGTH = 400
def _planned_event_definitions(event_plan: dict[str, Any] | None) -> list[PlaceholderEventDefinition]:
events = event_plan.get("events") if isinstance(event_plan, dict) else None
if not isinstance(events, list):
return []
definitions: list[PlaceholderEventDefinition] = []
seen_names: set[str] = set()
for planned_event in events[:MAX_PLANNED_EVENT_CANDIDATES]:
if not isinstance(planned_event, dict):
continue
raw_name = planned_event.get("name")
if not isinstance(raw_name, str):
continue
name = raw_name.strip().replace("\x00", "\ufffd")
if not name or name.startswith("$") or len(name) > MAX_EVENT_NAME_LENGTH or name in seen_names:
continue
description = planned_event.get("description")
definitions.append(
PlaceholderEventDefinition(
name=name,
description=description.replace("\x00", "\ufffd") if isinstance(description, str) else None,
)
)
seen_names.add(name)
if len(definitions) == MAX_PLANNED_EVENT_DEFINITIONS:
break
return definitions
@shared_task(
bind=True,
ignore_result=True,
max_retries=5,
queue=CeleryQueue.DEFAULT.value,
)
def sync_wizard_event_definitions(self: Task, team_id: int, session_id: str) -> None:
try:
session = WizardSession.objects.for_team(team_id).filter(session_id=session_id).first()
if session is None or session.run_phase != WizardSessionRunPhase.COMPLETED.value:
return
create_placeholder_event_definitions(
team_id=team_id,
definitions=_planned_event_definitions(session.event_plan),
)
except DatabaseError as error:
countdown = min(2**self.request.retries, 60)
logger.warning(
"Retrying wizard event definition sync",
team_id=team_id,
session_id=session_id,
retry=self.request.retries,
)
raise self.retry(exc=error, countdown=countdown)