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.

6.0 KB
Raw Blame
from typing import Literal

from products.warehouse_sources.sources.sdk import IncrementalField, frozen

FetchMethod = Literal["get", "post"]

# (connect, read) timeout for sync requests against the user-configured host. Bounds how long a
# stalled or slow-responding custom host can occupy an import worker; the credential probe in
# bigeye.py uses its own shorter timeout.
REQUEST_TIMEOUT_SECONDS: tuple[float, float] = (10.0, 60.0)


@frozen
class BigeyeEndpointConfig:
    name: str
    table_name: str
    path: str
    method: FetchMethod
    # Key in the JSON response body that holds the list of rows.
    data_selector: str
    # True for the `POST .../fetch` endpoints that page via a `pageCursor` in the JSON body.
    # False for the small GET endpoints that return their full result in one response.
    paginated: bool
    primary_keys: tuple[str, ...] = ("id",)
    # True for endpoints scoped to one metric. They fan out over the Metrics list, and each row
    # carries the parent's id as `metric_id`.
    per_metric: bool = False
    # True for endpoints scoped to one lineage data node. They fan out over the data node ids of the
    # Tables list, and each row carries the parent's node id as `root_data_node_id`.
    per_table_data_node: bool = False


# Bigeye's v1 API has no documented "updated since" filter on any list endpoint (see
# INCREMENTAL_FIELDS below), so every table here is full refresh. The `.../fetch` endpoints are
# still resumable via their `pageCursor`, which matters for large workspaces (many products/warehouse_sources/sources/tables/
# issues) surviving a heartbeat timeout mid-sync.
BIGEYE_ENDPOINTS: dict[str, BigeyeEndpointConfig] = {
    "Workspaces": BigeyeEndpointConfig(
        name="Workspaces",
        table_name="workspaces",
        path="/api/v1/workspaces",
        method="get",
        data_selector="workspaces",
        paginated=False,
    ),
    "Sources": BigeyeEndpointConfig(
        name="Sources",
        table_name="sources",
        path="/api/v1/sources/fetch",
        method="post",
        data_selector="sources",
        paginated=True,
    ),
    "Tables": BigeyeEndpointConfig(
        name="Tables",
        table_name="tables",
        path="/api/v1/tables/fetch",
        method="post",
        data_selector="tables",
        paginated=True,
    ),
    "Metrics": BigeyeEndpointConfig(
        name="Metrics",
        table_name="metrics",
        path="/api/v1/metrics",
        method="get",
        data_selector="metrics",
        paginated=False,
    ),
    "Collections": BigeyeEndpointConfig(
        name="Collections",
        table_name="collections",
        path="/api/v1/collections/info",
        method="get",
        data_selector="collectionInfos",
        paginated=False,
        # Rows nest their id under `collectionConfiguration.id`; `_flatten_collection` copies it
        # to the top level before this primary key is applied.
        primary_keys=("id",),
    ),
    "Issues": BigeyeEndpointConfig(
        name="Issues",
        table_name="issues",
        path="/api/v1/issues/fetch",
        method="post",
        # Bigeye's issue-list response wraps rows under the singular key `issue`, not `issues`.
        data_selector="issue",
        paginated=True,
    ),
    "Deltas": BigeyeEndpointConfig(
        name="Deltas",
        table_name="deltas",
        path="/api/v1/deltas/fetch",
        method="post",
        data_selector="deltaInfos",
        paginated=True,
        # Rows nest their id under `delta.id`; `_flatten_delta` copies it to the top level.
        primary_keys=("id",),
    ),
    "SchemaChanges": BigeyeEndpointConfig(
        name="SchemaChanges",
        table_name="schema_changes",
        path="/api/v1/schema-changes/fetch",
        method="post",
        data_selector="schemaChanges",
        paginated=True,
    ),
    "MetricRuns": BigeyeEndpointConfig(
        name="MetricRuns",
        table_name="metric_runs",
        path="/api/v1/metrics/runs",
        method="post",
        data_selector="metricRuns",
        paginated=True,
        per_metric=True,
        primary_keys=("metric_id", "id"),
    ),
    "MetricRevisions": BigeyeEndpointConfig(
        name="MetricRevisions",
        table_name="metric_revisions",
        path="/api/v1/metrics/{metricId}/revisions",
        method="get",
        data_selector="revisions",
        paginated=False,
        per_metric=True,
        # Revisions have no id of their own. `_flatten_revision` lifts these out of `revisionInfo`.
        primary_keys=("metric_id", "revision_epoch_seconds", "revision_type"),
    ),
    "MetricScorecard": BigeyeEndpointConfig(
        name="MetricScorecard",
        table_name="metric_scorecard",
        path="/api/v1/metrics/scorecard",
        method="post",
        data_selector="scorecards",
        paginated=False,
        # `_explode_scorecard` emits one row per metric category per day.
        primary_keys=("metric_category", "run_at_epoch_seconds"),
    ),
    "LineageGraph": BigeyeEndpointConfig(
        name="LineageGraph",
        table_name="lineage_graph",
        path="/api/v2/lineage/nodes/{dataNodeId}/graph",
        method="get",
        # A map of node id -> node, not a list. `_lineage_graph_rows` emits one row per node.
        data_selector="nodes",
        paginated=False,
        per_table_data_node=True,
        primary_keys=("root_data_node_id", "id"),
    ),
}

# Only the direct neighbours of each table. Every edge still appears in the graph of both of its
# tables, and an unbounded depth repeats the whole connected graph once per table in it.
LINEAGE_GRAPH_DEPTH = 1

# The scorecard API's documented maximum history window.
SCORECARD_DAYS_OF_HISTORY = 90

ENDPOINTS = tuple(BIGEYE_ENDPOINTS.keys())

# No endpoint exposes a server-side "updated since" / "modified after" filter in the published
# API reference, so nothing here can be a true incremental sync (see the module docstring in
# bigeye.py for what was checked). Every table syncs full refresh only.
INCREMENTAL_FIELDS: dict[str, list[IncrementalField]] = {}