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.

4.6 KB
Raw Blame
from typing import cast

from products.warehouse_sources.sources.modal._config import ModalSourceConfig
from products.warehouse_sources.sources.modal.canonical_descriptions import CANONICAL_DESCRIPTIONS
from products.warehouse_sources.sources.modal.modal import (
    AUTH_ERROR,
    PERMISSION_ERROR,
    ModalResumeConfig,
    modal_source,
    validate_credentials,
)
from products.warehouse_sources.sources.modal.settings import (
    BILLING_REPORT_TABLES,
    BILLING_SUMMARY,
    DEFAULT_INCREMENTAL_LOOKBACK_SECONDS,
    ENDPOINTS,
    INCREMENTAL_FIELDS,
)
from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    ResumableSource,
    ResumableSourceManager,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    build_endpoint_schemas,
    detect,
)


@SourceRegistry.register
class ModalSource(ResumableSource[ModalSourceConfig, ModalResumeConfig]):
    codebase_detection = detect(
        env=("^MODAL_TOKEN_ID$", "^MODAL_TOKEN_SECRET$"),
        npm=("modal",),
        pypi=("modal",),
        go=("github.com/modal-labs/modal-client/go", "github.com/modal-labs/libmodal/modal-go"),
    )

    lists_tables_without_credentials = True
    api_docs_url = "https://modal.com/docs/sdk/py/latest/Workspace#billingreport"

    @property
    def source_type(self) -> ExternalDataSourceType:
        return ExternalDataSourceType.MODAL

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.MODAL,
            category=DataWarehouseSourceCategory.ENGINEERING___MONITORING,
            label="Modal",
            iconPath="/static/services/modal.png",
            caption=(
                "Create a token in [Modal settings](https://modal.com/settings). "
                "Use a service-user token with workspace billing access. "
                "A Team or Enterprise plan is required for [Workspace billing reports](https://modal.com/docs/sdk/py/latest/Workspace#billingreport). "
                "The first sync imports 365 days of daily reports or 30 days of hourly reports."
            ),
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="token_id",
                        label="Token ID",
                        type=SourceFieldInputConfigType.TEXT,
                        required=True,
                        placeholder="ak-...",
                        secret=False,
                    ),
                    SourceFieldInputConfig(
                        name="token_secret",
                        label="Token secret",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="as-...",
                        secret=True,
                    ),
                ],
            ),
            releaseStatus=ReleaseStatus.BETA,
        )

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {AUTH_ERROR: AUTH_ERROR, PERMISSION_ERROR: PERMISSION_ERROR}

    def get_canonical_descriptions(self) -> CanonicalDescriptions:
        return CANONICAL_DESCRIPTIONS

    def get_schemas(
        self,
        config: ModalSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        return build_endpoint_schemas(
            ENDPOINTS,
            INCREMENTAL_FIELDS,
            names,
            merge_only=(*BILLING_REPORT_TABLES, BILLING_SUMMARY),
            default_incremental_lookback_seconds=DEFAULT_INCREMENTAL_LOOKBACK_SECONDS,
        )

    def validate_credentials(
        self,
        config: ModalSourceConfig,
        team_id: int,
        schema_name: str | None = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        return validate_credentials(config)

    def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[ModalResumeConfig]:
        return ResumableSourceManager(inputs, ModalResumeConfig)

    def source_for_pipeline(
        self,
        config: ModalSourceConfig,
        resumable_source_manager: ResumableSourceManager[ModalResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return modal_source(config, inputs, resumable_source_manager)