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.

13.6 KB
Raw Blame
import hashlib
import datetime
from collections.abc import Callable
from typing import Any, Optional, cast
from urllib.parse import quote, urlencode

import pyarrow as pa
import structlog
from dateutil import parser
from structlog.types import FilteringBoundLogger

from products.warehouse_sources.sources.doit._config import DoItSourceConfig
from products.warehouse_sources.sources.doit.settings import DOIT_ENDPOINTS, DoItEndpoint
from products.warehouse_sources.sources.sdk import (
    DEFAULT_RETRY,
    ClientConfig,
    Endpoint,
    EndpointResource,
    IncrementalField,
    IncrementalFieldType,
    RESTAPIConfig,
    SourceResponse,
    build_chained_resource,
    frozen,
    make_tracked_session,
    rest_api_resource,
)
from products.warehouse_sources.sources.sdk.internals import NamingConvention, table_from_iterator

# DoIt's API sits behind Cloudflare, which returns 52x origin errors (e.g. 524 when the origin
# times out under load or during maintenance) instead of the standard 502/503/504. These are
# transient like the gateway errors the default policy already retries, so add the Cloudflare
# origin family to the forcelist and let the HTTP layer retry them with backoff.
DOIT_RETRY = DEFAULT_RETRY.new(status_forcelist=(*(DEFAULT_RETRY.status_forcelist or ()), 520, 521, 522, 523, 524))

# `make_tracked_session` leaves `timeout` unset, so an unbounded request holds a worker thread until
# the enclosing Temporal activity's start-to-close budget (24h full refresh, a week incremental)
# expires, with the schema stuck in "Running" the whole time — the liveness heartbeat beats from the
# activity's event loop, not the thread doing the request, so it can't catch this. Split connect from
# read: the report fetch needs a long read budget for wide date windows, but neither call should sit
# on a black-holed connect. Read timeouts are socket-inactivity, not total elapsed, so a slow but
# streaming report is never cut off.
DOIT_CONNECT_TIMEOUT_SECONDS = 10
LIST_REPORTS_TIMEOUT_SECONDS = (DOIT_CONNECT_TIMEOUT_SECONDS, 30)
REPORT_TIMEOUT_SECONDS = (DOIT_CONNECT_TIMEOUT_SECONDS, 300)

# Key under a schema's persisted `schema_metadata` holding the DoIt report id.
REPORT_ID_METADATA_KEY = "report_id"

DOIT_BASE_URL = "https://api.doit.com"
DOIT_REPORTS_URL = f"{DOIT_BASE_URL}/analytics/v1/reports"

# The listing endpoint paginates at 50 reports per page by default, signalling more pages via a
# `pageToken` in the response body. The cap is a safety valve against a server that keeps returning
# tokens forever; at 50 reports per page it allows 10,000 reports.
DOIT_REPORTS_MAX_PAGES = 200

DOIT_INCREMENTAL_FIELDS: list[IncrementalField] = [
    {
        "field": "timestamp",
        "field_type": IncrementalFieldType.Timestamp,
        "label": "timestamp",
        "type": IncrementalFieldType.Timestamp,
    }
]


def build_pyarrow_schema(schema: dict[str, str]) -> pa.Schema:
    fields: list[pa.Field] = []
    for name, type in schema.items():
        arrow_type: pa.DataType

        match type:
            case "string":
                arrow_type = pa.string()
            case "float":
                arrow_type = pa.float64()
            case "timestamp":
                arrow_type = pa.timestamp("s")
            case "number":
                arrow_type = pa.int32()
            case "integer":
                arrow_type = pa.int32()
            case "boolean":
                arrow_type = pa.bool_()
            case _:
                arrow_type = pa.string()

        fields.append(pa.field(name, arrow_type, nullable=True))

    return pa.schema(fields)


@frozen
class DoItReport:
    id: str
    # Normalized identifier used as the schema row name.
    name: str
    # Raw DoIt name, shown as the schema label so a rename stays visible in the UI.
    report_name: str


