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.

11.8 KB
Raw Blame
import time
from collections.abc import Callable, Iterator
from datetime import UTC, date, datetime, timedelta
from typing import Any, Optional
from urllib.parse import quote, urlencode

import requests
from structlog.types import FilteringBoundLogger
from tenacity import retry_if_exception_type, stop_after_attempt, wait_exponential_jitter

from products.warehouse_sources.sources.clari.settings import (
    ACTIVITY_INITIAL_LOOKBACK_DAYS,
    ACTIVITY_TYPES,
    CLARI_BASE_URL,
)
from products.warehouse_sources.sources.sdk import (
    ResumableSourceManager,
    SourceResponse,
    frozen,
    make_tracked_session,
    retry_with_deadline,
)

REQUEST_TIMEOUT_SECONDS = 120
# ~10 req/s general limit; back off on 429.
MAX_RETRY_ATTEMPTS = 5
AUDIT_PAGE_LIMIT = 1000
# Export jobs usually finish in minutes; the per-attempt poll budget is capped
# so a stuck job surfaces as a retryable error instead of hanging the activity.
EXPORT_POLL_INTERVAL_SECONDS = 15
EXPORT_POLL_MAX_ATTEMPTS = 60


class ClariRetryableError(Exception):
    pass


@frozen
class ClariResumeConfig:
    # Forecast and activity: the in-flight export job to re-poll instead of
    # creating a new one (exports are quota-limited). Audit events: the
    # nextLink URL of the next unfetched page.
    job_id: Optional[str] = None
    next_link: Optional[str] = None


def _get_session(api_key: str) -> requests.Session:
    return make_tracked_session(headers={"apikey": api_key}, redact_values=(api_key,))


def _format_timestamp(value: Any) -> str:
    if isinstance(value, datetime):
        dt = value if value.tzinfo else value.replace(tzinfo=UTC)
        return dt.astimezone(UTC).strftime("%Y-%m-%dT%H:%M:%SZ")
    if isinstance(value, date):
        return value.strftime("%Y-%m-%dT00:00:00Z")
    return str(value)


def _to_datetime(value: Any) -> Optional[datetime]:
    """Coerce an incremental watermark to UTC. Activity `date` values are epoch milliseconds."""
    if isinstance(value, datetime):
        return value if value.tzinfo else value.replace(tzinfo=UTC)
    if isinstance(value, date):
        return datetime(value.year, value.month, value.day, tzinfo=UTC)
    if isinstance(value, int | float):
        return datetime.fromtimestamp(value / 1000, tz=UTC)
    if isinstance(value, str):
        try:
            parsed = datetime.fromisoformat(value.replace("Z", "+00:00"))
        except ValueError:
            return None
        return parsed if parsed.tzinfo else parsed.replace(tzinfo=UTC)
    return None


def validate_credentials(api_key: str) -> bool:
    """Confirm the API key works with a cheap audit-events probe."""
    try:
        response = _get_session(api_key).get(
            f"{CLARI_BASE_URL}/audit/events?limit=1",
            timeout=15,
        )
        return response.status_code == 200
    except Exception:
        return False


def _make_fetcher(session: requests.Session, logger: FilteringBoundLogger):
    @retry_with_deadline(
        retry=retry_if_exception_type((ClariRetryableError, requests.ReadTimeout, requests.ConnectionError)),
        stop=stop_after_attempt(MAX_RETRY_ATTEMPTS),
        wait=wait_exponential_jitter(initial=2, max=90),
        reraise=True,
    )
    def fetch(method: str, url: str, json_body: Optional[dict[str, Any]] = None) -> requests.Response:
        response = session.request(method, url, json=json_body, timeout=REQUEST_TIMEOUT_SECONDS)

        if response.status_code == 429 or response.status_code >= 500:
            raise ClariRetryableError(f"Clari API error (retryable): status={response.status_code}, url={url}")

        if not response.ok:
            logger.error(f"Clari API error: status={response.status_code}, body={response.text[:500]}, url={url}")
            response.raise_for_status()

        return response

    return fetch


