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.

19.0 KB
Raw Blame
from __future__ import annotations

from typing import Any, cast
from urllib.parse import urlparse

from django.db import transaction
from django.db.models import QuerySet

from drf_spectacular.utils import extend_schema_field
from rest_framework import serializers
from rest_framework.exceptions import ValidationError

from posthog.exceptions import as_drf_validation_error
from posthog.security.url_validation import is_microsoft_teams_webhook_url

from products.alerts.backend.facade.destinations import validate_destination_data
from products.alerts_platform.backend.facade.contracts import (
    AlertDestinationData,
    AlertDestinationValidationError,
    DestinationType,
)
from products.billing_alerts.backend.facade import api as billing_alerts_api
from products.billing_alerts.backend.facade.api import (
    BillingAlertConfiguration,
    BillingAlertEvent,
    validate_threshold_configuration,
)

_DESTINATIONS_CACHE_KEY = "_billing_alert_destinations_by_alert_id"
_NOT_PROVIDED = object()

MAX_DESTINATION_IDS_PER_DELETE_REQUEST = 100


def _any_field_changed(
    instance: BillingAlertConfiguration,
    validated_data: dict[str, Any],
    fields: set[str],
) -> bool:
    return any(field in validated_data and validated_data[field] != getattr(instance, field) for field in fields)


class BillingAlertEventSerializer(serializers.ModelSerializer):
    # Model properties derived from the claim, so DRF cannot generate them.
    evaluation_date = serializers.DateField(
        read_only=True,
        allow_null=True,
        help_text="Billing data date evaluated by this event.",
    )
    configuration_revision = serializers.IntegerField(
        source="claim.configuration_revision",
        read_only=True,
        help_text="Configuration revision used for this evaluation.",
    )

    class Meta:
        model = BillingAlertEvent
        fields = [
            "id",
            "kind",
            "source",
            "attempt_number",
            "created_at",
            "evaluation_date",
            "configuration_revision",
            "period_start",
            "period_end",
            "metric",
            "current_value",
            "baseline_value",
            "absolute_delta",
            "relative_delta_percentage",
            "threshold_breached",
            "state_before",
            "state_after",
            "notification_sent_at",
            "targets_notified",
            "query_duration_ms",
            "error_code",
            "error_message",
            "reason",
        ]
        read_only_fields = [field for field in fields if field not in ("evaluation_date", "configuration_revision")]
        extra_kwargs = {
            "id": {"help_text": "Unique identifier for this billing alert event."},
            "kind": {"help_text": "Event kind for a check, state transition, or delivery-worthy alert event."},
            "source": {"help_text": "Whether this evaluation was scheduled or manually requested."},
            "attempt_number": {"help_text": "Attempt number for this billing date and configuration revision."},
            "created_at": {"help_text": "When this event was recorded."},
            "period_start": {"help_text": "Start of the evaluated billing period."},
            "period_end": {"help_text": "End of the evaluated billing period."},
            "metric": {"help_text": "Billing metric evaluated by this event."},
            "current_value": {"help_text": "Metric value for the evaluated billing date."},
            "baseline_value": {"help_text": "Average metric value across the baseline window."},
            "absolute_delta": {"help_text": "Current value minus the baseline value."},
            "relative_delta_percentage": {"help_text": "Percentage change against the baseline value."},
            "threshold_breached": {"help_text": "Whether the evaluated value breached the configured threshold."},
            "state_before": {"help_text": "Alert state before this event was applied."},
            "state_after": {"help_text": "Alert state after this event was applied."},
            "notification_sent_at": {"help_text": "When notifications for this event were delivered."},
            "targets_notified": {"help_text": "Notification targets recorded for this event."},
            "query_duration_ms": {"help_text": "Milliseconds spent fetching billing data for this evaluation."},
            "error_code": {"help_text": "Exception class name recorded when the evaluation failed."},
            "error_message": {"help_text": "Failure description recorded when the evaluation failed."},
            "reason": {"help_text": "Human-readable explanation of the evaluation outcome."},
        }


class BillingAlertDestinationSummarySerializer(serializers.Serializer):
    type = serializers.ChoiceField(choices=["slack", "webhook", "teams"])
    hog_function_ids = serializers.ListField(child=serializers.UUIDField())