def doit_list_reports(config: DoItSourceConfig, logger: Optional[FilteringBoundLogger] = None) -> list[DoItReport]:
    if logger is None:
        logger = structlog.get_logger(__name__)

    session = make_tracked_session(retry=DOIT_RETRY)

    reports: list[dict[str, Any]] = []
    page_token: Optional[str] = None
    for _ in range(DOIT_REPORTS_MAX_PAGES):
        url = DOIT_REPORTS_URL
        if page_token:
            url = f"{DOIT_REPORTS_URL}?{urlencode({'pageToken': page_token})}"

        res = session.get(
            url,
            headers={"Authorization": f"Bearer {config.api_key}"},
            timeout=LIST_REPORTS_TIMEOUT_SECONDS,
        )

        # `DOIT_RETRY` sets `raise_on_status=False`, so a 5xx that outlives the retries lands here as
        # a normal response; without this guard it surfaces as a JSON/key error with the status code
        # lost.
        if res.status_code != 200:
            raise Exception(f"Request to list reports failed with status: {res.status_code}. With body: {res.text}")

        payload = res.json()
        reports.extend(payload.get("reports") or [])

        next_token = payload.get("pageToken")
        # Treat an echoed token as the last page so a misbehaving server can't loop us.
        if not next_token or next_token == page_token:
            break
        page_token = next_token
    else:
        logger.warning(
            "DoIt report listing hit the page cap; the report list may be truncated",
            max_pages=DOIT_REPORTS_MAX_PAGES,
            reports_seen=len(reports),
        )

    result = []
    for report in reports:
        report_name = report.get("reportName") or ""
        report_id = report.get("id", "unknown")
        if not report_name.strip():
            logger.warning("Skipping DoIt report with empty name", report_id=report_id)
            continue
        try:
            normalized = NamingConvention.normalize_identifier(report_name)
            result.append(DoItReport(id=report["id"], name=normalized, report_name=report_name))
        except ValueError:
            logger.warning("Skipping DoIt report with invalid name", report_id=report_id, report_name=report_name)
            continue

    return result


def resolve_report_id(
    config: DoItSourceConfig,
    schema_name: str,
    schema_metadata: Optional[dict[str, Any]],
    logger: Optional[FilteringBoundLogger] = None,
) -> str:
    # The report id is stamped into schema_metadata at schema creation, so a later rename of the
    # report in DoIt keeps syncing into the same warehouse table. The re-listing below is only a
    # safety net for schemas persisted without metadata.
    if schema_metadata and schema_metadata.get(REPORT_ID_METADATA_KEY):
        return str(schema_metadata[REPORT_ID_METADATA_KEY])

    matches = [report.id for report in doit_list_reports(config, logger=logger) if report.name == schema_name]
    if not matches:
        raise Exception("Report no longer exists")
    return matches[0]


def append_primary_key(row: dict[str, Any]) -> dict[str, Any]:
    columns_to_ignore = ["timestamp", "cost"]
    key = ""
    for name, value in row.items():
        if name not in columns_to_ignore:
            key = f"{key}-{value}"

    # this hash has no security impact
    # nosemgrep: python.lang.security.insecure-hash-algorithms-md5.insecure-hash-algorithm-md5
    hash_key = hashlib.md5(key.encode()).hexdigest()

    return {**row, "id": hash_key}


# NOTE: This source intentionally remains a SimpleSource and is not a candidate for ResumableSource.
# `doit_source` does a small fixed request flow: a single fetch of the resolved report for the
# requested date window, which returns that whole window in one response — there is no pagination
# loop, next-URL, continuation token, parent/child fanout, or other multi-batch checkpoint to
# persist mid-sync.
# For incremental runs, `db_incremental_field_last_value` already determines `startDate` when
# `should_use_incremental_field` is enabled. For full-refresh runs, there is no resumable progress
# marker today; a retry simply restarts the same full request. If DoIt exposes a paginated reports
# API in the future, or we explicitly chunk the `startDate`/`endDate` window, revisit this
# decision.
def doit_source(
    config: DoItSourceConfig,
    report_name: str,
    report_id: str,
    logger: FilteringBoundLogger,
    db_incremental_field_last_value: Optional[Any],
    should_use_incremental_field: bool = False,
) -> SourceResponse:
    def get_rows(report_id: str):
        request_uri = f"https://api.doit.com/analytics/v1/reports/{report_id}"

        if should_use_incremental_field and db_incremental_field_last_value is not None:
            if isinstance(db_incremental_field_last_value, datetime.date):
                start = db_incremental_field_last_value.strftime("%Y-%m-%d")
            elif isinstance(db_incremental_field_last_value, datetime.datetime):
                start = db_incremental_field_last_value.strftime("%Y-%m-%d")
            elif isinstance(db_incremental_field_last_value, str):
                date = parser.parse(db_incremental_field_last_value)
                start = date.strftime("%Y-%m-%d")
            else:
                raise Exception(
                    f"DoIt incremental type not recognised: {db_incremental_field_last_value.__class__.__name__}"
                )

            end = datetime.datetime.now().strftime("%Y-%m-%d")

            request_uri = f"{request_uri}?startDate={start}&endDate={end}"

        logger.debug(f"Requesting DoIt url: {request_uri}")

        res = make_tracked_session(retry=DOIT_RETRY).get(
            request_uri,
            headers={"Authorization": f"Bearer {config.api_key}"},
            timeout=REPORT_TIMEOUT_SECONDS,
        )

        if res.status_code != 200:
            raise Exception(f"Request to get report failed with status: {res.status_code}. With body: {res.text}")

        result = res.json()

        schema: list[dict[str, str]] = result["result"]["schema"]
        column_names = [column["name"] for column in schema]
        column_types_dict = {column["name"]: column["type"] for column in schema}
        arrow_schema = build_pyarrow_schema(column_types_dict)

        rows: list[list[Any]] = result["result"]["rows"]

        if "id" not in arrow_schema.names:
            arrow_schema = arrow_schema.append(pa.field("id", pa.string(), nullable=False))

        yield table_from_iterator((append_primary_key(dict(zip(column_names, row))) for row in rows), arrow_schema)

    return SourceResponse(name=report_name, items=lambda: get_rows(report_id), primary_keys=["id"])


