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.1 KB
Raw Blame
from typing import Optional

from django.db import models
from django.utils import timezone

from posthog.migration_helpers import deprecate_field
from posthog.models.activity_logging.model_activity import ModelActivityMixin


class Annotation(ModelActivityMixin, models.Model):
    class Scope(models.TextChoices):
        INSIGHT = "dashboard_item", "insight"
        DASHBOARD = "dashboard", "dashboard"
        PROJECT = "project", "project"
        ORGANIZATION = "organization", "organization"
        RECORDING = "recording", "recording"

    class CreationType(models.TextChoices):
        USER = "USR", "user"
        GITHUB = "GIT", "GitHub"

    content = models.CharField(max_length=8192, null=True, blank=True)
    created_at = models.DateTimeField(default=timezone.now, null=True)
    updated_at = models.DateTimeField(auto_now=True)
    dashboard_item = models.ForeignKey(
        "product_analytics.Insight", on_delete=models.SET_NULL, null=True, blank=True, related_name="+"
    )
    dashboard = models.ForeignKey(
        "dashboards.Dashboard", on_delete=models.SET_NULL, null=True, blank=True, related_name="+"
    )
    team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, related_name="+")
    organization = models.ForeignKey("posthog.Organization", on_delete=models.CASCADE, null=True, related_name="+")
    created_by = models.ForeignKey("posthog.User", on_delete=models.SET_NULL, null=True, blank=True, related_name="+")
    scope = models.CharField(max_length=24, choices=Scope, default=Scope.INSIGHT)
    # Optional emoji shown in place of the default badge when surfacing the annotation.
    # Long enough to hold a single multi-codepoint grapheme (ZWJ sequences, skin-tone modifiers).
    emoji = models.CharField(max_length=16, null=True, blank=True)
    creation_type = models.CharField(max_length=3, choices=CreationType, default=CreationType.USER)
    date_marker = models.DateTimeField(null=True, blank=True)
    deleted = models.BooleanField(default=False)
    hidden_in_user_interface = models.BooleanField(null=True, blank=True)

    # DEPRECATED: replaced by scope
    apply_all = models.BooleanField(null=True)
    # DEPRECATED: moved to the comment model
    recording_id = deprecate_field(models.UUIDField(null=True, blank=True))
    # DEPRECATED: moved to the comment model
    is_emoji = deprecate_field(models.BooleanField(default=False, null=True, blank=True))

    class Meta:
        db_table = "posthog_annotation"

    @property
    def insight_short_id(self) -> Optional[str]:
        return self.dashboard_item.short_id if self.dashboard_item is not None else None

    @property
    def insight_name(self) -> Optional[str]:
        return self.dashboard_item.name if self.dashboard_item is not None else None

    @property
    def insight_derived_name(self) -> Optional[str]:
        return self.dashboard_item.derived_name if self.dashboard_item is not None else None

    @property
    def dashboard_name(self) -> Optional[str]:
        return self.dashboard.name if self.dashboard is not None else None

    def get_analytics_metadata(self):
        return {"scope": str(self.scope), "date_marker": self.date_marker}