class BillingAlertDestinationCreateDataSerializer(serializers.Serializer):
    type = serializers.ChoiceField(choices=["slack", "webhook", "teams"], help_text="Destination type.")
    slack_workspace_id = serializers.IntegerField(
        required=False, help_text="Slack integration ID in the alert execution project."
    )
    slack_channel_id = serializers.CharField(required=False, help_text="Slack channel ID for alert delivery.")
    slack_channel_name = serializers.CharField(
        required=False, allow_blank=True, help_text="Optional Slack channel name shown in the UI."
    )
    webhook_url = serializers.URLField(
        required=False, help_text="HTTPS webhook URL for webhook or Microsoft Teams delivery."
    )

    def validate(self, attrs: dict) -> dict:
        data = cast(AlertDestinationData, attrs)
        data["type"] = DestinationType(attrs["type"])
        try:
            validate_destination_data(data, allowed_destination_types=billing_alerts_api.BILLING_DESTINATION_TYPES)
        except AlertDestinationValidationError as error:
            raise as_drf_validation_error(error)

        # URL-shape checks beyond the shared required-field validation.
        webhook_url = attrs.get("webhook_url")
        if data["type"] in (DestinationType.WEBHOOK, DestinationType.TEAMS) and webhook_url:
            parsed_url = urlparse(webhook_url)
            if parsed_url.scheme != "https" or not parsed_url.netloc:
                raise ValidationError({"webhook_url": "Webhook URLs must be valid HTTPS URLs."})
            if data["type"] == DestinationType.TEAMS and not is_microsoft_teams_webhook_url(webhook_url):
                raise ValidationError({"webhook_url": "Enter a supported Microsoft Teams webhook URL."})
        return attrs


class BillingAlertDestinationChangesSerializer(serializers.Serializer):
    delete = serializers.ListField(
        child=serializers.ListField(
            child=serializers.UUIDField(),
            min_length=1,
            max_length=MAX_DESTINATION_IDS_PER_DELETE_REQUEST,
        ),
        required=False,
        default=list,
    )

    def get_fields(self) -> dict[str, serializers.Field]:
        fields = super().get_fields()
        fields["create"] = BillingAlertDestinationCreateDataSerializer(many=True, required=False, default=list)
        return fields


