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.

3.7 KB
Raw Blame
from typing import TYPE_CHECKING

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

from posthog.models.file_system.constants import DEFAULT_SURFACE
from posthog.models.file_system.file_system_mixin import FileSystemSyncMixin
from posthog.models.file_system.file_system_representation import FileSystemRepresentation
from posthog.models.utils import RootTeamMixin, UUIDTModel, sane_repr

if TYPE_CHECKING:
    from posthog.models.team import Team


class EarlyAccessFeature(FileSystemSyncMixin, RootTeamMixin, UUIDTModel):
    class Meta:
        db_table = "posthog_earlyaccessfeature"
        managed = True

    class Stage(models.TextChoices):
        DRAFT = "draft", "draft"
        CONCEPT = "concept", "concept"
        ALPHA = "alpha", "alpha"
        BETA = "beta", "beta"
        GENERAL_AVAILABILITY = "general-availability", "general availability"
        ARCHIVED = "archived", "archived"

    # Stages where opted-in users get the feature flag enabled.
    # CONCEPT is excluded - it's for gauging interest before enabling functionality.
    ActiveStage = frozenset({Stage.ALPHA, Stage.BETA, Stage.GENERAL_AVAILABILITY})

    team = models.ForeignKey(
        "posthog.Team",
        on_delete=models.CASCADE,
        related_name="features",
        related_query_name="feature",
    )
    feature_flag = models.ForeignKey(
        "feature_flags.FeatureFlag",
        null=True,
        blank=True,
        on_delete=models.PROTECT,
        related_name="features",
        related_query_name="feature",
    )
    created_by = models.ForeignKey(
        "posthog.User",
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="early_access_features",
        # db_constraint disabled to avoid locking the hot posthog_user table when adding this FK.
        db_constraint=False,
    )
    name = models.CharField(max_length=200)
    description = models.TextField(blank=True)
    stage = models.CharField(max_length=40, choices=Stage)
    documentation_url = models.URLField(max_length=800, blank=True)
    payload = models.JSONField(default=dict, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    # Exactly one of assigned_user/assigned_role may be set (same convention as ErrorTrackingIssueAssignment).
    # Defaults to the creator on creation.
    assigned_user = models.ForeignKey(
        "posthog.User",
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="+",
        # db_constraint disabled to avoid locking the hot posthog_user table when adding this FK.
        db_constraint=False,
    )
    assigned_role = models.ForeignKey("ee.Role", null=True, blank=True, on_delete=models.SET_NULL, related_name="+")

    def __str__(self) -> str:
        return self.name

    __repr__ = sane_repr("id", "name", "team_id", "stage")

    @classmethod
    def get_file_system_unfiled(cls, team: "Team", surface: str = DEFAULT_SURFACE) -> QuerySet["EarlyAccessFeature"]:
        base_qs = cls.objects.filter(team=team)
        return cls._filter_unfiled_queryset(base_qs, team, type="early_access_feature", ref_field="id", surface=surface)

    def get_file_system_representation(self) -> FileSystemRepresentation:
        return FileSystemRepresentation(
            base_folder=self._get_assigned_folder("Unfiled/Early Access Features"),
            type="early_access_feature",  # sync with APIScopeObject in scopes.py
            ref=str(self.id),
            name=self.name or "Untitled",
            href=f"/early_access_features/{self.id}",
            meta={
                "created_at": str(self.created_at),
                "created_by": self.created_by_id,
            },
            should_delete=False,
        )