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.

8.7 KB
Raw Blame
import dataclasses
from collections.abc import Iterable
from typing import Any, Optional

from products.warehouse_sources.sources.octolens.settings import OCTOLENS_ENDPOINTS, PAGE_SIZE, OctolensEndpointConfig
from products.warehouse_sources.sources.sdk import (
    BasePaginator,
    ClientConfig,
    Endpoint,
    EndpointResource,
    JSONResponseCursorPaginator,
    RESTAPIConfig,
    ResumableSourceManager,
    SinglePagePaginator,
    SourceResponse,
    build_dependent_resource,
    make_tracked_session,
    rest_api_resource,
)

OCTOLENS_BASE_URL = "https://app.octolens.com"
# Cheap credential introspection endpoint: returns the workspace the key is bound to plus its
# effective scopes. Every read-capable key can call it, so it validates the key without needing
# access to any particular table.
AUTH_PROBE_PATH = "/auth"


@dataclasses.dataclass(frozen=True)
class OctolensResumeConfig:
    cursor: str = ""
    # Fan-out progress (completed parents, current parent, child page) for the search_results table.
    fanout_state: Optional[dict[str, Any]] = None


def _api_path(api_version: str, path: str) -> str:
    return f"/api/{api_version}{path}"


def _mentions_body() -> dict[str, Any]:
    # No `filters.startDate`: it filters on the mention's post time, which says nothing about when
    # Octolens collected or last changed the row, so watermarking on it would drop late-collected
    # historical mentions and every later edit to an already-synced row. See settings.py.
    return {
        "limit": PAGE_SIZE,
        # Defaults to false, which silently drops low-relevance mentions. A warehouse copy should
        # mirror everything Octolens collected, so opt in.
        "includeAll": True,
    }


def _paginator(config: OctolensEndpointConfig) -> BasePaginator:
    if not config.paginated:
        return SinglePagePaginator()
    # On the POST mentions feed the cursor rides in the body alongside the filters, not the query string.
    return JSONResponseCursorPaginator(
        cursor_path="pagination.nextCursor",
        cursor_param="cursor",
        param_location="json" if config.method == "post" else "query",
    )


def _resource(config: OctolensEndpointConfig, api_version: str) -> EndpointResource:
    endpoint: Endpoint = {
        "path": _api_path(api_version, config.path),
        "method": config.method,
        "data_selector": config.data_selector,
        "paginator": _paginator(config),
    }
    if config.method == "post":
        endpoint["json"] = _mentions_body()
    elif config.paginated:
        endpoint["params"] = {"limit": PAGE_SIZE}

    return {
        "name": config.name,
        "table_name": config.name,
        "table_format": "delta",
        # Every Octolens table is full refresh — the API exposes no update-aware cursor.
        "write_disposition": "replace",
        "endpoint": endpoint,
    }


def _client_config(api_key: str) -> ClientConfig:
    return {
        "base_url": OCTOLENS_BASE_URL,
        "headers": {"Content-Type": "application/json", "Accept": "application/json"},
        "auth": {"type": "bearer", "token": api_key},
    }


def _dependent_resource(
    config: OctolensEndpointConfig,
    api_key: str,
    api_version: str,
    team_id: int,
    job_id: str,
    resumable_source_manager: ResumableSourceManager[OctolensResumeConfig],
) -> Iterable[Any]:
    assert config.fanout is not None
    parent_config = OCTOLENS_ENDPOINTS[config.fanout.parent_name]
    versioned_endpoints = {
        name: dataclasses.replace(endpoint, path=_api_path(api_version, endpoint.path))
        for name, endpoint in OCTOLENS_ENDPOINTS.items()
    }

    initial_paginator_state: Optional[dict[str, Any]] = None
    if resumable_source_manager.can_resume():
        resume = resumable_source_manager.load_state()
        if resume is not None:
            initial_paginator_state = resume.fanout_state

    def save_checkpoint(state: Optional[dict[str, Any]]) -> None:
        if state:
            resumable_source_manager.save_state(OctolensResumeConfig(fanout_state=dict(state)))

    return build_dependent_resource(
        endpoint_configs=versioned_endpoints,
        child_endpoint=config.name,
        fanout=config.fanout,
        client_config=_client_config(api_key),
        path_format_values={},
        team_id=team_id,
        job_id=job_id,
        db_incremental_field_last_value=None,
        # Only the searches parent takes a page size, so it rides in its `parent_params`.
        page_size_param=None,
        parent_endpoint_extra={
            "paginator": _paginator(parent_config),
            "data_selector": parent_config.data_selector,
        },
        child_endpoint_extra={
            "paginator": SinglePagePaginator(),
            "data_selector": config.data_selector,
        },
        resume_hook=save_checkpoint,
        initial_paginator_state=initial_paginator_state,
    )


