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.

5.8 KB
Raw Blame
from __future__ import annotations

from typing import Final, Literal

from products.alerts.backend.facade.destinations import destination_template_id, list_owned_alert_destinations
from products.alerts_platform.backend.facade.contracts import DestinationType, EventKindSpec

EventKind = Literal["firing", "resolved", "errored", "broken"]

BILLING_DESTINATION_TYPES = (DestinationType.SLACK, DestinationType.WEBHOOK, DestinationType.TEAMS)

DESTINATION_TYPE_BY_TEMPLATE_ID = {
    destination_template_id(destination_type): destination_type for destination_type in BILLING_DESTINATION_TYPES
}

_PRODUCT_LABEL = "billing alert"
_BASE_DATA = {
    "alert_id": "{event.properties.alert_id}",
    "alert_name": "{event.properties.alert_name}",
    "metric": "{event.properties.metric}",
    "current_value": "{event.properties.current_value}",
    "baseline_value": "{event.properties.baseline_value}",
    "absolute_delta": "{event.properties.absolute_delta}",
    "relative_delta_percentage": "{event.properties.relative_delta_percentage}",
    "evaluation_date": "{event.properties.evaluation_date}",
    "reason": "{event.properties.reason}",
    "alert_url": "{event.properties.alert_url}",
}


_ERROR_DATA = {
    "error_message": "{event.properties.error_message}",
    "consecutive_failures": "{event.properties.consecutive_failures}",
}


def _spec(
    *,
    event: str,
    display_kind: str,
    header: str,
    details: tuple[tuple[str, str], ...],
    extra_data: dict[str, str] | None = None,
) -> EventKindSpec:
    return EventKindSpec(
        event_id=f"$billing_alert_{event}",
        display_kind=display_kind,
        header=header,
        details=details,
        primary_action_url="{event.properties.alert_url}",
        primary_action_label="View billing alert",
        webhook_body={
            "id": "{event.uuid}",
            "type": f"billing_alert.{event}",
            "timestamp": "{event.properties.triggered_at}",
            "data": {**_BASE_DATA, **(extra_data or {})},
        },
        product_label=_PRODUCT_LABEL,
    )


EVENT_KIND_CONFIG: dict[EventKind, EventKindSpec] = {
    "firing": _spec(
        event="firing",
        display_kind="firing",
        header="Billing alert '{event.properties.alert_name}' is firing",
        details=(
            ("Metric", "{event.properties.metric}"),
            ("Current", "{event.properties.current_value}"),
            ("Baseline", "{event.properties.baseline_value}"),
            ("Reason", "{event.properties.reason}"),
        ),
    ),
    "resolved": _spec(
        event="resolved",
        display_kind="resolved",
        header="Billing alert '{event.properties.alert_name}' has resolved",
        details=(
            ("Metric", "{event.properties.metric}"),
            ("Current", "{event.properties.current_value}"),
            ("Baseline", "{event.properties.baseline_value}"),
            ("Reason", "{event.properties.reason}"),
        ),
    ),
    "errored": _spec(
        event="errored",
        display_kind="errored",
        header="Billing alert '{event.properties.alert_name}' could not evaluate",
        details=(
            ("Error", "{event.properties.error_message}"),
            ("Failure count", "{event.properties.consecutive_failures}"),
        ),
        extra_data=_ERROR_DATA,
    ),
    "broken": _spec(
        event="auto_disabled",
        display_kind="auto-disabled",
        header="Billing alert '{event.properties.alert_name}' was auto-disabled",
        details=(
            ("Reason", "{event.properties.consecutive_failures} consecutive check failures."),
            ("Last error", "{event.properties.error_message}"),
        ),
        extra_data=_ERROR_DATA,
    ),
}

EVENT_KINDS: tuple[EventKind, ...] = tuple(EVENT_KIND_CONFIG)
BILLING_ALERT_EVENT_IDS: Final = tuple(spec.event_id for spec in EVENT_KIND_CONFIG.values())

BILLING_ALERT_SLACK_CONTEXT_ELEMENTS = (
    "Organization-wide billing alert",
    "Execution project: <{project.url}|{project.name}>",
)


def destination_groups_for_alerts(
    *,
    team_ids: set[int],
    alert_ids: set[str],
) -> dict[str, dict[str, dict[str, str]]]:
    """Map alert id -> destination type value -> event id -> HogFunction id for enabled,
    billing-owned destinations.
    """
    if not team_ids or not alert_ids:
        return {}

    rows = (
        row
        for team_id in team_ids
        for row in list_owned_alert_destinations(
            team_id=team_id,
            alert_ids=alert_ids,
            allowed_event_ids=BILLING_ALERT_EVENT_IDS,
            template_ids=list(DESTINATION_TYPE_BY_TEMPLATE_ID),
            enabled=True,
        )
    )

    groups: dict[str, dict[str, dict[str, str]]] = {}
    for row in rows:
        destination_type = DESTINATION_TYPE_BY_TEMPLATE_ID.get(row.template_id) if row.template_id else None
        if destination_type is None or row.filters is None:
            continue
        properties = row.filters.get("properties") or []
        events = row.filters.get("events") or []
        if not isinstance(properties, list) or not isinstance(events, list):
            continue
        event_id = next(
            (
                event_filter.get("id")
                for event_filter in events
                if isinstance(event_filter, dict) and event_filter.get("type") == "events"
            ),
            None,
        )
        if event_id not in BILLING_ALERT_EVENT_IDS:
            continue
        for property_filter in properties:
            if not isinstance(property_filter, dict) or property_filter.get("key") != "alert_id":
                continue
            alert_id = str(property_filter.get("value"))
            if alert_id in alert_ids:
                groups.setdefault(alert_id, {}).setdefault(destination_type.value, {})[event_id] = str(
                    row.hog_function_id
                )
            break
    return groups