def _endpoint_resource(name: str, settings: DoItEndpoint) -> EndpointResource:
    return {
        "name": name,
        "table_name": name.lower(),
        "write_disposition": "replace",
        "endpoint": {
            "path": settings.path,
            "params": dict(settings.params),
            "data_selector": settings.data_selector,
            "data_selector_required": True,
            "paginator": {
                "type": "cursor",
                "cursor_path": settings.cursor_path,
                "cursor_param": "pageToken",
                "param_location": "query",
                "raise_on_repeated_cursor": True,
            },
        },
        "table_format": "delta",
    }


def _path_segment_field(field: str) -> str:
    return f"{field}_path_segment"


def _add_path_segments(fields: list[str]) -> Callable[[dict[str, Any]], dict[str, Any]]:
    # The fan-out binds parent values into the path with no escaping, and DoiT insight keys are free
    # strings that can hold `/`, `?` or `#`. The raw values still go into the child columns.
    def _mapper(row: dict[str, Any]) -> dict[str, Any]:
        for field in fields:
            if row.get(field) is not None:
                row[_path_segment_field(field)] = quote(str(row[field]), safe="")
        return row

    return _mapper


def doit_endpoint_source(config: DoItSourceConfig, endpoint: str, team_id: int, job_id: str) -> SourceResponse:
    settings = DOIT_ENDPOINTS[endpoint]
    client_config: ClientConfig = {
        "base_url": DOIT_BASE_URL,
        "auth": {"type": "bearer", "token": config.api_key},
    }

    if settings.parent is None:
        rest_config: RESTAPIConfig = {
            "client": client_config,
            "resource_defaults": {},
            "resources": [_endpoint_resource(endpoint, settings)],
        }
        resource = rest_api_resource(rest_config, team_id, job_id, None)
        return SourceResponse(
            name=endpoint,
            items=lambda: resource,
            primary_keys=settings.primary_keys,
            column_hints=resource.column_hints,
        )

    parent = settings.parent
    child = _endpoint_resource(endpoint, settings)
    child["include_from_parent"] = list(parent.include_fields)
    child_endpoint = cast(Endpoint, child["endpoint"])
    child_endpoint["params"] = {
        **(child_endpoint.get("params") or {}),
        **{
            param: {"type": "resolve", "resource": parent.name, "field": _path_segment_field(field)}
            for param, field in parent.resolve_fields.items()
        },
    }
    # An insight that DoiT drops between the insights listing and this fetch must not sink the run.
    child_endpoint["response_actions"] = [{"status_code": 404, "action": "ignore"}]

    parent_resource = _endpoint_resource(parent.name, DOIT_ENDPOINTS[parent.name])
    parent_resource["data_map"] = _add_path_segments(list(parent.resolve_fields.values()))

    return SourceResponse(
        name=endpoint,
        items=lambda: build_chained_resource(
            resources=[parent_resource, child],
            child_name=endpoint,
            parent_name=parent.name,
            parent_field_renames=parent.include_fields,
            client_config=client_config,
            team_id=team_id,
            job_id=job_id,
        ),
        primary_keys=settings.primary_keys,
    )