def _source_response(endpoint: str, config: OctolensEndpointConfig, resource: Iterable[Any]) -> SourceResponse:
    return SourceResponse(
        name=endpoint,
        items=lambda: resource,
        primary_keys=config.primary_key,
        partition_mode="datetime" if config.partition_key else None,
        partition_format="month" if config.partition_key else None,
        partition_keys=[config.partition_key] if config.partition_key else None,
    )


def octolens_source(
    api_key: str,
    endpoint: str,
    team_id: int,
    job_id: str,
    api_version: str,
    resumable_source_manager: ResumableSourceManager[OctolensResumeConfig],
) -> SourceResponse:
    config = OCTOLENS_ENDPOINTS[endpoint]

    if config.fanout is not None:
        resource = _dependent_resource(config, api_key, api_version, team_id, job_id, resumable_source_manager)
        return _source_response(endpoint, config, resource)

    rest_config: RESTAPIConfig = {
        "client": _client_config(api_key),
        "resource_defaults": {},
        "resources": [_resource(config, api_version)],
    }

    initial_paginator_state: Optional[dict[str, Any]] = None
    if config.paginated and resumable_source_manager.can_resume():
        resume = resumable_source_manager.load_state()
        if resume is not None and resume.cursor:
            initial_paginator_state = {"cursor": resume.cursor}

    def save_checkpoint(state: Optional[dict[str, Any]]) -> None:
        # Fires AFTER a page is yielded, so a crash re-fetches the page we were about to read rather
        # than skipping it — completeness over an occasional replayed row.
        if state and state.get("cursor"):
            resumable_source_manager.save_state(OctolensResumeConfig(cursor=str(state["cursor"])))

    resource = rest_api_resource(
        rest_config,
        team_id,
        job_id,
        None,
        resume_hook=save_checkpoint if config.paginated else None,
        initial_paginator_state=initial_paginator_state,
    )

    return _source_response(endpoint, config, resource)


def check_access(api_key: str, api_version: str) -> tuple[int, Optional[str]]:
    """Probe the credential-introspection endpoint and report `(status, message)`.

    Status mimics HTTP semantics: 200 = key is genuine, 401 = bad/expired key, 403 = valid key
    without the scope, 0 = could not reach or parse Octolens.
    """
    session = make_tracked_session(redact_values=(api_key,))
    try:
        response = session.get(
            f"{OCTOLENS_BASE_URL}{_api_path(api_version, AUTH_PROBE_PATH)}",
            headers={"Authorization": f"Bearer {api_key}", "Accept": "application/json"},
            timeout=15,
        )
    except Exception as e:  # noqa: BLE001 — a credential probe must never raise out of validation
        return 0, f"Could not connect to Octolens: {e}"

    if response.status_code in (401, 403):
        return response.status_code, _error_message(response)

    if not 200 <= response.status_code < 300:
        return response.status_code, _error_message(response) or f"Octolens returned HTTP {response.status_code}"

    try:
        body = response.json()
    except ValueError:
        # A 200 that isn't JSON (a proxy or maintenance page) is not a valid Octolens response.
        return 0, "Octolens returned a non-JSON response"

    if not isinstance(body, dict) or not body.get("organizationId"):
        return 0, "Octolens did not return a workspace for this API key"

    return 200, None


def _error_message(response: Any) -> Optional[str]:
    """Pull `error.message` out of Octolens' error envelope, which every non-2xx response shares."""
    try:
        body = response.json()
    except ValueError:
        return None
    if not isinstance(body, dict):
        return None
    error = body.get("error")
    if not isinstance(error, dict):
        return None
    message = error.get("message")
    return str(message) if message else None