def get_audit_events(
    api_key: str,
    logger: FilteringBoundLogger,
    resumable_source_manager: ResumableSourceManager[ClariResumeConfig],
    should_use_incremental_field: bool = False,
    db_incremental_field_last_value: Any = None,
) -> Iterator[list[dict[str, Any]]]:
    session = _get_session(api_key)
    fetch = _make_fetcher(session, logger)

    resume_config = resumable_source_manager.load_state() if resumable_source_manager.can_resume() else None
    if resume_config is not None and resume_config.next_link:
        url: Optional[str] = resume_config.next_link
        logger.debug("Clari: resuming audit_events from saved nextLink")
    else:
        params: dict[str, Any] = {"limit": AUDIT_PAGE_LIMIT}
        if should_use_incremental_field and db_incremental_field_last_value is not None:
            params["dateFrom"] = _format_timestamp(db_incremental_field_last_value)
        url = f"{CLARI_BASE_URL}/audit/events?{urlencode(params)}"

    while url:
        data = fetch("GET", url).json()
        items = data.get("items") or data.get("activities") or []

        if items:
            yield items

        url = data.get("nextLink") or None
        if url:
            # Save state AFTER yielding so a crash re-yields the in-flight page.
            resumable_source_manager.save_state(ClariResumeConfig(next_link=url))


def _extract_result_rows(data: Any) -> list[dict[str, Any]]:
    if isinstance(data, list):
        return [row for row in data if isinstance(row, dict)]
    if isinstance(data, dict):
        for key in ("data", "rows", "records", "results", "activities"):
            value = data.get(key)
            if isinstance(value, list):
                return [row for row in value if isinstance(row, dict)]
        return [data]
    return []


def _run_export_job(
    fetch: Callable[..., requests.Response],
    logger: FilteringBoundLogger,
    resumable_source_manager: ResumableSourceManager[ClariResumeConfig],
    create_url: str,
    create_body: dict[str, Any],
) -> Any:
    resume_config = resumable_source_manager.load_state() if resumable_source_manager.can_resume() else None
    job_id = resume_config.job_id if resume_config is not None else None
    if job_id:
        logger.debug(f"Clari: re-polling existing export job {job_id}")

    if not job_id:
        created = fetch("POST", create_url, json_body=create_body).json()
        job_id = created.get("jobId") or (created.get("job") or {}).get("id")
        if not job_id:
            raise ValueError(f"Clari export job creation returned no jobId: {created}")
        # Persist immediately — exports are quota-limited, so a retried
        # activity must re-poll this job rather than create another.
        with resumable_source_manager.committing():
            resumable_source_manager.save_state(ClariResumeConfig(job_id=job_id))

    status = None
    for _attempt in range(EXPORT_POLL_MAX_ATTEMPTS):
        job_body = fetch("GET", f"{CLARI_BASE_URL}/export/jobs/{quote(job_id)}").json()
        job = job_body.get("job") if isinstance(job_body.get("job"), dict) else job_body
        status = job["status"]

        if status == "DONE":
            break
        if status in ("FAILED", "CANCELLED", "ABORTED"):
            # Don't re-poll a dead job on retry.
            with resumable_source_manager.committing():
                resumable_source_manager.save_state(ClariResumeConfig(job_id=None))
            raise ValueError(f"Clari export job {job_id} ended with status {status}")

        time.sleep(EXPORT_POLL_INTERVAL_SECONDS)
    else:
        raise ClariRetryableError(f"Clari export job {job_id} still {status} after polling budget")

    return fetch("GET", f"{CLARI_BASE_URL}/export/jobs/{quote(job_id)}/results").json()


