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.5 KB
Raw Blame
from dataclasses import dataclass, field
from typing import Literal, Optional

from products.warehouse_sources.sources.sdk import DependentEndpointConfig, IncrementalField, IncrementalFieldType


def _timestamp_field(name: str) -> IncrementalField:
    return {
        "label": name,
        "type": IncrementalFieldType.DateTime,
        "field": name,
        "field_type": IncrementalFieldType.DateTime,
    }


# Rootly resources are Rails records that all expose `updated_at` / `created_at`. For mutable
# collections we advertise both: `updated_at` (default, catches edits) and `created_at`.
_UPDATED_THEN_CREATED: list[IncrementalField] = [_timestamp_field("updated_at"), _timestamp_field("created_at")]


@dataclass(frozen=False)
class RootlyEndpointConfig:
    name: str
    path: str
    incremental_fields: list[IncrementalField] = field(default_factory=list)
    # Only True where Rootly exposes a genuine server-side `filter[<field>][gt]` + `sort=<field>`.
    # When True the transport cursors and sorts ascending on the user's chosen incremental field, so
    # rows arrive in watermark order and pagination terminates server-side at the cutoff.
    supports_incremental: bool = False
    # Stable field to partition by — always a creation timestamp, never `updated_at`.
    partition_key: Optional[str] = "created_at"
    primary_keys: list[str] = field(default_factory=lambda: ["id"])
    should_sync_default: bool = True
    page_size: int = 100
    # "links_next" follows the JSON:API `links.next` URL. "cursor" sends `meta.next_cursor` back as
    # `page[after]`, for feeds that return no `links`.
    pagination: Literal["links_next", "cursor"] = "links_next"
    # Set for endpoints that are only reachable under a parent resource (one request series per parent).
    fanout: Optional[DependentEndpointConfig] = None
    default_incremental_field: Optional[str] = None


# Endpoint catalog. Paths are relative to https://api.rootly.com/v1. The set mirrors the streams a
# user actually wants from an incident-management platform and matches the resource list other
# connectors (Airbyte/Fivetran) expose for Rootly. Every path here was confirmed to be a real route
# against the live API (real routes return 401 on a bad token; unknown routes return 404).
ROOTLY_ENDPOINTS: dict[str, RootlyEndpointConfig] = {
    # High-value, high-volume mutable collections — incremental.
    "incidents": RootlyEndpointConfig(
        name="incidents",
        path="/incidents",
        supports_incremental=True,
        incremental_fields=_UPDATED_THEN_CREATED,
    ),
    "alerts": RootlyEndpointConfig(
        name="alerts",
        path="/alerts",
        supports_incremental=True,
        incremental_fields=_UPDATED_THEN_CREATED,
    ),
    "action_items": RootlyEndpointConfig(
        name="action_items",
        path="/action_items",
        supports_incremental=True,
        incremental_fields=_UPDATED_THEN_CREATED,
    ),
    "post_mortems": RootlyEndpointConfig(
        name="post_mortems",
        path="/post_mortems",
        supports_incremental=True,
        incremental_fields=_UPDATED_THEN_CREATED,
    ),
    # Pulses are the per-incident activity timeline — high volume, so off by default.
    "pulses": RootlyEndpointConfig(
        name="pulses",
        path="/pulses",
        supports_incremental=True,
        incremental_fields=_UPDATED_THEN_CREATED,
        should_sync_default=False,
    ),
    # The flat event feed across all alerts. It filters and sorts only on `created_at`, and caps
    # `page[size]` at 50. High volume, so off by default.
    "alert_events": RootlyEndpointConfig(
        name="alert_events",
        path="/alert_events",
        supports_incremental=True,
        incremental_fields=[_timestamp_field("created_at")],
        pagination="cursor",
        page_size=50,
        should_sync_default=False,
    ),
    # Rootly lists incident timeline events only per incident and with no time filter, so this
    # fans out over every incident on each sync. Off by default because of the request volume.
    "incident_events": RootlyEndpointConfig(
        name="incident_events",
        path="/incidents/{incident_id}/events",
        fanout=DependentEndpointConfig(
            parent_name="incidents",
            resolve_param="incident_id",
            resolve_field="id",
            include_from_parent=["id"],
            parent_field_renames={"id": "incident_id"},
            # An incident deleted between the parent listing and its child fetch must not fail the sync.
            child_response_actions=[{"status_code": 404, "action": "ignore"}],
        ),
        primary_keys=["incident_id", "id"],
        should_sync_default=False,
    ),
    # Configuration / reference resources — small and slowly-changing, so full refresh each sync.
    "users": RootlyEndpointConfig(name="users", path="/users"),
    "teams": RootlyEndpointConfig(name="teams", path="/teams"),
    "services": RootlyEndpointConfig(name="services", path="/services"),
    "functionalities": RootlyEndpointConfig(name="functionalities", path="/functionalities"),
    "schedules": RootlyEndpointConfig(name="schedules", path="/schedules"),
    "escalation_policies": RootlyEndpointConfig(name="escalation_policies", path="/escalation_policies"),
    "workflows": RootlyEndpointConfig(name="workflows", path="/workflows"),
    "statuses": RootlyEndpointConfig(name="statuses", path="/statuses"),
    # Small enumeration resources whose timestamp columns we haven't confirmed against the live API.
    # Partitioning a handful of rows buys nothing, so don't risk partitioning on an absent field.
    "environments": RootlyEndpointConfig(name="environments", path="/environments", partition_key=None),
    "severities": RootlyEndpointConfig(name="severities", path="/severities", partition_key=None),
    "incident_types": RootlyEndpointConfig(name="incident_types", path="/incident_types", partition_key=None),
    "causes": RootlyEndpointConfig(name="causes", path="/causes", partition_key=None),
}

ENDPOINTS = tuple(ROOTLY_ENDPOINTS.keys())

# Mapping of endpoint name -> incremental fields, for endpoints that expose a server-side filter.
# Only incremental endpoints carry fields; full-refresh ones are omitted (empty list == full refresh).
INCREMENTAL_FIELDS: dict[str, list[IncrementalField]] = {
    name: config.incremental_fields for name, config in ROOTLY_ENDPOINTS.items() if config.incremental_fields
}

# Per-endpoint default sync flag; the high-volume timeline and event endpoints are off by default.
SHOULD_SYNC_DEFAULT: dict[str, bool] = {name: config.should_sync_default for name, config in ROOTLY_ENDPOINTS.items()}