Skip to content

: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.

4.9 KB
Raw Blame
import json
from datetime import timedelta

import structlog
import temporalio
import posthoganalytics
from temporalio import workflow
from temporalio.common import RetryPolicy

from products.signals.backend.temporal.reingestion import (
    DeleteReportInput,
    SoftDeleteReportSignalsInput,
    delete_report_activity,
    soft_delete_report_signals_activity,
)
from products.signals.backend.temporal.signal_queries import (
    FetchSignalsForReportInput,
    FetchSignalsForReportOutput,
    WaitForClickHouseInput,
    WaitForClickHouseMode,
    WaitForClickHouseSignal,
    fetch_signals_for_report_activity,
    wait_for_signal_in_clickhouse_activity,
)
from products.signals.backend.temporal.types import SignalReportDeletionWorkflowInputs

logger = structlog.get_logger(__name__)


@temporalio.workflow.defn(name="signal-report-deletion")
class SignalReportDeletionWorkflow:
    """
    Workflow that soft-deletes a report and its signals.

    Flow:
    1. Fetch all signals for the report from ClickHouse
    2. Soft-delete all signals in ClickHouse (re-emit with metadata.deleted=True)
    2b. Wait for the last soft-deleted signal to land in ClickHouse
    3. Delete the report in Postgres (transition to DELETED)
    """

    @staticmethod
    def parse_inputs(inputs: list[str]) -> SignalReportDeletionWorkflowInputs:
        loaded = json.loads(inputs[0])
        return SignalReportDeletionWorkflowInputs(**loaded)

    @staticmethod
    def workflow_id_for(team_id: int, report_id: str) -> str:
        return f"signal-report-deletion-{team_id}-{report_id}"

    @temporalio.workflow.run
    async def run(self, inputs: SignalReportDeletionWorkflowInputs) -> None:
        with posthoganalytics.new_context(capture_exceptions=False):
            posthoganalytics.tag("team_id", inputs.team_id)
            posthoganalytics.tag("report_id", inputs.report_id)
            posthoganalytics.tag("product", "signals")
            await self._run_impl(inputs)

    async def _run_impl(self, inputs: SignalReportDeletionWorkflowInputs) -> None:
        # Bind team_id + report_id so all logs flow to the log_entries sink (the Temporal
        # structlog renderer skips producing when team_id isn't in the event dict).
        log = logger.bind(team_id=inputs.team_id, report_id=inputs.report_id)
        # 1. Fetch all signals for the report from ClickHouse
        fetch_result: FetchSignalsForReportOutput = await workflow.execute_activity(
            fetch_signals_for_report_activity,
            FetchSignalsForReportInput(team_id=inputs.team_id, report_id=inputs.report_id),
            start_to_close_timeout=timedelta(minutes=5),
            retry_policy=RetryPolicy(maximum_attempts=3),
        )

        if not fetch_result.signals:
            log.warning("No signals found for report, deleting report only")
            await workflow.execute_activity(
                delete_report_activity,
                DeleteReportInput(team_id=inputs.team_id, report_id=inputs.report_id),
                start_to_close_timeout=timedelta(minutes=1),
                retry_policy=RetryPolicy(maximum_attempts=3),
            )
            return

        # 2. Soft-delete all signals in ClickHouse
        await workflow.execute_activity(
            soft_delete_report_signals_activity,
            SoftDeleteReportSignalsInput(team_id=inputs.team_id, report_id=inputs.report_id),
            start_to_close_timeout=timedelta(minutes=5),
            retry_policy=RetryPolicy(maximum_attempts=3),
        )

        # 2b. Wait for all soft-deleted signals to land in ClickHouse
        await workflow.execute_activity(
            wait_for_signal_in_clickhouse_activity,
            WaitForClickHouseInput(
                team_id=inputs.team_id,
                signals=[
                    WaitForClickHouseSignal(
                        signal_id=s.signal_id,
                        timestamp=s.timestamp,
                        inserted_at=s.inserted_at,
                    )
                    for s in fetch_result.signals
                ],
                max_wait_time_seconds=3600,
                # The report is soft-deleted either way; briefly-stale signal rows in
                # semantic search are acceptable, so don't burn ClickHouse queries here.
                mode=WaitForClickHouseMode.OPTIMISTIC,
            ),
            start_to_close_timeout=timedelta(hours=1, minutes=5),
            heartbeat_timeout=timedelta(minutes=2),
            retry_policy=RetryPolicy(maximum_attempts=2),
        )

        # 3. Delete the report in Postgres
        await workflow.execute_activity(
            delete_report_activity,
            DeleteReportInput(team_id=inputs.team_id, report_id=inputs.report_id),
            start_to_close_timeout=timedelta(minutes=1),
            retry_policy=RetryPolicy(maximum_attempts=3),
        )

        log.info(
            "Deletion complete for report: signals soft-deleted",
            signal_count=len(fetch_result.signals),
        )