def get_forecast(
    api_key: str,
    forecast_id: str,
    logger: FilteringBoundLogger,
    resumable_source_manager: ResumableSourceManager[ClariResumeConfig],
) -> Iterator[list[dict[str, Any]]]:
    session = _get_session(api_key)
    fetch = _make_fetcher(session, logger)

    results = _run_export_job(
        fetch,
        logger,
        resumable_source_manager,
        create_url=f"{CLARI_BASE_URL}/export/forecast/{quote(forecast_id)}",
        create_body={"exportFormat": "JSON"},
    )
    rows = _extract_result_rows(results)
    if rows:
        yield rows


def get_activity(
    api_key: str,
    logger: FilteringBoundLogger,
    resumable_source_manager: ResumableSourceManager[ClariResumeConfig],
    should_use_incremental_field: bool = False,
    db_incremental_field_last_value: Any = None,
) -> Iterator[list[dict[str, Any]]]:
    session = _get_session(api_key)
    fetch = _make_fetcher(session, logger)

    end_date = datetime.now(tz=UTC)
    start_date = _to_datetime(db_incremental_field_last_value) if should_use_incremental_field else None
    if start_date is None:
        start_date = end_date - timedelta(days=ACTIVITY_INITIAL_LOOKBACK_DAYS)

    results = _run_export_job(
        fetch,
        logger,
        resumable_source_manager,
        create_url=f"{CLARI_BASE_URL}/export/activity",
        create_body={
            "activityTypes": list(ACTIVITY_TYPES),
            # The export reads the date and ignores the time, so the window is day-granular
            # and an incremental run re-exports the watermark's own day. Merge dedupes it.
            "startDate": _format_timestamp(start_date),
            "endDate": _format_timestamp(end_date),
            "exportFormat": "JSON",
        },
    )
    rows = _extract_result_rows(results)
    if rows:
        yield rows


def clari_source(
    api_key: str,
    forecast_id: str,
    endpoint: str,
    logger: FilteringBoundLogger,
    resumable_source_manager: ResumableSourceManager[ClariResumeConfig],
    should_use_incremental_field: bool = False,
    db_incremental_field_last_value: Optional[Any] = None,
) -> SourceResponse:
    if endpoint == "activity":
        return SourceResponse(
            name=endpoint,
            items=lambda: get_activity(
                api_key=api_key,
                logger=logger,
                resumable_source_manager=resumable_source_manager,
                should_use_incremental_field=should_use_incremental_field,
                db_incremental_field_last_value=db_incremental_field_last_value,
            ),
            primary_keys=["activityId"],
            partition_count=1,
            partition_size=1,
            # Export ordering is undocumented, so only commit the watermark
            # once a sync completes.
            sort_mode="desc",
        )

    if endpoint == "audit_events":
        return SourceResponse(
            name=endpoint,
            items=lambda: get_audit_events(
                api_key=api_key,
                logger=logger,
                resumable_source_manager=resumable_source_manager,
                should_use_incremental_field=should_use_incremental_field,
                db_incremental_field_last_value=db_incremental_field_last_value,
            ),
            # Audit events carry no unique id — dedupe on the natural composite. This composite can
            # still collide, but that's expected, not a data-quality problem the user can fix, so
            # don't set has_duplicate_primary_keys: that flag tells validate_incremental_sync to
            # block incremental syncing altogether. The merge's own per-batch dedup
            # (keep-last-per-key) already resolves the collision safely.
            primary_keys=["eventTimestamp", "actorId", "sessionId", "event"],
            partition_count=1,
            partition_size=1,
            # Result ordering is undocumented, so only commit the watermark
            # once a sync completes.
            sort_mode="desc",
        )

    return SourceResponse(
        name=endpoint,
        items=lambda: get_forecast(
            api_key=api_key,
            forecast_id=forecast_id,
            logger=logger,
            resumable_source_manager=resumable_source_manager,
        ),
        # Snapshot-style export rows have no identifier; the table fully
        # refreshes each sync.
        primary_keys=None,
        partition_count=1,
        partition_size=1,
    )