class BillingAlertConfigurationSerializer(serializers.ModelSerializer):
    id = serializers.UUIDField(read_only=True, help_text="Unique identifier for this billing alert.")
    organization_id = serializers.UUIDField(read_only=True, help_text="Organization this billing alert belongs to.")
    execution_team_id = serializers.IntegerField(
        source="team_id",
        read_only=True,
        allow_null=True,
        help_text="Team used as the execution context for internal notification destinations.",
    )
    created_by_id = serializers.IntegerField(
        read_only=True, allow_null=True, help_text="User ID that created this alert."
    )
    updated_by_id = serializers.IntegerField(
        read_only=True,
        allow_null=True,
        help_text="User ID that last updated this alert.",
    )
    name = serializers.CharField(max_length=160, help_text="Display name for this billing alert.")
    description = serializers.CharField(required=False, allow_blank=True, help_text="Optional internal description.")
    enabled = serializers.BooleanField(required=False, help_text="Whether scheduled checks should evaluate this alert.")
    metric = serializers.ChoiceField(
        choices=BillingAlertConfiguration.Metric.choices,
        required=False,
        help_text="Billing-period total to evaluate: current spend so far, or projected period-end spend.",
    )
    threshold_type = serializers.ChoiceField(
        choices=BillingAlertConfiguration.ThresholdType.choices,
        required=False,
        help_text="Threshold rule type. The first version supports absolute value only.",
    )
    threshold_percentage = serializers.DecimalField(
        max_digits=8,
        decimal_places=2,
        required=False,
        allow_null=True,
        help_text="Reserved for future increase-over-baseline rules. Not used by absolute value alerts.",
    )
    threshold_value = serializers.DecimalField(
        max_digits=20,
        decimal_places=6,
        required=False,
        allow_null=True,
        help_text="Absolute value or absolute increase that triggers absolute threshold alerts.",
    )
    baseline_window_days = serializers.IntegerField(
        required=False,
        min_value=1,
        max_value=90,
        help_text="Reserved for future increase-over-baseline rules. Not used by absolute value alerts.",
    )
    snoozed_until = serializers.DateTimeField(
        required=False,
        allow_null=True,
        help_text="ISO 8601 timestamp until which evaluation and notifications are snoozed, or null to resume.",
    )
    destinations = serializers.SerializerMethodField(
        help_text="Notification destination groups configured for this alert, including their shared HogFunctions.",
    )
    destination_changes = BillingAlertDestinationChangesSerializer(
        required=False,
        write_only=True,
        help_text="Destination groups to create or delete in the same transaction as this configuration write.",
    )

    class Meta:
        model = BillingAlertConfiguration
        fields = [
            "id",
            "organization_id",
            "execution_team_id",
            "created_by_id",
            "updated_by_id",
            "name",
            "description",
            "enabled",
            "metric",
            "currency",
            "configuration_revision",
            "threshold_type",
            "threshold_percentage",
            "threshold_value",
            "baseline_window_days",
            "state",
            "snoozed_until",
            "next_check_at",
            "last_checked_at",
            "last_notified_at",
            "consecutive_failures",
            "destinations",
            "destination_changes",
            "created_at",
            "updated_at",
        ]
        read_only_fields = [
            "currency",
            "configuration_revision",
            "state",
            "next_check_at",
            "last_checked_at",
            "last_notified_at",
            "consecutive_failures",
            "created_at",
            "updated_at",
        ]
        extra_kwargs = {
            "currency": {"help_text": "Server-controlled currency for spend values."},
            "configuration_revision": {"help_text": "Revision incremented whenever evaluation behavior changes."},
            "state": {"help_text": "Current lifecycle state of this alert."},
            "next_check_at": {"help_text": "When the next scheduled evaluation is due."},
            "last_checked_at": {"help_text": "When this alert was last evaluated."},
            "last_notified_at": {"help_text": "When notifications were last delivered for this alert."},
            "consecutive_failures": {"help_text": "Number of consecutive failed evaluations."},
            "created_at": {"help_text": "When this alert was created."},
            "updated_at": {"help_text": "When this alert was last updated."},
        }

    @extend_schema_field(BillingAlertDestinationSummarySerializer(many=True))
    def get_destinations(self, obj: BillingAlertConfiguration) -> list[dict[str, Any]]:
        cache = self.context.get(_DESTINATIONS_CACHE_KEY)
        if cache is None:
            alerts = self._destination_cache_alerts(obj)
            cache = billing_alerts_api.destinations_for_alerts(alerts)
            self.context[_DESTINATIONS_CACHE_KEY] = cache
        destinations_by_alert_id = cast(dict[str, list[dict[str, Any]]], cache)
        return destinations_by_alert_id.get(str(obj.id), [])

    def _destination_cache_alerts(self, obj: BillingAlertConfiguration) -> list[BillingAlertConfiguration]:
        parent_instance = getattr(getattr(self, "parent", None), "instance", None)
        instance = parent_instance if parent_instance is not None else self.instance

        if isinstance(instance, BillingAlertConfiguration):
            return [instance]
        if isinstance(instance, QuerySet | list):
            alerts = [item for item in instance if isinstance(item, BillingAlertConfiguration)]
            if alerts:
                return alerts
        return [obj]

    def validate(self, attrs: dict[str, Any]) -> dict[str, Any]:
        current = self.instance
        # On create there is no instance, so fall back to the model default rather than
        # RELATIVE_INCREASE: an omitted threshold_type should validate as what actually
        # gets persisted (ABSOLUTE_VALUE), not trip the "absolute value only" rejection below.
        threshold_type = attrs.get(
            "threshold_type",
            current.threshold_type if current else BillingAlertConfiguration.ThresholdType.ABSOLUTE_VALUE,
        )
        threshold_percentage = attrs.get(
            "threshold_percentage",
            current.threshold_percentage if current else None,
        )
        threshold_value = attrs.get("threshold_value", current.threshold_value if current else None)

        errors = validate_threshold_configuration(
            threshold_type=threshold_type,
            threshold_percentage=threshold_percentage,
            threshold_value=threshold_value,
        )
        if errors:
            raise ValidationError(errors)

        # The first version evaluates a single billing-period total, so only absolute value
        # thresholds are meaningful. Increase-over-baseline rules need daily buckets.
        if threshold_type != BillingAlertConfiguration.ThresholdType.ABSOLUTE_VALUE:
            raise ValidationError(
                {"threshold_type": "Billing alerts currently support the absolute value threshold only."}
            )

        enabled = attrs.get("enabled", current.enabled if current else True)
        if attrs.get("snoozed_until") is not None and not enabled:
            raise ValidationError({"snoozed_until": "A disabled alert cannot also be snoozed."})
        return attrs

    def create(self, validated_data: dict[str, Any]) -> BillingAlertConfiguration:
        destination_changes = validated_data.pop("destination_changes", None)
        with transaction.atomic():
            alert = super().create(validated_data)
            billing_alerts_api.initialize_billing_alert_lifecycle(alert)
            if destination_changes:
                try:
                    billing_alerts_api.apply_destination_changes(
                        alert, request=self.context["request"], changes=destination_changes
                    )
                except AlertDestinationValidationError as error:
                    raise as_drf_validation_error(error)
            return alert

    def update(
        self,
        instance: BillingAlertConfiguration,
        validated_data: dict[str, Any],
    ) -> BillingAlertConfiguration:
        destination_changes = validated_data.pop("destination_changes", None)
        snoozed_until = validated_data.get("snoozed_until", _NOT_PROVIDED)
        evaluation_fields = {
            "threshold_type",
            "threshold_percentage",
            "threshold_value",
            "baseline_window_days",
        }
        revision_fields = evaluation_fields | {
            "enabled",
            "snoozed_until",
        }
        with transaction.atomic():
            locked = BillingAlertConfiguration.objects.select_for_update().get(
                pk=instance.pk,
                organization_id=instance.organization_id,
            )
            evaluation_changed = _any_field_changed(locked, validated_data, evaluation_fields)
            configuration_changed = _any_field_changed(
                locked,
                validated_data,
                revision_fields,
            )
            enabled_change: bool | None = None
            if "enabled" in validated_data and validated_data["enabled"] != locked.enabled:
                enabled_change = validated_data["enabled"]

            billing_alerts_api.apply_billing_alert_configuration_lifecycle(
                locked,
                enabled_change=enabled_change,
                snoozed_until_provided=snoozed_until is not _NOT_PROVIDED,
                snoozed_until=snoozed_until if snoozed_until is not _NOT_PROVIDED else None,
                threshold_changed=evaluation_changed,
            )

            updated = super().update(locked, validated_data)
            billing_alerts_api.reschedule_billing_alert_configuration(
                updated,
                configuration_changed=configuration_changed,
            )
            if destination_changes:
                try:
                    billing_alerts_api.apply_destination_changes(
                        updated, request=self.context["request"], changes=destination_changes
                    )
                except AlertDestinationValidationError as error:
                    raise as_drf_validation_error(error)
            return updated


class BillingAlertDeleteDestinationSerializer(serializers.Serializer):
    hog_function_ids = serializers.ListField(
        child=serializers.UUIDField(),
        min_length=1,
        max_length=MAX_DESTINATION_IDS_PER_DELETE_REQUEST,
        help_text="HogFunction IDs to delete as one atomic destination group.",
    )


class BillingAlertDestinationResponseSerializer(serializers.Serializer):
    hog_function_ids = serializers.ListField(child=serializers.UUIDField())


class BillingAlertCheckNowResponseSerializer(serializers.Serializer):
    event = BillingAlertEventSerializer(
        required=False,
        allow_null=True,
        help_text="Evaluation event recorded by the manual check, or null for a paused preview.",
    )
    dispatched_destinations = serializers.IntegerField(help_text="Number of destination HogFunctions queued.")
    preview = serializers.BooleanField(
        help_text="True when the alert is paused: it was evaluated but no notifications were sent.",
    )
    threshold_breached = serializers.BooleanField(
        help_text="Whether the evaluated billing-period total breached the configured threshold.",
    )
    current_value = serializers.CharField(
        required=False,
        allow_null=True,
        help_text="Evaluated billing-period total, or null when billing data was not available.",
    )